-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelImporter.php
More file actions
141 lines (112 loc) · 3.08 KB
/
ModelImporter.php
File metadata and controls
141 lines (112 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace App\Service;
use App\Controller\EntityController;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessor;
class ModelImporter
{
private array $done = [];
private array $errors = [];
private array $data = [];
private EntityManagerInterface $em;
public function __construct(
EntityManagerInterface $entityManager
) {
$this->em = $entityManager;
}
public function import( array $data ): array
{
$this->done = [];
$this->data = $data;
foreach ( $data as $ref => $fields ) {
if ( isset( $this->done[ $ref ] ) ) {
continue;
}
$this->importRef( $ref, $fields );
$this->done[ $ref ] = true;
}
$this->em->flush();
return $this->errors;
}
public function importRef( $ref, $fields )
{
// Already running.
if ( isset( $this->done[ $ref ] ) ) {
return $this->done[ $ref ];
}
$entity = $fields['_entity'];
if ( ! $entity ) {
$this->errors[] = 'Entity not found for: ' . $ref;
return null;
}
unset( $fields['_entity'] );
$modelClass = EntityController::getEntityModelClass( $entity );
if ( ! class_exists( $modelClass ) ) {
$this->errors[] = 'Model not found for: ' . $ref . ' (' . $modelClass . ')';
return null;
}
$model = $modelClass::get( $ref );
if ( $model ) {
// Update.
$entity = $model->getEntity();
} else {
// Create.
$entity = "\\App\\Entity\\" . $entity;
$entity = new $entity();
$entity->setRef( $ref );
$model = new $modelClass( $entity );
}
$this->done[ $ref ] = $model;
$queued = [];
foreach ( $fields as $property => $value ) {
if ( 'ref' === $property ) {
continue;
}
if ( is_string( $value ) && ! empty( $this->data[ $value ] ) ) {
$queued[ $property ] = $value;
continue;
}
if ( is_array( $value ) ) {
$queued[ $property ] = $value;
continue;
}
$setter = 'set' . ucfirst( $property );
if ( method_exists( $entity, $setter ) ) {
// Call setter on model.
call_user_func( [ $model, $setter ], $value );
}
}
// Make sure that we store the scalar properties first so we can get an ID for this entity.
$model->persist( $this->em, true );
foreach ( $queued as $property => $value ) {
if ( is_string( $value ) && ! empty( $this->data[ $value ] ) ) {
$relation = $this->importRef( $value, $this->data[ $value ] );
$value = $relation->getEntity();
}
if ( is_array( $value ) ) {
$value = $this->parseSubFields( $value );
}
$setter = 'set' . ucfirst( $property );
if ( method_exists( $entity, $setter ) ) {
// Call setter on model.
call_user_func( [ $model, $setter ], $value );
}
}
$model->persist( $this->em );
return $model;
}
public function parseSubFields( array $fields ): array
{
foreach ( $fields as $key => $value ) {
if ( is_string( $value ) ) {
if ( isset( $this->data[ $value ] ) ) {
$model = $this->importRef( $value, $this->data[ $value ] );
$fields[ $key ] = $model->getId();
}
} elseif ( is_array( $value ) ) {
$fields[ $key ] = $this->parseSubFields( $value );
}
}
return $fields;
}
}