Completed
Pull Request — develop (#311)
by
unknown
15:42
created

RepositoryEventsSubscriber::postLoad()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Core\Repository\DoctrineMongoODM\Event;
11
12
use Doctrine\Common\EventSubscriber;
13
use Zend\ServiceManager\ServiceLocatorInterface;
14
use Core\Repository\RepositoryInterface;
15
use Core\Entity\AttachableEntityInterface;
16
use Core\Entity\AttachableEntityManager;
17
use Doctrine\ODM\MongoDB\Events;
18
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
19
20
class RepositoryEventsSubscriber implements EventSubscriber
21
{
22
    const postConstruct = 'postRepositoryConstruct';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected POSTCONSTRUCT).
Loading history...
23
    
24
    /**
25
     * @var ServiceLocatorInterface
26
     */
27
    protected $services;
28
    
29
    /**
30
     * @var AttachableEntityManager
31
     */
32
    protected $attachableEntityManagerPrototype;
33
    
34
    /**
35
     * @param ServiceLocatorInterface $serviceLocator
36
     */
37
    public function __construct(ServiceLocatorInterface $serviceLocator)
38
    {
39
        $this->services = $serviceLocator;
40
    }
41
    
42
    public function postRepositoryConstruct($eventArgs)
43
    {
44
        $repo = $eventArgs->getRepository();
45
        if ($repo instanceof RepositoryInterface) {
46
            $documentName = $repo->getDocumentName();
47
            $entity = new $documentName();
48
            
49
            if ($entity instanceof AttachableEntityInterface) {
50
                $this->injectAttachableEntityManager($entity);
51
            }
52
            
53
            $repo->setEntityPrototype($entity);
54
            $repo->init($this->services);
55
        }
56
    }
57
    
58
    /**
59
     * @param LifecycleEventArgs $eventArgs
60
     * @since 0.28
61
     */
62
    public function postLoad(LifecycleEventArgs $eventArgs)
63
    {
64
        $entity = $eventArgs->getDocument();
65
        
66
        if ($entity instanceof AttachableEntityInterface) {
67
            $this->injectAttachableEntityManager($entity);
68
        }
69
    }
70
    
71
    /**
72
     * @see \Doctrine\Common\EventSubscriber::getSubscribedEvents()
73
     */
74
    public function getSubscribedEvents()
75
    {
76
        return array(self::postConstruct, Events::postLoad);
77
    }
78
    
79
    /**
80
     * @param ServiceLocatorInterface $serviceLocator
81
     * @return RepositoryEventsSubscriber
82
     */
83
    public static function factory(ServiceLocatorInterface $serviceLocator)
84
    {
85
        return new static($serviceLocator);
86
    }
87
    
88
    /**
89
     * @param AttachableEntityInterface $entity
90
     * @since 0.28
91
     */
92
    protected function injectAttachableEntityManager(AttachableEntityInterface $entity)
93
    {
94
        if (! isset($this->attachableEntityManagerPrototype)) {
95
            $this->attachableEntityManagerPrototype = new AttachableEntityManager($this->services->get('repositories'));
0 ignored issues
show
Documentation introduced by
$this->services->get('repositories') is of type object|array, but the function expects a object<Core\Repository\RepositoryService>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
        }
97
        
98
        $entity->setAttachableEntityManager(clone $this->attachableEntityManagerPrototype);
99
    }
100
}
101