<commit_before><?php

namespace AppBundle\EventListener;

use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Sonata\MediaBundle\Controller\Api\MediaController;

class SerializerSubscriber implements EventSubscriberInterface
{
    /** @var MediaController  */
    protected $mediaController;

    public function __construct(MediaController $mediaController)
    {
        $this->mediaController = $mediaController;
    }

    /**
     * @inheritdoc
     */
    static public function getSubscribedEvents()
    {
        return array(
            array('event' => 'serializer.pre_serialize', 'class' => 'AppBundle\Entity\Employee', 'method' => 'onPreEmployeeSerialize'),
        );
    }

    public function onPreEmployeeSerialize(ObjectEvent $event)
    {
        $avatarId = $event->getObject()->getAvatar()->getId();

        $avatarLinks = $this->mediaController->getMediumFormatsAction($avatarId);

        $event->getObject()->setAvatar($avatarLinks);
    }
}<commit_msg>Fixed Employee serialization if avatar is absent<commit_after><?php

namespace AppBundle\EventListener;

use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use Sonata\MediaBundle\Controller\Api\MediaController;

class SerializerSubscriber implements EventSubscriberInterface
{
    /** @var MediaController  */
    protected $mediaController;

    public function __construct(MediaController $mediaController)
    {
        $this->mediaController = $mediaController;
    }

    /**
     * @inheritdoc
     */
    static public function getSubscribedEvents()
    {
        return array(
            array('event' => 'serializer.pre_serialize', 'class' => 'AppBundle\Entity\Employee', 'method' => 'onPreEmployeeSerialize'),
        );
    }

    public function onPreEmployeeSerialize(ObjectEvent $event)
    {
        if (!$avatar = $event->getObject()->getAvatar()) {
            return;
        }

        $avatarLinks = $this->mediaController->getMediumFormatsAction($avatar->getId());
        $event->getObject()->setAvatar($avatarLinks);
    }
}
{'fraction_non_alphanumeric': 0.11794871794871795, 'fraction_numerical': 0.0, 'mean_word_length': 3.625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '930845', 'n_tokens_mistral': 588, 'n_tokens_neox': 572, 'n_words': 98}