Newscoop\Services\UserTopicService::updateTopics PHP Метод

updateTopics() публичный Метод

Update user topics
public updateTopics ( User $user, array $topics ) : void
$user Newscoop\Entity\User
$topics array
Результат void
    public function updateTopics(User $user, array $topics)
    {
        $repository = $this->em->getRepository('Newscoop\\Entity\\UserTopic');
        $userTopics = $repository->findByUser($user);
        $matches = array();
        foreach ($userTopics as $topic) {
            if (array_key_exists($topic->getTopicId(), $topics)) {
                $matches[$topic->getTopicId()] = $topic;
            }
        }
        foreach ($topics as $topicId => $status) {
            if ($status === 'false' && array_key_exists($topicId, $matches)) {
                foreach ($matches as $match) {
                    if ($match->getTopicId() == $topicId) {
                        $this->em->remove($match);
                    }
                }
            } elseif ($status === 'true' && !array_key_exists($topicId, $matches)) {
                $topic = $this->findTopic($topicId);
                if ($topic) {
                    $this->em->persist(new UserTopic($user, $this->findTopic($topicId)));
                }
            }
        }
        $this->em->flush();
    }