|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Darkilliant\ImportBundle\Step; |
|
4
|
|
|
|
|
5
|
|
|
use Darkilliant\ImportBundle\Resolver\EntityResolver; |
|
6
|
|
|
use Darkilliant\ImportBundle\Serializer\Serializer; |
|
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
8
|
|
|
use Darkilliant\ProcessBundle\State\ProcessState; |
|
9
|
|
|
use Darkilliant\ProcessBundle\Step\AbstractConfigurableStep; |
|
10
|
|
|
|
|
11
|
|
|
class LoadObjectNormalizedStep extends AbstractConfigurableStep |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var Serializer */ |
|
14
|
|
|
private $denormalizer; |
|
15
|
|
|
|
|
16
|
|
|
/** @var EntityResolver */ |
|
17
|
|
|
private $entityResolver; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(Serializer $denormalizer, EntityResolver $entityResolver) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->denormalizer = $denormalizer; |
|
22
|
|
|
$this->entityResolver = $entityResolver; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function configureOptionResolver(OptionsResolver $resolver): OptionsResolver |
|
26
|
|
|
{ |
|
27
|
|
|
$resolver |
|
28
|
|
|
->setRequired(['entity_class', 'serializer']); |
|
29
|
|
|
$resolver->setDefault('serializer', 'auto'); |
|
30
|
|
|
|
|
31
|
|
|
return parent::configureOptionResolver($resolver); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param ProcessState $state |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \Exception |
|
38
|
|
|
*/ |
|
39
|
|
|
public function execute(ProcessState $state) |
|
40
|
|
|
{ |
|
41
|
|
|
$object = $this->denormalizer->denormalize( |
|
42
|
|
|
$state->getData(), |
|
43
|
|
|
$state->getOptions()['entity_class'], |
|
44
|
|
|
$state->getOptions()['serializer'] |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$state->setContext('class', get_class($object)); |
|
48
|
|
|
$state->setContext('id', |
|
49
|
|
|
(method_exists($object, 'getId')) |
|
50
|
|
|
? $object->getId() |
|
51
|
|
|
: null |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
$state->info('create object'); |
|
55
|
|
|
|
|
56
|
|
|
$state->setData($object); |
|
57
|
|
|
|
|
58
|
|
|
if ($state->isLoop() && $state->getLoop()['last']) { |
|
59
|
|
|
$this->entityResolver->clear(); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function describe(ProcessState $state) |
|
64
|
|
|
{ |
|
65
|
|
|
$state->info('create object {class} with array data', [ |
|
66
|
|
|
'class' => $state->getOptions()['entity_class'] ?? '', |
|
67
|
|
|
]); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|