Passed
Pull Request — master (#21)
by Pavel
06:33
created

CreateController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 7
dl 0
loc 54
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

2 Methods

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