| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Palladium\Mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * SQL code responsible for locating all of the identities, that have been associated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * to a given account and discarding them in bulk. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Used mostly in case of password reset or, if cookie has been compromised. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Palladium\Component\DataMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Palladium\Entity as Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PDOStatement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PDO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class IdentityCollection extends DataMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param Entity\IdentityCollection $collection | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 1 |  |     public function store(Entity\IdentityCollection $collection) | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 1 |  |         $sql = "UPDATE {$this->table} | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |                    SET status = :status | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |                  WHERE identity_id = :id"; | 
            
                                                                        
                            
            
                                    
            
            
                | 28 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 | 1 |  |         foreach ($collection as $entity) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |             $statement->execute([ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 1 |  |                 ':id' => $entity->getId(), | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |                 ':status' => $entity->getStatus(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param Entity\IdentityCollection $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 2 |  |     public function fetch(Entity\IdentityCollection $collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 2 |  |         if ($collection->getParentId() !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |             $this->fetchByParent($collection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         $this->fetchByAccount($collection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |     private function fetchByAccount(Entity\IdentityCollection $collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $sql = "SELECT identity_id  AS id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |                   FROM {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                  WHERE status = :status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                    AND account_id = :account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                    AND type = :type"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $statement->bindValue(':account', $collection->getAccountId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         $statement->bindValue(':type', $collection->getType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 1 |  |         $this->populateCollection($collection, $statement); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |     private function fetchByParent(Entity\IdentityCollection $collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $sql = "SELECT identity_id  AS id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |                   FROM {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                  WHERE status = :status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                    AND parent_id = :parent"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         $statement->bindValue(':parent', $collection->getParentId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $this->populateCollection($collection, $statement); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |     private function populateCollection(Entity\IdentityCollection $collection, PDOStatement $statement) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |         $statement->bindValue(':status', $collection->getStatus()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |         foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $parameters) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             $collection->addBlueprint($parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |