3
3
namespace Port \Doctrine ;
4
4
5
5
use Port \Doctrine \Exception \UnsupportedDatabaseTypeException ;
6
+ use Port \Doctrine \LookupStrategy \FieldsLookupStrategy ;
6
7
use Port \Writer ;
7
8
use Doctrine \Common \Util \Inflector ;
8
9
use Doctrine \DBAL \Logging \SQLLogger ;
@@ -26,20 +27,6 @@ class DoctrineWriter implements Writer, Writer\FlushableWriter
26
27
*/
27
28
protected $ objectManager ;
28
29
29
- /**
30
- * Fully qualified model name
31
- *
32
- * @var string
33
- */
34
- protected $ objectName ;
35
-
36
- /**
37
- * Doctrine object repository
38
- *
39
- * @var ObjectRepository
40
- */
41
- protected $ objectRepository ;
42
-
43
30
/**
44
31
* @var ClassMetadata
45
32
*/
@@ -60,57 +47,50 @@ class DoctrineWriter implements Writer, Writer\FlushableWriter
60
47
protected $ truncate = true ;
61
48
62
49
/**
63
- * List of fields used to lookup an object
64
- *
65
- * @var array
50
+ * @var LookupStrategy
66
51
*/
67
- protected $ lookupFields = [] ;
52
+ private $ lookupStrategy ;
68
53
69
- /**
70
- * Method used for looking up the item
71
- *
72
- * @var array
73
- */
74
- protected $ lookupMethod ;
54
+ public static function withLookupStrategy (
55
+ ObjectManager $ objectManager ,
56
+ LookupStrategy $ lookupStrategy
57
+ ) {
58
+ return new self ( $ objectManager , null , null , $ lookupStrategy );
59
+ }
75
60
76
61
/**
77
62
* Constructor
78
63
*
79
- * @param ObjectManager $objectManager
80
- * @param string $objectName
81
- * @param string|array $index Field or fields to find current entities by
82
- * @param string $lookupMethod Method used for looking up the item
64
+ * @param ObjectManager $objectManager
65
+ * @param string $objectName
66
+ * @param string|array $index Field or fields to find current
67
+ * entities by
68
+ * @param string $lookupMethod Method used for looking up the item
69
+ * @param LookupStrategy $lookupStrategy
70
+ *
71
+ * @throws UnsupportedDatabaseTypeException
83
72
*/
84
73
public function __construct (
85
74
ObjectManager $ objectManager ,
86
75
$ objectName ,
87
76
$ index = null ,
88
- $ lookupMethod = 'findOneBy '
77
+ $ lookupMethod = 'findOneBy ' ,
78
+ LookupStrategy $ lookupStrategy = null
89
79
) {
90
80
$ this ->ensureSupportedObjectManager ($ objectManager );
91
81
$ this ->objectManager = $ objectManager ;
92
- $ this ->objectRepository = $ objectManager ->getRepository ($ objectName );
93
82
$ this ->objectMetadata = $ objectManager ->getClassMetadata ($ objectName );
94
- //translate objectName in case a namespace alias is used
95
- $ this -> objectName = $ this -> objectMetadata -> getName ();
96
- if ( $ index ) {
97
- if ( is_array ( $ index )) {
98
- $ this -> lookupFields = $ index ;
99
- } else {
100
- $ this -> lookupFields = [ $ index ] ;
83
+
84
+ if ( $ objectManager !== null && $ index !== null ) {
85
+ $ lookupStrategy = ( new FieldsLookupStrategy ( $ objectManager , $ objectName ))
86
+ -> withIndex ( $ index );
87
+
88
+ if ( $ lookupMethod ) {
89
+ $ lookupStrategy = $ lookupStrategy -> withLookupMethod ( $ lookupMethod ) ;
101
90
}
102
- }
103
91
104
- if (!method_exists ($ this ->objectRepository , $ lookupMethod )) {
105
- throw new \InvalidArgumentException (
106
- sprintf (
107
- 'Repository %s has no method %s ' ,
108
- get_class ($ this ->objectRepository ),
109
- $ lookupMethod
110
- )
111
- );
92
+ $ this ->lookupStrategy = $ lookupStrategy ;
112
93
}
113
- $ this ->lookupMethod = [$ this ->objectRepository , $ lookupMethod ];
114
94
}
115
95
116
96
/**
@@ -189,7 +169,7 @@ public function writeItem(array $item)
189
169
public function flush ()
190
170
{
191
171
$ this ->objectManager ->flush ();
192
- $ this ->objectManager ->clear ($ this ->objectName );
172
+ $ this ->objectManager ->clear ($ this ->objectMetadata -> getName () );
193
173
}
194
174
195
175
/**
@@ -284,7 +264,9 @@ protected function truncateTable()
284
264
$ query = $ connection ->getDatabasePlatform ()->getTruncateTableSQL ($ tableName , true );
285
265
$ connection ->executeQuery ($ query );
286
266
} elseif ($ this ->objectManager instanceof \Doctrine \ODM \MongoDB \DocumentManager) {
287
- $ this ->objectManager ->getDocumentCollection ($ this ->objectName )->remove (array ());
267
+ $ this ->objectManager ->getDocumentCollection (
268
+ $ this ->objectMetadata ->getName ()
269
+ )->remove ([]);
288
270
}
289
271
}
290
272
@@ -320,27 +302,13 @@ protected function reEnableLogging()
320
302
*/
321
303
protected function findOrCreateItem (array $ item )
322
304
{
323
- $ object = null ;
324
305
// If the table was not truncated to begin with, find current object
325
306
// first
326
307
if (!$ this ->truncate ) {
327
- if (!empty ($ this ->lookupFields )) {
328
- $ lookupConditions = array ();
329
- foreach ($ this ->lookupFields as $ fieldName ) {
330
- $ lookupConditions [$ fieldName ] = $ item [$ fieldName ];
331
- }
332
-
333
- $ object = call_user_func ($ this ->lookupMethod , $ lookupConditions );
334
- } else {
335
- $ object = $ this ->objectRepository ->find (current ($ item ));
336
- }
337
- }
338
-
339
- if (!$ object ) {
340
- return $ this ->getNewInstance ();
308
+ return $ this ->lookupStrategy ->lookup ($ item );
341
309
}
342
310
343
- return $ object ;
311
+ return $ this -> getNewInstance () ;
344
312
}
345
313
346
314
protected function ensureSupportedObjectManager (ObjectManager $ objectManager )
0 commit comments