Completed
Push — master ( 734e62...750475 )
by Mārtiņš
03:48
created

StandardIdentity::fetch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 6
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Palladium\Mapper;
4
5
/**
6
 * SQL logic for authentication attempts using username/password
7
 */
8
9
use Palladium\Component\DataMapper;
10
use Palladium\Entity as Entity;
11
use PDOStatement;
12
use PDO;
13
14
class StandardIdentity extends DataMapper
15
{
16
17
    /**
18
     * @param Entity\StandardIdentity $entity
19
     */
20
    public function exists(Entity\StandardIdentity $entity)
21
    {
22
        $sql = "SELECT 1
23
                  FROM {$this->table}
24
                 WHERE type = :type
25
                   AND fingerprint = :fingerprint
26
                   AND identifier = :email
27
                   AND (expires_on IS NULL OR expires_on > :now)";
28
29
        $statement = $this->connection->prepare($sql);
30
31
        $statement->bindValue(':type', Entity\StandardIdentity::TYPE_EMAIL);
32
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
33
        $statement->bindValue(':email', $entity->getIdentifier());
34
        $statement->bindValue(':now', time());
35
36
        $statement->execute();
37
        $data = $statement->fetch(PDO::FETCH_ASSOC);
38
39
        return empty($data) === false;
40
    }
41
42
43
    /**
44
     * @param Entity\StandardIdentity $entity
45
     */
46
    public function fetch(Entity\StandardIdentity $entity)
47
    {
48
        if ($entity->getId()) {
49
            $this->fetchById($entity);
50
            return;
51
        }
52
53
        $this->fetchByIdentifier($entity);
54
    }
55
56
57 View Code Duplication
    private function fetchByIdentifier(Entity\StandardIdentity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $sql = "SELECT identity_id      AS id,
60
                       account_id       AS accountId,
61
                       hash             AS hash,
62
                       status           AS status,
63
                       used_on          AS lastUsed,
64
                       token            AS token,
65
                       token_action     AS tokenAction,
66
                       token_expires_on AS tokenEndOfLife,
67
                       token_payload    AS tokenPayload
68
                  FROM {$this->table}
69
                 WHERE type = :type
70
                   AND fingerprint = :fingerprint
71
                   AND identifier = :email";
72
73
        $statement = $this->connection->prepare($sql);
74
75
        $statement->bindValue(':type', $entity->getType());
76
        $statement->bindValue(':email', $entity->getIdentifier());
77
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
78
79
        $statement->execute();
80
81
        $data = $statement->fetch(PDO::FETCH_ASSOC);
82
83
        if ($data) {
84
            $this->applyValues($entity, $data);
85
        }
86
    }
87
88
89 View Code Duplication
    private function fetchById(Entity\StandardIdentity $entity)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $sql = "SELECT identity_id      AS id,
92
                       identifier       AS identifier,
93
                       account_id       AS accountId,
94
                       hash             AS hash,
95
                       status           AS status,
96
                       used_on          AS lastUsed,
97
                       token            AS token,
98
                       token_action     AS tokenAction,
99
                       token_expires_on AS tokenEndOfLife,
100
                       token_payload    AS tokenPayload
101
                  FROM {$this->table}
102
                 WHERE type = :type
103
                   AND identity_id = :id";
104
105
        $statement = $this->connection->prepare($sql);
106
107
        $statement->bindValue(':type', $entity->getType());
108
        $statement->bindValue(':id', $entity->getId());
109
110
        $statement->execute();
111
112
        $data = $statement->fetch(PDO::FETCH_ASSOC);
113
114
        if ($data) {
115
            $this->applyValues($entity, $data);
116
        }
117
    }
118
119
120
    /**
121
     * @param Entity\StandardIdentity $entity
122
     */
123 2
    public function store(Entity\StandardIdentity $entity)
124
    {
125 2
        if ($entity->getId() === null) {
126 1
            $this->createIdentity($entity);
127 1
            return;
128
        }
129
130 1
        $this->updateIdentity($entity);
131 1
    }
132
133
134 1
    private function createIdentity(Entity\StandardIdentity $entity)
135
    {
136 1
        $sql = "INSERT INTO {$this->table}
137
                       (type, status, identifier, fingerprint, hash, created_on, token, token_action, token_expires_on, token_payload)
138
                VALUES (:type, :status, :email, :fingerprint, :hash, :created, :token, :action, :token_eol, :payload)";
139
140 1
        $statement = $this->connection->prepare($sql);
141
142 1
        $statement->bindValue(':type', Entity\StandardIdentity::TYPE_EMAIL);
143 1
        $statement->bindValue(':status', Entity\StandardIdentity::STATUS_NEW);
144 1
        $statement->bindValue(':email', $entity->getIdentifier());
145 1
        $statement->bindValue(':fingerprint', $entity->getFingerprint());
146 1
        $statement->bindValue(':hash', $entity->getHash());
147 1
        $statement->bindValue(':created', time());
148
149 1
        $this->bindToken($statement, $entity);
150
151 1
        $statement->execute();
152
153 1
        $entity->setId($this->connection->lastInsertId());
154 1
    }
155
156
157 1
    private function updateIdentity(Entity\StandardIdentity $entity)
158
    {
159 1
        $sql = "UPDATE {$this->table}
160
                   SET hash = :hash,
161
                       status = :status,
162
                       used_on = :used,
163
                       expires_on = :expires,
164
                       token = :token,
165
                       token_action = :action,
166
                       token_expires_on = :token_eol,
167
                       token_payload = :payload
168
                 WHERE identity_id = :id";
169
170 1
        $statement = $this->connection->prepare($sql);
171
172 1
        $statement->bindValue(':id', $entity->getId());
173 1
        $statement->bindValue(':hash', $entity->getHash());
174 1
        $statement->bindValue(':status', $entity->getStatus());
175 1
        $statement->bindValue(':used', $entity->getLastUsed());
176 1
        $statement->bindValue(':expires', $entity->getExpiresOn());
177
178 1
        $this->bindToken($statement, $entity);
179
180 1
        $statement->execute();
181 1
    }
182
183
184 2
    private function bindToken(PDOStatement $statement, Entity\StandardIdentity $entity)
185
    {
186 2
        $statement->bindValue(':token', $entity->getToken());
187 2
        $statement->bindValue(':action', $entity->getTokenAction());
188 2
        $statement->bindValue(':token_eol', $entity->getTokenEndOfLife());
189 2
        $statement->bindValue(':payload', $entity->getTokenPayload());
190 2
    }
191
}
192