Completed
Pull Request — master (#3)
by Pavel
04:11
created

EntityRepository::getManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: batanov.pavel
5
 * Date: 29.12.2015
6
 * Time: 11:11
7
 */
8
9
namespace Bankiru\Api\Doctrine;
10
11
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
12
use Bankiru\Api\Doctrine\Proxy\ApiCollection;
13
use Doctrine\Common\Persistence\ObjectRepository;
14
use ScayTrase\Api\Rpc\RpcClientInterface;
15
16
class EntityRepository implements ObjectRepository
17
{
18
    /** @var  ApiMetadata */
19
    private $metadata;
20
    /** @var EntityManager */
21
    private $manager;
22
23
    /**
24
     * EntityRepository constructor.
25
     *
26
     * @param EntityManager $manager
27
     * @param string        $className
28
     */
29 12
    public function __construct(EntityManager $manager, $className)
30
    {
31 12
        $this->manager  = $manager;
32 12
        $this->metadata = $this->manager->getClassMetadata($className);
33 12
    }
34
35
    /**
36
     * Finds an object by its primary key / identifier.
37
     *
38
     * @param mixed $id The identifier.
39
     *
40
     * @return object The object.
41
     */
42 9
    public function find($id)
43
    {
44 9
        return $this->manager->find($this->getClassName(), $id);
45
    }
46
47
    /**
48
     * Returns the class name of the object managed by the repository.
49
     *
50
     * @return string
51
     */
52 9
    public function getClassName()
53
    {
54 9
        return $this->metadata->getReflectionClass()->getName();
0 ignored issues
show
Bug introduced by
Consider using $this->metadata->getReflectionClass()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
55
    }
56
57
    /**
58
     * Finds all objects in the repository.
59
     *
60
     * @return array The objects.
61
     */
62
    public function findAll()
63
    {
64
        return $this->findBy([]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->findBy(array()); (Bankiru\Api\Doctrine\Proxy\ApiCollection) is incompatible with the return type declared by the interface Doctrine\Common\Persiste...jectRepository::findAll of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
65
    }
66
67
    /**
68
     * Finds objects by a set of criteria.
69
     *
70
     * Optionally sorting and limiting details can be passed. An implementation may throw
71
     * an UnexpectedValueException if certain values of the sorting or limiting details are
72
     * not supported.
73
     *
74
     * @param array      $criteria
75
     * @param array|null $orderBy
76
     * @param int|null   $limit
77
     * @param int|null   $offset
78
     *
79
     * @return array The objects.
80
     *
81
     * @throws \UnexpectedValueException
82
     */
83 3
    public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
84
    {
85 3
        return new ApiCollection($this->manager, $this->metadata, [$criteria, $orderBy, $limit, $offset]);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Bankiru\Api\...rBy, $limit, $offset)); (Bankiru\Api\Doctrine\Proxy\ApiCollection) is incompatible with the return type declared by the interface Doctrine\Common\Persiste...bjectRepository::findBy of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
86
    }
87
88
    /**
89
     * Finds a single object by a set of criteria.
90
     *
91
     * @param array $criteria The criteria.
92
     *
93
     * @return object The object.
94
     */
95
    public function findOneBy(array $criteria)
96
    {
97
        $objects = $this->findBy($criteria, [], 1);
98
99
        return array_shift($objects);
100
    }
101
102
    /**
103
     * @return RpcClientInterface
104
     */
105 1
    protected function getClient()
106
    {
107 1
        return $this->manager->getConfiguration()->getRegistry()->get($this->metadata->getClientName());
108
    }
109
110
    /**
111
     * @return ApiMetadata
112
     */
113 1
    protected function getMetadata()
114
    {
115 1
        return $this->metadata;
116
    }
117
118
    /**
119
     * @return EntityManager
120
     */
121
    public function getManager()
122
    {
123
        return $this->manager;
124
    }
125
}
126