Completed
Push — master ( 4ff9cc...589f5d )
by Pavel
07:24
created

SearchController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 53
rs 10
ccs 9
cts 10
cp 0.9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A findAction() 0 16 3
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\Exception\CriteriaConfigurationException;
9
10
final class SearchController
11
{
12
    const ACTION = 'findAction';
13
14
    /** @var string */
15
    private $fqcn;
16
    /** @var CriteriaConfiguratorInterface */
17
    private $filters = [];
18
    /** @var  Selectable */
19
    private $repository;
20
21
    /**
22
     * ReadController constructor.
23
     *
24
     * @param string                          $fqcn
25
     * @param Selectable                      $repository
26
     * @param CriteriaConfiguratorInterface[] $filters
27
     */
28
    public function __construct($fqcn, Selectable $repository, array $filters)
29
    {
30
        $this->fqcn       = $fqcn;
31
        $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...
32 2
        $this->repository = $repository;
33
    }
34
35
    /**
36
     * Performs search of entities and returns them
37 2
     *
38 2
     * @param array $criteria
39 2
     * @param array $order
40 2
     * @param int   $limit
41
     * @param int   $offset
42
     *
43
     * @return object[]
44
     * @throws CriteriaConfigurationException
45
     */
46
    public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0)
47
    {
48
        $queryCriteria = new Criteria(null, $order, $offset, $limit);
49
50
        $unknown = array_diff_key($criteria, $this->filters);
51
52
        if (count($unknown) > 0) {
53 2
            throw CriteriaConfigurationException::unknown(array_keys($unknown));
54
        }
55 2
56
        foreach ($criteria as $filter => $item) {
57 2
            $this->filters[$filter]->configure($this->fqcn, $queryCriteria, $item);
58
        }
59 2
60
        return $this->repository->matching($queryCriteria);
61
    }
62
}
63