Completed
Push — master ( e7f043...356a06 )
by Mārtiņš
01:50
created

NonceIdentity   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 85
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createIdentity() 0 18 1
A store() 0 8 2
A updateIdentity() 0 14 1
B fetch() 0 26 2
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 2
    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 2
                  FROM {$this->table}
27
                 WHERE type = :type
28
                   AND status = :status
29
                   AND fingerprint = :fingerprint
30
                   AND identifier = :identifier";
31
32 2
        $statement = $this->connection->prepare($sql);
33
34 2
        $statement->bindValue(':type', $entity->getType());
35 2
        $statement->bindValue(':status', Entity\Identity::STATUS_ACTIVE);
36 2
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
37 2
        $statement->bindValue(':identifier', $entity->getIdentifier());
38
39 2
        $statement->execute();
40
41 2
        $data = $statement->fetch(PDO::FETCH_ASSOC);
42
43 2
        if ($data) {
44 1
            $this->applyValues($entity, $data);
45
        }
46 2
    }
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