| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Sirius\Orm\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sirius\Orm\Entity\EntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Sirius\Orm\Entity\StateEnum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Sirius\Orm\Mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Sirius\Orm\Relation\ManyToMany; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Sirius\Orm\Relation\Relation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Sirius\Orm\Relation\RelationConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class DeletePivotRows extends BaseAction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var Mapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $nativeMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var EntityInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $nativeEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function __construct(ManyToMany $relation, EntityInterface $nativeEntity, EntityInterface $foreignEntity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->relation = $relation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->nativeMapper = $relation->getNativeMapper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->nativeEntity = $nativeEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->mapper = $relation->getForeignMapper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->entity = $foreignEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     protected function execute() | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $conditions = $this->getConditions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         if (empty($conditions)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $delete = new \Sirius\Sql\Delete($this->mapper->getWriteConnection()); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $delete->from((string) $this->relation->getOption(RelationConfig::THROUGH_TABLE)); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $delete->whereAll($conditions, false); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $delete->perform(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function revert() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return; // no change to the entity has actually been performed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function onSuccess() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected function getConditions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $conditions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $nativeEntityPk = (array)$this->nativeMapper->getPrimaryKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $nativeThroughCols = (array)$this->relation->getOption(RelationConfig::THROUGH_NATIVE_COLUMN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($nativeEntityPk as $idx => $col) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $val = $this->nativeMapper->getEntityAttribute($this->nativeEntity, $col); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             if ($val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $conditions[$nativeThroughCols[$idx]] = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $entityPk = (array)$this->mapper->getPrimaryKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $throughCols = (array)$this->relation->getOption(RelationConfig::THROUGH_FOREIGN_COLUMN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($entityPk as $idx => $col) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $val = $this->mapper->getEntityAttribute($this->entity, $col); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if ($val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $conditions[$throughCols[$idx]] = $val; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // not enough columns? bail | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if (count($conditions) != count($entityPk) + count($nativeEntityPk)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return $conditions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |