Completed
Push — develop ( b72595...6a420f )
by
unknown
10s
created

RepositoryEventsSubscriber::postRepositoryCreate()   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
use Core\Repository\DoctrineMongoODM\Event\EventArgs;
20
21
class RepositoryEventsSubscriber implements EventSubscriber
22
{
23
    const postConstruct = 'postRepositoryConstruct';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected POSTCONSTRUCT).
Loading history...
24
    const postCreate = 'postRepositoryCreate';
0 ignored issues
show
Coding Style introduced by
This class constant is not uppercase (expected POSTCREATE).
Loading history...
25
    
26
    /**
27
     * @var ServiceLocatorInterface
28
     */
29
    protected $services;
30
    
31
    /**
32
     * @var AttachableEntityManager
33
     */
34
    protected $attachableEntityManagerPrototype;
35
    
36
    /**
37
     * @param ServiceLocatorInterface $serviceLocator
38
     */
39
    public function __construct(ServiceLocatorInterface $serviceLocator)
40
    {
41
        $this->services = $serviceLocator;
42
    }
43
    
44
    /**
45
     * @param EventArgs $eventArgs
46
     */
47
    public function postRepositoryConstruct(EventArgs $eventArgs)
48
    {
49
        $repo = $eventArgs->get('repository');
50
        
51
        if ($repo instanceof RepositoryInterface) {
52
            $documentName = $repo->getDocumentName();
53
            $entity = new $documentName();
54
            $repo->setEntityPrototype($entity);
55
            $repo->init($this->services);
56
        }
57
    }
58
    
59
    /**
60
     * @param EventArgs $eventArgs
61
     * @since 0.28
62
     */
63
    public function postRepositoryCreate(EventArgs $eventArgs)
64
    {
65
        $entity = $eventArgs->get('entity');
66
        
67
        if ($entity instanceof AttachableEntityInterface) {
68
            $this->injectAttachableEntityManager($entity);
69
        }
70
    }
71
    
72
    /**
73
     * @param LifecycleEventArgs $eventArgs
74
     * @since 0.28
75
     */
76
    public function postLoad(LifecycleEventArgs $eventArgs)
77
    {
78
        $entity = $eventArgs->getDocument();
79
        
80
        if ($entity instanceof AttachableEntityInterface) {
81
            $this->injectAttachableEntityManager($entity);
82
        }
83
    }
84
    
85
    
86
    /**
87
     * @see \Doctrine\Common\EventSubscriber::getSubscribedEvents()
88
     */
89
    public function getSubscribedEvents()
90
    {
91
        return [
92
            self::postConstruct,
93
            self::postCreate,
94
            Events::postLoad
95
        ];
96
    }
97
    
98
    /**
99
     * @param ServiceLocatorInterface $serviceLocator
100
     * @return RepositoryEventsSubscriber
101
     */
102
    public static function factory(ServiceLocatorInterface $serviceLocator)
103
    {
104
        return new static($serviceLocator);
105
    }
106
    
107
    /**
108
     * @param AttachableEntityInterface $entity
109
     * @since 0.28
110
     */
111
    protected function injectAttachableEntityManager(AttachableEntityInterface $entity)
112
    {
113
        if (! isset($this->attachableEntityManagerPrototype)) {
114
            $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...
115
        }
116
        
117
        $entity->setAttachableEntityManager(clone $this->attachableEntityManagerPrototype);
118
    }
119
}
120