Test Failed
Push — master ( 0fcd0f...4e8aa2 )
by Pavel
04:49 queued 16s
created

ReadController::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 ReadController
15
{
16
    /** @var CriteriaConfiguratorInterface */
17
    private $filters = [];
18
    /** @var  EntityRepository */
19
    private $repository;
20
    /** @var  EventDispatcherInterface */
21
    private $evm;
22
23
    /**
24
     * ReadController constructor.
25
     *
26
     * @param EntityRepository                $repository
27
     * @param CriteriaConfiguratorInterface[] $filters
28
     * @param EventDispatcherInterface        $evm
29
     */
30
    public function __construct(
31
        EntityRepository $repository,
32
        array $filters,
33
        EventDispatcherInterface $evm = null
34
    ) {
35
        $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...
36
        $this->repository = $repository;
37
        $this->evm        = $evm ?: new EventDispatcher();
38
    }
39
40
    /**
41
     * Performs search of entities and returns them
42
     *
43
     * @param array $criteria
44
     * @param array $order
45
     * @param int   $limit
46
     * @param int   $offset
47
     *
48
     * @return object[]
49
     * @throws FilterException
50
     */
51
    public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0)
52
    {
53
        $builder = $this->repository->createQueryBuilder('e');
54
55
        $unknown = array_diff_key($criteria, $this->filters);
56
57
        if (count($unknown) > 0) {
58
            throw FilterException::unknown(array_keys($unknown));
59
        }
60
61
        foreach ($criteria as $filter => $item) {
62
            $this->filters[$filter]->configure($builder, $item);
63
        }
64
65
        foreach ($order as $key => $value) {
66
            $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...
67
        }
68
69
        $builder->setMaxResults($limit);
70
        $builder->setFirstResult($offset);
71
72
        $entities = $builder->getQuery()->getResult();
73
74
        $this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent($entities));
75
76
        return $entities;
77
    }
78
79
    /**
80
     * Returns the entity by given identifiers
81
     *
82
     * @param mixed $identifier
83
     *
84
     * @return null|object
85
     */
86
    public function getAction($identifier)
87
    {
88
        $entity = $this->repository->find($identifier);
89
90
        $this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent([$entity]));
91
92
        return $entity;
93
    }
94
}
95