LogEntryExtension.php 819 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
<?php

declare(strict_types=1);

namespace App\Twig;

use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
 * @SuppressWarnings("PHPMD.ShortVariable")
 */
class LogEntryExtension extends AbstractExtension
{
    /**
     * @var EntityManagerInterface
     */
    protected $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public function getFunctions()
    {
        return [
            new TwigFunction('getLogUser', [$this, 'getLogUser']),
        ];
    }

    public function getLogUser(?string $username): ?User
    {
        if (null !== $username) {
            return $this->em->getRepository(User::class)->findOneByUsername($username);
        }

        return null;
    }
}