PonthubFileListener::postLoad()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace KI\PonthubBundle\Listener;
4
5
use Doctrine\ORM\Event\LifecycleEventArgs;
6
use KI\PonthubBundle\Entity\PonthubFile;
7
use KI\UserBundle\Entity\User;
8
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
9
10
class PonthubFileListener
11
{
12
    protected $tokenStorage;
13
14
    public function __construct(TokenStorageInterface $tokenStorage)
15
    {
16
        $this->tokenStorage = $tokenStorage;
17
    }
18
19
    public function postLoad(LifecycleEventArgs $args)
20
    {
21
        if ($token = $this->tokenStorage->getToken()) {
22
            $user   = $token->getUser();
23
            $entity = $args->getEntity();
24
25
            if ($entity instanceof PonthubFile && $user instanceof User) {
26
                $entity->setDownloaded(
27
                    $args->getEntityManager()
28
                        ->getRepository('KIPonthubBundle:PonthubFileUser')
29
                        ->hasBeenDownloadedBy($entity, $user)
30
                );
31
            }
32
        }
33
    }
34
}
35