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

UpdateController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 7
dl 0
loc 56
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A patchAction() 0 15 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Controller;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Doctrine\Common\Persistence\ObjectRepository;
7
use ScayTrase\Api\Cruds\EntityProcessorInterface;
8
use ScayTrase\Api\Cruds\Event\CollectionCrudEvent;
9
use ScayTrase\Api\Cruds\Event\CrudEvents;
10
use ScayTrase\Api\Cruds\Exception\EntityNotFoundException;
11
use Symfony\Component\EventDispatcher\EventDispatcher;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
final class UpdateController
15
{
16
    const ACTION = 'patchAction';
17
18
    /** @var  ObjectManager */
19
    private $manager;
20
    /** @var  EventDispatcherInterface */
21
    private $evm;
22
    /** @var  ObjectRepository */
23
    private $repository;
24
    /** @var  EntityProcessorInterface */
25
    private $processor;
26
27
    /**
28
     * UpdateController constructor.
29
     *
30
     * @param ObjectRepository         $repository
31
     * @param EntityProcessorInterface $processor
32
     * @param ObjectManager            $manager
33
     * @param EventDispatcherInterface $evm
34
     */
35 4
    public function __construct(
36
        ObjectRepository $repository,
37
        EntityProcessorInterface $processor,
38
        ObjectManager $manager,
39
        EventDispatcherInterface $evm = null
40
    ) {
41 4
        $this->manager    = $manager;
42 4
        $this->repository = $repository;
43 4
        $this->processor  = $processor;
44 4
        $this->evm        = $evm ?: new EventDispatcher();
45 4
    }
46
47
    /**
48
     * @param mixed $identifier
49
     * @param mixed $data
50
     *
51
     * @return object
52
     * @throws EntityNotFoundException
53
     */
54 4
    public function patchAction($identifier, $data)
55
    {
56 4
        $entity = $this->repository->find($identifier);
57
58 4
        if (!$entity) {
59
            throw EntityNotFoundException::byIdentifier($identifier);
60
        }
61
62 4
        $this->evm->dispatch(CrudEvents::PRE_UPDATE, new CollectionCrudEvent([$entity]));
63 4
        $entity = $this->processor->updateEntity($entity, $data);
64 4
        $this->evm->dispatch(CrudEvents::POST_UPDATE, new CollectionCrudEvent([$entity]));
65 4
        $this->manager->flush();
66
67 4
        return $entity;
68
    }
69
}
70