Completed
Push — master ( 98c39c...c8155d )
by Pavel
16:50
created

CreateController::createAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
crap 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 10
    public function __construct(
37
        EntityProcessorInterface $processor,
38
        ObjectManager $manager,
39
        EntityFactoryInterface $factory,
40
        EventDispatcherInterface $evm = null
41
    ) {
42 10
        $this->manager   = $manager;
43 10
        $this->factory   = $factory;
44 10
        $this->processor = $processor;
45 10
        $this->evm       = $evm ?: new EventDispatcher();
46 10
    }
47
48
    /**
49
     * @param mixed $data
50
     *
51
     * @return object
52
     * @throws EntityProcessingException
53
     */
54 10
    public function createAction($data)
55
    {
56 10
        $this->evm->dispatch(CrudEvents::CREATE, new Event());
57
58 10
        $object = $this->factory->createEntity($data);
59 10
        $entity = $this->processor->updateEntity($object, $data);
60
61 10
        $this->evm->dispatch(CrudEvents::READ, new CollectionCrudEvent([$entity]));
62 10
        $this->manager->persist($entity);
63 10
        $this->manager->flush();
64
65 10
        return $entity;
66
    }
67
}
68