Completed
Push — master ( 839f22...1b3a7e )
by Pavel
04:14
created

SearchController::findAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 4
crap 1
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 $configurator;
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 $configurator
32
     * @param EventDispatcherInterface      $evm
33
     */
34 4
    public function __construct(
35
        $fqcn,
36
        Selectable $repository,
37
        CriteriaConfiguratorInterface $configurator,
38
        EventDispatcherInterface $evm
39
    ) {
40 4
        $this->fqcn         = $fqcn;
41 4
        $this->configurator = $configurator;
42 4
        $this->repository   = $repository;
43 4
        $this->evm          = $evm;
44 4
    }
45
46
    /**
47
     * Performs search of entities and returns them
48
     *
49
     * @param array $criteria
50
     * @param array $order
51
     * @param int   $limit
52
     * @param int   $offset
53
     *
54
     * @return object[]
55
     * @throws CriteriaConfigurationException
56
     */
57 4
    public function findAction(array $criteria, array $order = [], $limit = 10, $offset = 0)
58
    {
59 4
        $queryCriteria = new Criteria(null, $order, $offset, $limit);
60
61 4
        $this->configurator->configure($this->fqcn, $queryCriteria, $criteria);
62 4
        $entities = $this->repository->matching($queryCriteria);
63
64 4
        $this->evm->dispatch(CrudEvents::READ, new CollectionCrudEvent($entities));
65
66 4
        return $entities;
67
    }
68
}
69