33namespace Port \Doctrine ;
44
55use Port \Doctrine \Exception \UnsupportedDatabaseTypeException ;
6+ use Port \Doctrine \LookupStrategy \PrimaryKeyLookupStrategy ;
67use Port \Writer ;
78use Doctrine \Common \Util \Inflector ;
89use Doctrine \DBAL \Logging \SQLLogger ;
@@ -26,20 +27,6 @@ class DoctrineWriter implements Writer, Writer\FlushableWriter
2627 */
2728 protected $ objectManager ;
2829
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-
4330 /**
4431 * @var ClassMetadata
4532 */
@@ -60,57 +47,50 @@ class DoctrineWriter implements Writer, Writer\FlushableWriter
6047 protected $ truncate = true ;
6148
6249 /**
63- * List of fields used to lookup an object
64- *
65- * @var array
50+ * @var LookupStrategy
6651 */
67- protected $ lookupFields = [] ;
52+ private $ lookupStrategy ;
6853
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+ }
7560
7661 /**
7762 * Constructor
7863 *
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
8372 */
8473 public function __construct (
8574 ObjectManager $ objectManager ,
8675 $ objectName ,
8776 $ index = null ,
88- $ lookupMethod = 'findOneBy '
77+ $ lookupMethod = 'findOneBy ' ,
78+ LookupStrategy $ lookupStrategy = null
8979 ) {
9080 $ this ->ensureSupportedObjectManager ($ objectManager );
9181 $ this ->objectManager = $ objectManager ;
92- $ this ->objectRepository = $ objectManager ->getRepository ($ objectName );
9382 $ 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 PrimaryKeyLookupStrategy ( $ objectManager , $ objectName ))
86+ -> withIndex ( $ index );
87+
88+ if ( $ lookupMethod ) {
89+ $ lookupStrategy = $ lookupStrategy -> withLookupMethod ( $ lookupMethod ) ;
10190 }
102- }
10391
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 ;
11293 }
113- $ this ->lookupMethod = [$ this ->objectRepository , $ lookupMethod ];
11494 }
11595
11696 /**
@@ -189,7 +169,7 @@ public function writeItem(array $item)
189169 public function flush ()
190170 {
191171 $ this ->objectManager ->flush ();
192- $ this ->objectManager ->clear ($ this ->objectName );
172+ $ this ->objectManager ->clear ($ this ->objectMetadata -> getName () );
193173 }
194174
195175 /**
@@ -284,7 +264,9 @@ protected function truncateTable()
284264 $ query = $ connection ->getDatabasePlatform ()->getTruncateTableSQL ($ tableName , true );
285265 $ connection ->executeQuery ($ query );
286266 } 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 ([]);
288270 }
289271 }
290272
@@ -320,27 +302,13 @@ protected function reEnableLogging()
320302 */
321303 protected function findOrCreateItem (array $ item )
322304 {
323- $ object = null ;
324305 // If the table was not truncated to begin with, find current object
325306 // first
326307 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 );
341309 }
342310
343- return $ object ;
311+ return $ this -> getNewInstance () ;
344312 }
345313
346314 protected function ensureSupportedObjectManager (ObjectManager $ objectManager )
0 commit comments