Completed
Push — master ( f78604...98e267 )
by Pavel
04:30
created

SearchController::findAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 9.4285
cc 3
eloc 10
nc 3
nop 4
crap 3.0067
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Controller;
4
5
use Doctrine\Common\Collections\Criteria;
6
use Doctrine\Common\Collections\Selectable;
7
use ScayTrase\Api\Cruds\CriteriaConfiguratorInterface;
8
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent;
9
use ScayTrase\Api\Cruds\Event\CrudEvents;
10
use ScayTrase\Api\Cruds\Exception\CriteriaConfigurationException;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
final class SearchController
14
{
15
    const ACTION = 'findAction';
16
17
    /** @var string */
18
    private $fqcn;
19
    /** @var CriteriaConfiguratorInterface */
20
    private $filters = [];
21
    /** @var  Selectable */
22
    private $repository;
23
    /** @var EventDispatcherInterface */
24
    private $evm;
25
26
    /**
27
     * ReadController constructor.
28
     *
29
     * @param string                          $fqcn
30
     * @param Selectable                      $repository
31
     * @param CriteriaConfiguratorInterface[] $filters
32
     * @param EventDispatcherInterface        $evm
33
     */
34 4
    public function __construct($fqcn, Selectable $repository, array $filters, EventDispatcherInterface $evm)
35
    {
36 4
        $this->fqcn       = $fqcn;
37 4
        $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 4
        $this->repository = $repository;
39 4
        $this->evm        = $evm;
40 4
    }
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 CriteriaConfigurationException
52
     */
53 4
    public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0)
54
    {
55 4
        $queryCriteria = new Criteria(null, $order, $offset, $limit);
56
57 4
        $unknown = array_diff_key($criteria, $this->filters);
58
59 4
        if (count($unknown) > 0) {
60
            throw CriteriaConfigurationException::unknown(array_keys($unknown));
61
        }
62
63 4
        foreach ($criteria as $filter => $item) {
64 2
            $this->filters[$filter]->configure($this->fqcn, $queryCriteria, $item);
65 4
        }
66
67 4
        $entities = $this->repository->matching($queryCriteria);
68
69 4
        $this->evm->dispatch(CrudEvents::READ, new CollectionCrudEvent($entities));
0 ignored issues
show
Documentation introduced by
$entities is of type object<Doctrine\Common\Collections\Collection>, 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...
70
71 4
        return $entities;
72
    }
73
}
74