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\CrudEvents; |
9
|
|
|
use ScayTrase\Api\Cruds\Exception\CriteriaConfigurationException; |
10
|
|
|
use Symfony\Component\EventDispatcher\Event; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
|
13
|
|
|
final class CountController |
14
|
|
|
{ |
15
|
|
|
const ACTION = 'countAction'; |
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
|
|
|
* CountController constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $fqcn |
30
|
|
|
* @param Selectable $repository |
31
|
|
|
* @param CriteriaConfiguratorInterface $configurator |
32
|
|
|
* @param EventDispatcherInterface $evm |
33
|
|
|
*/ |
34
|
5 |
View Code Duplication |
public function __construct( |
|
|
|
|
35
|
|
|
$fqcn, |
36
|
|
|
Selectable $repository, |
37
|
|
|
CriteriaConfiguratorInterface $configurator, |
38
|
|
|
EventDispatcherInterface $evm |
39
|
|
|
) { |
40
|
5 |
|
$this->fqcn = $fqcn; |
41
|
5 |
|
$this->configurator = $configurator; |
42
|
5 |
|
$this->repository = $repository; |
43
|
5 |
|
$this->evm = $evm; |
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Performs counting search of entities and returns count |
48
|
|
|
* |
49
|
|
|
* @param array $criteria |
50
|
|
|
* |
51
|
|
|
* @return integer |
52
|
|
|
* @throws CriteriaConfigurationException |
53
|
|
|
*/ |
54
|
5 |
|
public function countAction(array $criteria) |
55
|
|
|
{ |
56
|
5 |
|
$queryCriteria = new Criteria(); |
57
|
|
|
|
58
|
5 |
|
$this->configurator->configure($this->fqcn, $queryCriteria, $criteria); |
59
|
5 |
|
$count = $this->repository->matching($queryCriteria)->count(); |
60
|
|
|
|
61
|
5 |
|
$this->evm->dispatch(CrudEvents::COUNT, new Event()); |
62
|
|
|
|
63
|
5 |
|
return $count; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.