PonthubFileListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A postLoad() 0 15 4
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