Passed
Push — master ( 61ebc4...c33ad8 )
by Pavel
17:39
created

SearchController::findAction()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.439
cc 5
eloc 14
nc 5
nop 4
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Controller;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\Query\Expr;
7
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface;
8
use ScayTrase\Api\Cruds\Event\CrudEvents;
9
use ScayTrase\Api\Cruds\Event\EntityCrudEvent;
10
use ScayTrase\Api\Cruds\Exception\FilterException;
11
use Symfony\Component\EventDispatcher\EventDispatcher;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
final class SearchController
15
{
16
    const ACTION = 'findAction';
17
18
    /** @var CriteriaConfiguratorInterface */
19
    private $filters = [];
20
    /** @var  EntityRepository */
21
    private $repository;
22
    /** @var  EventDispatcherInterface */
23
    private $evm;
24
25
    /**
26
     * ReadController constructor.
27
     *
28
     * @param EntityRepository                $repository
29
     * @param CriteriaConfiguratorInterface[] $filters
30
     * @param EventDispatcherInterface        $evm
31
     */
32
    public function __construct(
33
        EntityRepository $repository,
34
        array $filters,
35
        EventDispatcherInterface $evm = null
36
    ) {
37
        $this->filters    = $filters;
0 ignored issues
show
Documentation Bug introduced by
It seems like $filters of type array<integer,object<Sca...ConfiguratorInterface>> is incompatible with the declared type object<ScayTrase\Api\Cru...aConfiguratorInterface> of property $filters.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
        $this->repository = $repository;
39
        $this->evm        = $evm ?: new EventDispatcher();
40
    }
41
42
    /**
43
     * Performs search of entities and returns them
44
     *
45
     * @param array $criteria
46
     * @param array $order
47
     * @param int   $limit
48
     * @param int   $offset
49
     *
50
     * @return object[]
51
     * @throws FilterException
52
     */
53
    public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0)
54
    {
55
        $builder = $this->repository->createQueryBuilder('e');
56
57
        $unknown = array_diff_key($criteria, $this->filters);
58
59
        if (count($unknown) > 0) {
60
            throw FilterException::unknown(array_keys($unknown));
61
        }
62
63
        foreach ($criteria as $filter => $item) {
64
            $this->filters[$filter]->configure($builder, $item);
65
        }
66
67
        foreach ($order as $key => $value) {
68
            $builder->addOrderBy(new Expr\Literal($key), strtolower($value) === 'asc' ? 'ASC' : 'DESC');
0 ignored issues
show
Documentation introduced by
$key is of type integer|string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
69
        }
70
71
        $builder->setMaxResults($limit);
72
        $builder->setFirstResult($offset);
73
74
        $entities = $builder->getQuery()->getResult();
75
76
        $this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent($entities));
77
78
        return $entities;
79
    }
80
}
81