| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Palladium\Mapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * SQL logic for authentication attemps using username/password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Palladium\Component\DataMapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Palladium\Entity as Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PDO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class NonceIdentity extends DataMapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param Entity\NonceIdentity $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 5 |  |     public function fetch(Entity\NonceIdentity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $sql = "SELECT identity_id      AS id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                        account_id       AS accountId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                        hash             AS hash, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                        status           AS status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                        expires_on       AS expiresOn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 5 |  |                   FROM {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                  WHERE type = :type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                    AND status = :status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                    AND fingerprint = :fingerprint | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |                    AND identifier = :identifier"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 5 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |         $statement->bindValue(':type', $entity->getType()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |         $statement->bindValue(':status', Entity\Identity::STATUS_ACTIVE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |         $statement->bindValue(':fingerprint', $entity->getFingerprint()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |         $statement->bindValue(':identifier', $entity->getIdentifier()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 5 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 5 |  |         $data = $statement->fetch(PDO::FETCH_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 5 |  |         if ($data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 3 |  |             $this->applyValues($entity, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param Entity\NonceIdentity $entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 2 |  |     public function store(Entity\NonceIdentity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |         if ($entity->getId() === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |             $this->createIdentity($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $this->updateIdentity($entity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 1 |  |     private function createIdentity(Entity\NonceIdentity $entity) | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |         $sql = "INSERT INTO {$this->table} | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                        (account_id, type, status, identifier, fingerprint, hash, created_on, expires_on) | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                 VALUES (:account, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)"; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |         $statement->bindValue(':account', $entity->getAccountId()); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         $statement->bindValue(':type', $entity->getType()); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |         $statement->bindValue(':status', $entity->getStatus()); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |         $statement->bindValue(':identifier', $entity->getIdentifier()); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |         $statement->bindValue(':fingerprint', $entity->getFingerprint()); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |         $statement->bindValue(':hash', $entity->getHash()); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |         $statement->bindValue(':created', time()); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |         $statement->bindValue(':expires', $entity->getExpiresOn()); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 1 |  |     private function updateIdentity(Entity\NonceIdentity $entity) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |         $sql = "UPDATE {$this->table} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                    SET status = :status, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                        used_on = :used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                  WHERE identity_id = :id"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         $statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         $statement->bindValue(':id', $entity->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |         $statement->bindValue(':status', $entity->getStatus()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |         $statement->bindValue(':used', time()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         $statement->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |