| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Palladium\Mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * SQL code for locating identity data by token and updating last usage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Palladium\Component\DataMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Palladium\Entity as Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PDO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Identity extends DataMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param Entity\Identity $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 10 |  |     public function store(Entity\Identity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 10 |  |         $sql = "UPDATE {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                    SET used_on = :used, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                        status = :status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                  WHERE identity_id = :id"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 10 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 10 |  |         $statement->bindValue(':id', $entity->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 10 |  |         $statement->bindValue(':used', $entity->getLastUsed()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 10 |  |         $statement->bindValue(':status', $entity->getStatus()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 10 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param Entity\Identity $entity | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 1 |  |     public function remove(Entity\Identity $entity) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |         $sql = "DELETE FROM {$this->table} WHERE identity_id = :id"; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |         $statement->bindValue(':id', $entity->getId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param Entity\Identity $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 9 |  |     public function fetch(Entity\Identity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 9 |  |         if ($entity->getId()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             $this->fetchById($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 8 |  |         $this->fetchByToken($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |     private function fetchById(Entity\Identity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $sql = "SELECT identity_id      AS id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                        parent_id        AS parentId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                        account_id       AS accountId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                        status           AS status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                        hash             AS hash, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                        token_expires_on AS tokenEndOfLife | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                   FROM {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                  WHERE identity_id = :id"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         $statement->bindValue(':id', $entity->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |         $data = $statement->fetch(PDO::FETCH_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         if ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |             $this->applyValues($entity, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 8 |  |     private function fetchByToken(Entity\Identity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $sql = "SELECT identity_id      AS id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                        parent_id        AS parentId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                        account_id       AS accountId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                        status           AS status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                        hash             AS hash, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                        token_expires_on AS tokenEndOfLife, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                        token_payload    AS tokenPayload | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 8 |  |                   FROM {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                  WHERE token = :token | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                    AND token_action = :action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                    AND token_expires_on > :expires"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 8 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 8 |  |         $statement->bindValue(':token', $entity->getToken()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 8 |  |         $statement->bindValue(':action', $entity->getTokenAction()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 8 |  |         $statement->bindValue(':expires', $entity->getTokenEndOfLife()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 8 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 8 |  |         $data = $statement->fetch(PDO::FETCH_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 8 |  |         if ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 5 |  |             if ($data['tokenPayload'] !== null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |                 $data['tokenPayload'] = json_decode($data['tokenPayload'], true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 5 |  |             $this->applyValues($entity, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 8 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |