Completed
Push — develop ( b92fd0...28494b )
by
unknown
23:27 queued 12:02
created

AbstractRepository::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Core\Repository;
4
5
use Core\Entity\EntityInterface;
6
use \Doctrine\ODM\MongoDB as ODM;
7
use Zend\ServiceManager\ServiceLocatorInterface;
8
9
abstract class AbstractRepository extends ODM\DocumentRepository implements RepositoryInterface
10
{
11
12
    protected $entityPrototype;
13
14
    /**
15
     * @param ODM\DocumentManager       $dm
16
     * @param ODM\UnitOfWork            $uow
17
     * @param ODM\Mapping\ClassMetadata $class
18
     */
19
    public function __construct(ODM\DocumentManager $dm, ODM\UnitOfWork $uow, ODM\Mapping\ClassMetadata $class)
20
    {
21
        parent::__construct($dm, $uow, $class);
22
        $eventArgs = new DoctrineMongoODM\Event\EventArgs(
23
            array(
24
            'repository' => $this
25
            )
26
        );
27
        $dm->getEventManager()->dispatchEvent(DoctrineMongoODM\Event\RepositoryEventsSubscriber::postConstruct, $eventArgs);
28
    }
29
30
    /**
31
     * @param ServiceLocatorInterface $serviceLocator
32
     */
33
    public function init(ServiceLocatorInterface $serviceLocator)
34
    {
35
        
36
    }
37
38
    /**
39
     * @param $name
40
     *
41
     * @return mixed
42
     */
43
    public function getService($name)
44
    {
45
        return $this->dm->getConfiguration()->getServiceLocator()->get($name);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Doctrine\ODM\MongoDB\Configuration as the method getServiceLocator() does only exist in the following sub-classes of Doctrine\ODM\MongoDB\Configuration: Core\Repository\Doctrine...catorAwareConfiguration. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
46
    }
47
48
    /**
49
     * @param EntityInterface $entity
50
     *
51
     * @return $this
52
     */
53
    public function setEntityPrototype(EntityInterface $entity)
54
    {
55
        $this->entityPrototype = $entity;
56
        return $this;
57
    }
58
59
    /**
60
     * @param array $data
0 ignored issues
show
Documentation introduced by
Should the type for parameter $data not be null|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
61
     *
62
     * @return mixed
63
     */
64
    public function create(array $data = null, $persist = false)
65
    {
66
        if (null === $this->entityPrototype) {
67
            throw new \RuntimeException('Could not create an entity. No prototype is set!');
68
        }
69
70
        $entity = clone $this->entityPrototype;
71
        
72
        if (null !== $data) {
73
            foreach ($data as $property => $value) {
74
                $setter = "set$property";
75
76
                if (method_exists($entity, $setter)) {
77
                    $entity->$setter($value);
78
                }
79
            }
80
        }
81
82
        if ($persist) {
83
            $this->dm->persist($entity);
84
        }
85
86
        return $entity;
87
    }
88
89
    /**
90
     * @param $entity
91
     * @throws \InvalidArgumentException
92
     * @return self
93
     */
94
    public function store($entity)
95
    {
96
        $this->checkEntityType($entity);
97
        $this->dm->persist($entity);
98
        $this->dm->flush($entity);
99
100
        return $this;
101
    }
102
103
    public function remove($entity)
104
    {
105
        $this->checkEntityType($entity);
106
        $this->dm->remove($entity);
107
108
        return $this;
109
    }
110
111
    protected function checkEntityType($entity)
112
    {
113
        if ( !($entity instanceOf $this->entityPrototype) ) {
114
            throw new \InvalidArgumentException(sprintf(
115
                                                    'Entity must be of type %s but recieved %s instead',
116
                                                    get_class($this->entityPrototype),
117
                                                    get_class($entity)
118
                                                ));
119
        }
120
121
    }
122
123
}
124