Completed
Push — master ( 670f0c...07e238 )
by Pavel
12:30
created

ReadController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 42
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A getAction() 0 12 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Controller;
4
5
use Doctrine\Common\Persistence\ObjectRepository;
6
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent;
7
use ScayTrase\Api\Cruds\Event\CrudEvents;
8
use ScayTrase\Api\Cruds\Exception\EntityNotFoundException;
9
use Symfony\Component\EventDispatcher\EventDispatcher;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
final class ReadController
13
{
14
    const ACTION = 'getAction';
15
16
    /** @var  ObjectRepository */
17
    private $repository;
18
    /** @var  EventDispatcherInterface */
19
    private $evm;
20
21
    /**
22
     * ReadController constructor.
23
     *
24
     * @param ObjectRepository         $repository
25
     * @param EventDispatcherInterface $evm
26
     */
27 4
    public function __construct(ObjectRepository $repository, EventDispatcherInterface $evm = null)
28
    {
29 4
        $this->repository = $repository;
30 4
        $this->evm        = $evm ?: new EventDispatcher();
31 4
    }
32
33
    /**
34
     * Returns the entity by given identifiers
35
     *
36
     * @param mixed $identifier
37
     *
38
     * @return object
39
     * @throws EntityNotFoundException
40
     */
41 4
    public function getAction($identifier)
42
    {
43 4
        $entity = $this->repository->find($identifier);
44
45 4
        if (!$entity) {
46
            throw EntityNotFoundException::byIdentifier($identifier);
47
        }
48
49 4
        $this->evm->dispatch(CrudEvents::READ, new CollectionCrudEvent([$entity]));
50
51 4
        return $entity;
52
    }
53
}
54