Completed
Push — master ( 138e01...b86448 )
by Julián
08:06
created

MongoDBRepository::findBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
3
/*
4
 * doctrine-mongodb-odm-repositories (https://github.com/juliangut/doctrine-mongodb-odm-repositories).
5
 * Doctrine2 MongoDB ODM utility entity repositories.
6
 *
7
 * @license MIT
8
 * @link https://github.com/juliangut/doctrine-mongodb-odm-repositories
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Jgut\Doctrine\Repository\MongoDB\ODM;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Util\ClassUtils;
18
use Doctrine\ODM\MongoDB\Cursor;
19
use Doctrine\ODM\MongoDB\DocumentManager;
20
use Doctrine\ODM\MongoDB\DocumentRepository;
21
use Jgut\Doctrine\Repository\EventsTrait;
22
use Jgut\Doctrine\Repository\FiltersTrait;
23
use Jgut\Doctrine\Repository\PaginatorTrait;
24
use Jgut\Doctrine\Repository\Repository;
25
use Jgut\Doctrine\Repository\RepositoryTrait;
26
use Zend\Paginator\Paginator;
27
28
/**
29
 * MongoDB document repository.
30
 */
31
class MongoDBRepository extends DocumentRepository implements Repository
32
{
33
    use RepositoryTrait;
34
    use EventsTrait;
35
    use FiltersTrait;
36
    use PaginatorTrait;
37
38
    /**
39
     * Class name.
40
     *
41
     * @var string
42
     */
43
    protected $className;
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getClassName(): string
49
    {
50
        if ($this->className === null) {
51
            $this->className = ClassUtils::getRealClass($this->getDocumentName());
52
        }
53
54
        return $this->className;
55
    }
56
57
    /**
58
     * Finds documents by a set of criteria.
59
     *
60
     * @param array    $criteria
61
     * @param array    $sort
0 ignored issues
show
Documentation introduced by
Should the type for parameter $sort not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

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. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

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

Loading history...
62
     * @param int|null $limit
63
     * @param int|null $skip
64
     *
65
     * @return ArrayCollection
66
     *
67
     * @codeCoverageIgnore
68
     */
69
    public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null)
70
    {
71
        return new ArrayCollection(parent::findBy($criteria, $sort, $limit, $skip));
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Doctrine\Com...$sort, $limit, $skip)); (Doctrine\Common\Collections\ArrayCollection) 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...
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function getFilterCollection()
78
    {
79
        return $this->getManager()->getFilterCollection();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    protected function getManager(): DocumentManager
86
    {
87
        return $this->getDocumentManager();
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * @param array      $criteria
94
     * @param array|null $orderBy
95
     * @param int        $itemsPerPage
96
     *
97
     * @return \Zend\Paginator\Paginator
98
     */
99
    public function findPaginatedBy($criteria, array $orderBy = null, int $itemsPerPage = 10): Paginator
100
    {
101
        return $this->paginate($this->getDocumentPersister()->loadAll($criteria, $orderBy), $itemsPerPage);
102
    }
103
104
    /**
105
     * Paginate MongoDB cursor.
106
     *
107
     * @param Cursor $cursor
108
     * @param int    $itemsPerPage
109
     *
110
     * @return Paginator
111
     */
112
    protected function paginate(Cursor $cursor, int $itemsPerPage = 10): Paginator
113
    {
114
        return $this->getPaginator(new MongoDBPaginatorAdapter($cursor), $itemsPerPage);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     *
120
     * @param array $criteria
121
     *
122
     * @return int
123
     */
124
    public function countBy($criteria): int
125
    {
126
        return $this->getDocumentPersister()->loadAll($criteria)->count();
127
    }
128
}
129