Completed
Push — master ( 06e52a...98c39c )
by Pavel
13:39 queued 10:37
created

CreateController::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 4
crap 6
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
     * @param mixed $data
50
     *
51
     * @return object
52
     * @throws EntityProcessingException
53
     */
54
    public function createAction($data)
55
    {
56
        $this->evm->dispatch(CrudEvents::CREATE, new Event());
57
58
        $object = $this->factory->createEntity($data);
59
        $entity = $this->processor->updateEntity($object, $data);
60
61
        $this->evm->dispatch(CrudEvents::READ, new CollectionCrudEvent([$entity]));
62
        $this->manager->persist($entity);
63
        $this->manager->flush();
64
65
        return $entity;
66
    }
67
}
68