Completed
Push — master ( a9d0a0...c83be6 )
by Filipe
07:24
created

EntityListingService::setPagination()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Service\Entity;
11
12
use Slick\Common\Utils\CollectionInterface;
13
use Slick\Database\Sql\Select;
14
use Slick\Mvc\Service\Entity\QueryFilter\QueryFilterCollection;
15
use Slick\Mvc\Service\EntityServiceInterface;
16
use Slick\Mvc\Utils\Pagination;
17
use Slick\Orm\EntityInterface;
18
use Slick\Orm\Orm;
19
use Slick\Orm\Repository\QueryObject\QueryObjectInterface;
20
use Slick\Orm\RepositoryInterface;
21
22
/**
23
 * Entity Listing Service
24
 * 
25
 * @package Slick\Mvc\Service\Entity
26
 * @author  Filipe Silva <[email protected]>
27
 */
28
class EntityListingService extends AbstractEntityService implements
29
    EntityServiceInterface
30
{
31
32
    /**
33
     * @var Pagination
34
     */
35
    private $pagination;
36
37
    /**
38
     * @var QueryFilterInterface[]|QueryFilterCollectionInterface
39
     */
40
    private $filters;
41
42
    /**
43
     * @var RepositoryInterface
44
     */
45
    private $repository;
46
47
    /**
48
     * Entity Listing Service needs an entity or entity class name.
49
     * 
50
     * @param string|EntityInterface $className
51
     */
52 10
    public function __construct($className)
53
    {
54 10
        $method = 'setEntity';
55 10
        if (!$className instanceof EntityInterface) {
56 10
            $method = 'setEntityClass';
57 5
        }
58 10
        $this->$method($className);
59 10
    }
60
61
    /**
62
     * Get a paginated list of entities
63
     * 
64
     * @return \Slick\Orm\Entity\EntityCollection
65
     */
66 2
    public function getList()
67
    {
68
        /** @var Select|QueryObjectInterface $query */
69 2
        $query = $this->getRepository()->find();
70 2
        $this->getFilters()->apply($query);
0 ignored issues
show
Bug introduced by
It seems like $query can also be of type object<Slick\Database\Sql\Select>; however, Slick\Mvc\Service\Entity...ctionInterface::apply() does only seem to accept object<Slick\Orm\Reposit...t\QueryObjectInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
71 2
        $this->getPagination()->setTotal($query->count());
72 2
        $query->limit(
0 ignored issues
show
Bug introduced by
The method limit does only exist in Slick\Database\Sql\Select, but not in Slick\Orm\Repository\Que...ct\QueryObjectInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73 2
            $this->getPagination()->offset,
74 2
            $this->getPagination()->rowsPerPage
75 1
        );
76 2
        return $query->all();
77
    }
78
79
    /**
80
     * Gets the pagination object
81
     * 
82
     * @return Pagination
83
     */
84 4
    public function getPagination()
85
    {
86 4
        if (null == $this->pagination) {
87 4
            $this->setPagination(new Pagination());
88 2
        }
89 4
        return $this->pagination;
90
    }
91
92
    /**
93
     * Set pagination
94
     * 
95
     * @param Pagination $pagination
96
     * 
97
     * @return EntityListingService
98
     */
99 4
    public function setPagination($pagination)
100
    {
101 4
        $this->pagination = $pagination;
102 4
        return $this;
103
    }
104
105
    /**
106
     * Get query filters collection
107
     * 
108
     * @return CollectionInterface|QueryFilterCollectionInterface
109
     */
110 4
    public function getFilters()
111
    {
112 4
        if (null == $this->filters) {
113 4
            $this->setFilters(new QueryFilterCollection());
114 2
        }
115 4
        return $this->filters;
116
    }
117
118
    /**
119
     * Set filters collection
120
     * 
121
     * @param QueryFilterCollectionInterface $filters
122
     * 
123
     * @return EntityListingService
124
     */
125 4
    public function setFilters(QueryFilterCollectionInterface $filters)
126
    {
127 4
        $this->filters = $filters;
128 4
        return $this;
129
    }
130
131
    /**
132
     * Get entity query
133
     * 
134
     * @return RepositoryInterface
135
     */
136 6
    public function getRepository()
137
    {
138 6
        if (null == $this->repository) {
139 4
            $this->setRepository(
140 4
                Orm::getRepository($this->getEntityClassName())
141 2
            );
142 2
        }
143 6
        return $this->repository;
144
    }
145
146
    /**
147
     * Set entity repository
148
     * 
149
     * @param RepositoryInterface $repository
150
     * 
151
     * @return EntityListingService
152
     */
153 6
    public function setRepository(RepositoryInterface $repository)
154
    {
155 6
        $this->repository = $repository;
156 6
        return $this;
157
    }
158
159
160
    
161
}