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
|
|
|
|