| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Archivr\IndexMerger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Archivr\ConflictHandler\ConflictHandlerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Archivr\Index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Archivr\IndexObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class StandardIndexMerger extends AbstractIndexMerger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public function merge(Index $remoteIndex, Index $localIndex, Index $lastLocalIndex = null): Index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $mergedIndex = new Index(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         if ($lastLocalIndex === null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             // lets make our life easier | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             $lastLocalIndex = new Index(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         foreach ($localIndex as $localObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             /** @var IndexObject $localObject */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             $remoteObject = $remoteIndex->getObjectByPath($localObject->getRelativePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $lastLocalObject = $lastLocalIndex->getObjectByPath($localObject->getRelativePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             // compare existing to known object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             if ($lastLocalObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 $localObjectModified = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 $localObjectModified = $localObjectModified || ($localObject->getType() !== $lastLocalObject->getType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 $localObjectModified = $localObjectModified || ($localObject->getMtime() !== $lastLocalObject->getMtime()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 $localObjectModified = $localObjectModified || ($localObject->getCtime() !== $lastLocalObject->getCtime()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 $localObjectModified = $localObjectModified || ($localObject->getMode() !== $lastLocalObject->getMode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $localObjectModified = $localObjectModified || ($localObject->getSize() !== $lastLocalObject->getSize()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 $localObjectModified = $localObjectModified || ($localObject->getLinkTarget() !== $lastLocalObject->getLinkTarget()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 // remote object has been modified if it does not equal the object on its last synchronization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 $remoteObjectModified = !$lastLocalObject->equals($remoteObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             // object has been created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 $localObjectModified = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 // object has been created since last synchronization | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $remoteObjectModified = $remoteObject !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             // conflict if both the local and the remote object has been changed | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 55 |  | View Code Duplication |             if ($localObjectModified && $remoteObjectModified) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $this->conflict($mergedIndex, $remoteObject, $localObject, $lastLocalObject); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             // add the remote object if only it has been modified | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             elseif ($remoteObjectModified) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $mergedIndex->addObject($remoteObject); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             // add the local object otherwise if only it or none has been modified | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $mergedIndex->addObject($localObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($remoteIndex as $remoteObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             /** @var IndexObject $remoteObject */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $lastLocalObject = $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             // only consider objects not existing locally as those are already considered by the first loop | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             if (!$localObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 if ($lastLocalObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     // local object has been deleted | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     $localObjectModified = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     // compare remote object to object state at last sync | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     $remoteObjectModified = !$remoteObject->equals($lastLocalObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                     // object already didn't exist locally at the last sync | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $localObjectModified = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     // another client added the remote object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                     $remoteObjectModified = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 // conflict if both the local and the remote object has been changed | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 103 |  | View Code Duplication |                 if ($localObjectModified && $remoteObjectModified) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     $this->conflict($mergedIndex, $remoteObject, $localObject, $lastLocalObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 // another client added the remote object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 elseif (!$lastLocalObject) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                     $mergedIndex->addObject($remoteObject); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return $mergedIndex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |     protected function conflict(Index $mergedIndex, IndexObject $remoteObject, IndexObject $localObject = null, IndexObject $lastLocalObject = null): void | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |         $solution = $this->getConflictHandler()->handleConflict($remoteObject, $localObject, $lastLocalObject); | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |         switch ($solution) | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |         { | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |             case ConflictHandlerInterface::USE_LOCAL: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |                 if ($localObject) | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |                 { | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |                     $mergedIndex->addObject($localObject); | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |             case ConflictHandlerInterface::USE_REMOTE: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |                 if ($remoteObject) | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |                 { | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |                     $mergedIndex->addObject($remoteObject); | 
            
                                                        
            
                                    
            
            
                | 139 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |                 break; | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |             default: | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |                 throw new \LogicException(); | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 148 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 149 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.