1
1
<?php
2
2
3
- /*
3
+ /**
4
4
* This file is part of the Orbitale DoctrineTools package.
5
5
*
6
6
* (c) Alexandre Rock Ancelet <[email protected] >
34
34
*/
35
35
abstract class AbstractFixture extends BaseAbstractFixture implements OrderedFixtureInterface
36
36
{
37
- /**
38
- * @var EntityManager
39
- */
37
+ /** @var ObjectManager */
40
38
private $ manager ;
41
39
42
- /**
43
- * @var EntityRepository
44
- */
40
+ /** @var EntityRepository */
45
41
private $ repo ;
46
42
47
- /**
48
- * @var int
49
- */
50
- private $ order ;
51
-
52
- /**
53
- * @var string
54
- */
55
- private $ entityClass ;
56
-
57
- /**
58
- * @var PropertyAccessor
59
- */
43
+ /** @var PropertyAccessor */
60
44
private $ propertyAccessor ;
61
45
62
- /**
63
- * @var null|string
64
- */
65
- private $ referencePrefix ;
66
-
67
- /**
68
- * @var bool
69
- */
70
- private $ searchForMatchingIds = true ;
71
-
72
- /**
73
- * @var int
74
- */
46
+ /** @var int */
75
47
private $ totalNumberOfObjects = 0 ;
76
48
77
- /**
78
- * @var int
79
- */
49
+ /** @var int */
80
50
private $ numberOfIteratedObjects = 0 ;
81
51
82
- /**
83
- * @var int
84
- */
85
- private $ flushEveryXIterations = 0 ;
86
-
87
- /**
88
- * @var bool
89
- */
52
+ /** @var bool */
90
53
private $ clearEMOnFlush = true ;
91
54
92
55
public function __construct ()
93
56
{
94
- $ this ->order = $ this ->getOrder ();
95
- $ this ->flushEveryXIterations = $ this ->flushEveryXIterations ();
96
- $ this ->searchForMatchingIds = $ this ->searchForMatchingIds ();
97
- $ this ->entityClass = $ this ->getEntityClass ();
98
- $ this ->referencePrefix = $ this ->getReferencePrefix ();
99
- $ this ->propertyAccessor = class_exists ('Symfony\Component\PropertyAccess\PropertyAccess ' ) ? PropertyAccess::createPropertyAccessor () : null ;
100
- $ this ->clearEMOnFlush = $ this ->clearEntityManagerOnFlush ();
57
+ $ this ->clearEMOnFlush = $ this ->clearEntityManagerOnFlush ();
58
+ if (class_exists ('Symfony\Component\PropertyAccess\PropertyAccess ' )) {
59
+ $ this ->propertyAccessor = PropertyAccess::createPropertyAccessor ();
60
+ }
101
61
}
102
62
63
+ /**
64
+ * Returns the class of the entity you're managing.
65
+ *
66
+ * @return string
67
+ */
68
+ abstract protected function getEntityClass (): string ;
69
+
70
+ /**
71
+ * Returns a list of objects to insert in the database.
72
+ *
73
+ * @return ArrayCollection|object[]
74
+ */
75
+ abstract protected function getObjects ();
76
+
103
77
/**
104
78
* {@inheritdoc}
105
79
*/
106
80
public function load (ObjectManager $ manager )
107
81
{
108
82
$ this ->manager = $ manager ;
109
83
110
- if ($ this ->disableLogger ()) {
84
+ if ($ this ->disableLogger () && $ this -> manager instanceof EntityManager ) {
111
85
$ this ->manager ->getConnection ()->getConfiguration ()->setSQLLogger (null );
112
86
}
113
87
@@ -126,8 +100,8 @@ public function load(ObjectManager $manager)
126
100
// Flush if we performed a "whole" fixture load,
127
101
// or if we flushed with batches but have not flushed all items.
128
102
if (
129
- !$ this ->flushEveryXIterations
130
- || ($ this ->flushEveryXIterations && $ this ->numberOfIteratedObjects !== $ this ->totalNumberOfObjects )
103
+ !$ this ->flushEveryXIterations ()
104
+ || ($ this ->flushEveryXIterations () && $ this ->numberOfIteratedObjects !== $ this ->totalNumberOfObjects )
131
105
) {
132
106
$ this ->manager ->flush ();
133
107
if ($ this ->clearEMOnFlush ) {
@@ -145,7 +119,7 @@ public function load(ObjectManager $manager)
145
119
* @param ClassMetadata $metadata
146
120
* @param null $id
147
121
*/
148
- protected function setGeneratorBasedOnId (ClassMetadata $ metadata , $ id = null )
122
+ protected function setGeneratorBasedOnId (ClassMetadata $ metadata , $ id = null ): void
149
123
{
150
124
if ($ id ) {
151
125
$ metadata ->setIdGenerator (new AssignedGenerator ());
@@ -157,7 +131,7 @@ protected function setGeneratorBasedOnId(ClassMetadata $metadata, $id = null)
157
131
/**
158
132
* Creates the object and persist it in database.
159
133
*
160
- * @param array| object $data
134
+ * @param object $data
161
135
*/
162
136
private function fixtureObject ($ data )
163
137
{
@@ -168,7 +142,7 @@ private function fixtureObject($data)
168
142
$ identifier = $ metadata ->getIdentifier ();
169
143
170
144
// The ID is taken in account to force its use in the database.
171
- $ id = array () ;
145
+ $ id = [] ;
172
146
foreach ($ identifier as $ key ) {
173
147
$ id [$ key ] = $ this ->getPropertyFromData ($ data , $ key );
174
148
}
@@ -183,7 +157,7 @@ private function fixtureObject($data)
183
157
$ addRef = false ;
184
158
185
159
// If the user specifies an ID and the fixture class wants it to be merged, we search for an object.
186
- if ($ id && $ this ->searchForMatchingIds ) {
160
+ if ($ id && $ this ->searchForMatchingIds () ) {
187
161
// Checks that the object ID exists in database.
188
162
$ obj = $ this ->repo ->findOneBy ($ id );
189
163
if ($ obj ) {
@@ -203,7 +177,6 @@ private function fixtureObject($data)
203
177
if (is_array ($ data )) {
204
178
$ obj = $ this ->createNewInstance ($ data );
205
179
foreach ($ data as $ field => $ value ) {
206
-
207
180
// If the value is a callable we execute it and inject the fixture object and the manager.
208
181
if ($ value instanceof \Closure) {
209
182
$ value = $ value ($ obj , $ this , $ this ->manager );
@@ -226,9 +199,9 @@ private function fixtureObject($data)
226
199
227
200
// If we need to flush it, then we do it too.
228
201
if (
229
- $ this ->flushEveryXIterations
230
- && $ this ->numberOfIteratedObjects
231
- && $ this ->numberOfIteratedObjects % $ this ->flushEveryXIterations === 0
202
+ $ this ->numberOfIteratedObjects > 0
203
+ && $ this ->flushEveryXIterations () > 0
204
+ && $ this ->numberOfIteratedObjects % $ this ->flushEveryXIterations () === 0
232
205
) {
233
206
$ this ->manager ->flush ();
234
207
if ($ this ->clearEMOnFlush ) {
@@ -239,27 +212,27 @@ private function fixtureObject($data)
239
212
}
240
213
241
214
// If we have to add a reference, we do it
242
- if ($ addRef === true && $ obj && $ this ->referencePrefix ) {
215
+ if ($ addRef === true && $ obj && $ this ->getReferencePrefix () ) {
243
216
if (!$ id || !reset ($ id )) {
244
217
// If no id was provided in the object, maybe there was one after data hydration.
245
218
// Can be done maybe in entity constructor or in a property callback.
246
219
// So let's try to get it.
247
220
if ($ this ->propertyAccessor ) {
248
221
if ($ this ->propertyAccessor ) {
249
222
try {
250
- $ id = array ( 'id ' => $ this ->propertyAccessor ->getValue ($ obj , 'id ' )) ;
223
+ $ id = [ 'id ' => $ this ->propertyAccessor ->getValue ($ obj , 'id ' )] ;
251
224
} catch (NoSuchIndexException $ e ) {
252
- $ id = array () ;
225
+ $ id = [] ;
253
226
}
254
227
}
255
228
} elseif (method_exists ($ obj , 'getId ' )) {
256
- $ id = array ( 'id ' => $ obj ->getId ()) ;
229
+ $ id = [ 'id ' => $ obj ->getId ()] ;
257
230
}
258
231
}
259
232
if (1 === count ($ id )) {
260
233
// Only reference single identifiers.
261
234
$ id = reset ($ id );
262
- $ this ->addReference ($ this ->referencePrefix .($ id ?: (string ) $ obj ), $ obj );
235
+ $ this ->addReference ($ this ->getReferencePrefix () .($ id ?: (string ) $ obj ), $ obj );
263
236
} elseif (count ($ id ) > 1 ) {
264
237
throw new \RuntimeException ('Cannot add reference for composite identifiers. ' );
265
238
}
@@ -274,7 +247,7 @@ private function fixtureObject($data)
274
247
*
275
248
* @return mixed
276
249
*/
277
- private function getPropertyFromData ($ data , $ key )
250
+ private function getPropertyFromData ($ data , string $ key )
278
251
{
279
252
if (is_object ($ data )) {
280
253
$ method = 'get ' .ucfirst ($ key );
@@ -286,9 +259,7 @@ private function getPropertyFromData($data, $key)
286
259
}
287
260
}
288
261
289
- if (isset ($ data [$ key ])) {
290
- return $ data [$ key ];
291
- }
262
+ return $ data [$ key ] ?? null ;
292
263
}
293
264
294
265
/**
@@ -298,9 +269,9 @@ private function getPropertyFromData($data, $key)
298
269
*
299
270
* @return int
300
271
*/
301
- public function getOrder ()
272
+ public function getOrder (): int
302
273
{
303
- return $ this -> order ;
274
+ return 0 ;
304
275
}
305
276
306
277
/**
@@ -309,7 +280,7 @@ public function getOrder()
309
280
*
310
281
* @return bool
311
282
*/
312
- protected function disableLogger ()
283
+ protected function disableLogger (): bool
313
284
{
314
285
return false ;
315
286
}
@@ -320,35 +291,30 @@ protected function disableLogger()
320
291
* NOTE: To create references of an object, it must have an ID, and if not, implement __toString(), because
321
292
* each object is referenced BEFORE flushing the database.
322
293
* NOTE2: If you specified a "flushEveryXIterations" value, then the object will be provided with an ID every time.
323
- *
324
- * @return string|null
325
294
*/
326
- protected function getReferencePrefix ()
295
+ protected function getReferencePrefix (): ? string
327
296
{
328
- return $ this -> referencePrefix ;
297
+ return null ;
329
298
}
330
299
331
300
/**
332
301
* If specified, the entity manager will be flushed every X times, depending on your specified values.
333
302
* Default is null, so the database is flushed only at the end of all persists.
334
- *
335
- * @return bool
336
303
*/
337
- protected function flushEveryXIterations ()
304
+ protected function flushEveryXIterations (): int
338
305
{
339
- return $ this -> flushEveryXIterations ;
306
+ return 0 ;
340
307
}
341
308
342
309
/**
343
- * If true and an ID is specified, will execute a $manager->find($id) in the database.
344
- * By default this var is true.
310
+ * If true and an ID is specified in fixture's $data, will execute a $manager->find($id) in the database.
345
311
* Be careful, if you set it to false you may have "duplicate entry" errors if your database is already populated.
346
312
*
347
313
* @return bool
348
314
*/
349
- protected function searchForMatchingIds ()
315
+ protected function searchForMatchingIds (): bool
350
316
{
351
- return $ this -> searchForMatchingIds ;
317
+ return true ;
352
318
}
353
319
354
320
/**
@@ -357,35 +323,23 @@ protected function searchForMatchingIds()
357
323
*
358
324
* @return bool
359
325
*/
360
- protected function clearEntityManagerOnFlush ()
326
+ protected function clearEntityManagerOnFlush (): bool
361
327
{
362
- return $ this -> clearEMOnFlush ;
328
+ return true ;
363
329
}
364
330
365
331
/**
366
332
* Creates a new instance of the class associated with the fixture.
367
- * Very useful if you have constructor arguments to manage.
333
+ * Override this method if you have constructor arguments to manage yourself depending on input data .
368
334
*
369
335
* @param array $data
370
336
*
371
337
* @return object
372
338
*/
373
- protected function createNewInstance ($ data )
339
+ protected function createNewInstance (array $ data )
374
340
{
375
- return new $ this ->entityClass ;
376
- }
377
-
378
- /**
379
- * Returns the class of the entity you're managing.
380
- *
381
- * @return string
382
- */
383
- protected abstract function getEntityClass ();
341
+ $ class = $ this ->getEntityClass ();
384
342
385
- /**
386
- * Returns a list of objects to insert in the database.
387
- *
388
- * @return ArrayCollection|object[]
389
- */
390
- protected abstract function getObjects ();
343
+ return new $ class ;
344
+ }
391
345
}
0 commit comments