Completed
Push — master ( c8924f...ae2ef6 )
by Filipe
02:36
created

EntityListingService::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * @var string
49
     */
50
    private $order;
51
52
    /**
53
     * Entity Listing Service needs an entity or entity class name.
54
     * 
55
     * @param string|EntityInterface $className
56
     */
57 10
    public function __construct($className)
58
    {
59 10
        $method = 'setEntity';
60 10
        if (!$className instanceof EntityInterface) {
61 10
            $method = 'setEntityClass';
62 10
        }
63 10
        $this->$method($className);
64 10
    }
65
66
    /**
67
     * Get a paginated list of entities
68
     * 
69
     * @return \Slick\Orm\Entity\EntityCollection
70
     */
71 2
    public function getList()
72
    {
73
        /** @var Select|QueryObjectInterface $query */
74 2
        $query = $this->getRepository()->find();
75 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...
76 2
        $query->order($this->getOrder());
0 ignored issues
show
Bug introduced by
The method order 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...
77 2
        $this->getPagination()->setTotal($query->count());
78 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...
79 2
            $this->getPagination()->offset,
80 2
            $this->getPagination()->rowsPerPage
81 2
        );
82 2
        return $query->all();
83
    }
84
85
    /**
86
     * Gets the pagination object
87
     * 
88
     * @return Pagination
89
     */
90 4
    public function getPagination()
91
    {
92 4
        if (null == $this->pagination) {
93 4
            $this->setPagination(new Pagination());
94 4
        }
95 4
        return $this->pagination;
96
    }
97
98
    /**
99
     * Set pagination
100
     * 
101
     * @param Pagination $pagination
102
     * 
103
     * @return EntityListingService
104
     */
105 4
    public function setPagination($pagination)
106
    {
107 4
        $this->pagination = $pagination;
108 4
        return $this;
109
    }
110
111
    /**
112
     * Get query filters collection
113
     * 
114
     * @return CollectionInterface|QueryFilterCollectionInterface
115
     */
116 4
    public function getFilters()
117
    {
118 4
        if (null == $this->filters) {
119 4
            $this->setFilters(new QueryFilterCollection());
120 4
        }
121 4
        return $this->filters;
122
    }
123
124
    /**
125
     * Set filters collection
126
     * 
127
     * @param QueryFilterCollectionInterface $filters
128
     * 
129
     * @return EntityListingService
130
     */
131 4
    public function setFilters(QueryFilterCollectionInterface $filters)
132
    {
133 4
        $this->filters = $filters;
134 4
        return $this;
135
    }
136
137
    /**
138
     * Get entity query
139
     * 
140
     * @return RepositoryInterface
141
     */
142 4
    public function getRepository()
143
    {
144 4
        if (null == $this->repository) {
145 2
            $this->setRepository(
146 2
                Orm::getRepository($this->getEntityClassName())
147 2
            );
148 2
        }
149 4
        return $this->repository;
150
    }
151
152
    /**
153
     * Set entity repository
154
     * 
155
     * @param RepositoryInterface $repository
156
     * 
157
     * @return EntityListingService
158
     */
159 4
    public function setRepository(RepositoryInterface $repository)
160
    {
161 4
        $this->repository = $repository;
162 4
        return $this;
163
    }
164
165
    /**
166
     * Get the order by clause
167
     *
168
     * @return string
169
     */
170 2
    public function getOrder()
171
    {
172 2
        return $this->order;
173
    }
174
175
    /**
176
     * Set the order by clause
177
     *
178
     * @param string $order
179
     *
180
     * @return EntityListingService
181
     */
182
    public function setOrder($order)
183
    {
184
        $this->order = $order;
185
        return $this;
186
    }
187
    
188
}