UserAclListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 5
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B onFlush() 0 28 4
A __construct() 0 4 1
1
<?php
2
3
namespace DoS\UserBundle\EventListener;
4
5
use Doctrine\ORM\Event\OnFlushEventArgs;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
8
use DoS\UserBundle\Model\UserAclOwnerAwareInterface;
9
10
class UserAclListener
11
{
12
    /**
13
     * @var ContainerInterface
14
     */
15
    protected $container;
16
17
    public function __construct(ContainerInterface $container)
18
    {
19
        $this->container = $container;
20
    }
21
22
    /**
23
     * @param OnFlushEventArgs $onFlushEventArgs
24
     */
25
    public function onFlush(OnFlushEventArgs $onFlushEventArgs)
26
    {
27
        $entityManager = $onFlushEventArgs->getEntityManager();
28
        $unitOfWork = $entityManager->getUnitOfWork();
29
30
        $entities = array_merge(
31
            $unitOfWork->getScheduledEntityInsertions(),
32
            $unitOfWork->getScheduledEntityUpdates()
33
        );
34
35
        foreach ($entities as $entity) {
36
            if (!$entity instanceof UserAclOwnerAwareInterface) {
37
                continue;
38
            }
39
40
            if (!$owner = $entity->getAclOwner()) {
41
                throw new \RuntimeException("Not found AclOwner for: " . get_class($entity));
42
            }
43
44
            $this->container->get('oneup_acl.manager')
45
                ->addObjectPermission($entity, MaskBuilder::MASK_OWNER, $owner)
46
            ;
47
48
            $entityManager->persist($entity);
49
            $metadata = $entityManager->getClassMetadata(get_class($entity));
50
            $unitOfWork->recomputeSingleEntityChangeSet($metadata, $entity);
51
        }
52
    }
53
}
54