Passed
Push — master ( 61ebc4...c33ad8 )
by Pavel
17:39
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 ScayTrase\Api\Cruds\Event\CrudEvents;
7
use ScayTrase\Api\Cruds\Event\EntityCrudEvent;
8
use Symfony\Component\EventDispatcher\EventDispatcher;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
final class ReadController
12
{
13
    const ACTION = 'getAction';
14
15
    /** @var  EntityRepository */
16
    private $repository;
17
    /** @var  EventDispatcherInterface */
18
    private $evm;
19
20
    /**
21
     * ReadController constructor.
22
     *
23
     * @param EntityRepository         $repository
24
     * @param EventDispatcherInterface $evm
25
     */
26
    public function __construct(EntityRepository $repository, EventDispatcherInterface $evm = null)
27
    {
28
        $this->repository = $repository;
29
        $this->evm        = $evm ?: new EventDispatcher();
30
    }
31
32
    /**
33
     * Returns the entity by given identifiers
34
     *
35
     * @param mixed $identifier
36
     *
37
     * @return null|object
38
     */
39
    public function getAction($identifier)
40
    {
41
        $entity = $this->repository->find($identifier);
42
43
        $this->evm->dispatch(CrudEvents::READ, new EntityCrudEvent([$entity]));
44
45
        return $entity;
46
    }
47
}
48