Completed
Push — master ( 356a06...1ef8e7 )
by Mārtiņš
01:49
created

CookieIdentity::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * All of the SQL code related to the storage and retrieval of cookie-based identities.
7
 * Probably need some cleanup in `store()` method
8
 */
9
10
use Palladium\Component\DataMapper;
11
use Palladium\Entity as Entity;
12
use PDOStatement;
13
use PDO;
14
15
class CookieIdentity extends DataMapper
16
{
17
18
    /**
19
     * @param Entity\CookieIdentity $entity
20
     */
21 1
    public function fetch(Entity\CookieIdentity $entity)
22
    {
23
        $sql = "SELECT identity_id  AS id,
24
                       parent_id    AS parentId,
25
                       hash         AS hash,
26
                       expires_on   AS expiresOn
27 1
                  FROM {$this->table} AS Identities
28
                 WHERE type = :type
29
                   AND account_id = :account
30
                   AND identifier = :identifier
31
                   AND fingerprint = :fingerprint
32
                   AND status = :status";
33
34 1
        $statement = $this->connection->prepare($sql);
35 1
        $this->bindCommonParameters($statement, $entity);
36
37 1
        $statement->execute();
38
39 1
        $data = $statement->fetch(PDO::FETCH_ASSOC);
40
41 1
        if ($data) {
42 1
            $this->applyValues($entity, $data);
43
        }
44 1
    }
45
46
47 2
    private function bindCommonParameters(PDOStatement $statement, Entity\CookieIdentity $entity)
48
    {
49 2
        $statement->bindValue(':type', $entity->getType());
50 2
        $statement->bindValue(':status', $entity->getStatus());
51 2
        $statement->bindValue(':account', $entity->getAccountId());
52 2
        $statement->bindValue(':identifier', $entity->getSeries());
53 2
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
54 2
    }
55
56
57
58 2
    public function store(Entity\CookieIdentity $entity)
59
    {
60 2
        if ($entity->getId() === null) {
61 1
            $this->createCookie($entity);
62 1
            return;
63
        }
64
65 1
        $this->updateCookie($entity);
66 1
    }
67
68
69 1
    private function createCookie(Entity\CookieIdentity $entity)
70
    {
71 1
        $sql = "INSERT INTO {$this->table}
72
                       (account_id, parent_id, type, status, identifier, fingerprint, hash, created_on, expires_on)
73
                VALUES (:account, :parent, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)";
74
75 1
        $statement = $this->connection->prepare($sql);
76
77 1
        $statement->bindValue(':parent', $entity->getParentId());
78 1
        $statement->bindValue(':hash', $entity->getHash());
79 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
80 1
        $statement->bindValue(':created', time());
81
82 1
        $this->bindCommonParameters($statement, $entity);
83
84 1
        $statement->execute();
85
86 1
        $entity->setId($this->connection->lastInsertId());
87 1
    }
88
89
90 1
    private function updateCookie(Entity\CookieIdentity $entity)
91
    {
92 1
        $active = Entity\Identity::STATUS_ACTIVE;
93
94 1
        $sql = "UPDATE {$this->table}
95
                   SET status = :status,
96
                       hash = :hash,
97
                       used_on = :used,
98
                       expires_on = :expires
99
                 WHERE identity_id = :id
100 1
                   AND status = {$active}";
101
102 1
        $statement = $this->connection->prepare($sql);
103
104 1
        $statement->bindValue(':id', $entity->getId());
105 1
        $statement->bindValue(':status', $entity->getStatus());
106 1
        $statement->bindValue(':hash', $entity->getHash());
107 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
108 1
        $statement->bindValue(':used', $entity->getLastUsed());
109
110 1
        $statement->execute();
111 1
    }
112
}
113