|
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
|
|
|
|
|
14
|
|
|
class CookieIdentity extends DataMapper |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Entity\CookieIdentity $entity |
|
19
|
|
|
*/ |
|
20
|
|
View Code Duplication |
public function fetch(Entity\CookieIdentity $entity) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
$sql = "SELECT identity_id AS id, |
|
23
|
|
|
parent_id AS parentId, |
|
24
|
|
|
hash AS hash, |
|
25
|
|
|
expires_on AS expiresOn |
|
26
|
|
|
FROM {$this->table} AS Identities |
|
27
|
|
|
WHERE type = :type |
|
28
|
|
|
AND account_id = :account |
|
29
|
|
|
AND identifier = :identifier |
|
30
|
|
|
AND fingerprint = :fingerprint |
|
31
|
|
|
AND status = :status"; |
|
32
|
|
|
|
|
33
|
|
|
$statement = $this->connection->prepare($sql); |
|
34
|
|
|
$this->bindCommonParameters($statement, $entity); |
|
35
|
|
|
|
|
36
|
|
|
$statement->execute(); |
|
37
|
|
|
|
|
38
|
|
|
$data = $statement->fetch(); |
|
39
|
|
|
|
|
40
|
|
|
if ($data) { |
|
41
|
|
|
$this->applyValues($entity, $data); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
1 |
|
private function bindCommonParameters(PDOStatement $statement, Entity\CookieIdentity $entity) |
|
47
|
|
|
{ |
|
48
|
1 |
|
$statement->bindValue(':type', $entity->getType()); |
|
49
|
1 |
|
$statement->bindValue(':status', $entity->getStatus()); |
|
50
|
1 |
|
$statement->bindValue(':account', $entity->getAccountId()); |
|
51
|
1 |
|
$statement->bindValue(':identifier', $entity->getSeries()); |
|
52
|
1 |
|
$statement->bindValue(':fingerprint', $entity->getFingerprint()); |
|
53
|
1 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
2 |
|
public function store(Entity\CookieIdentity $entity) |
|
58
|
|
|
{ |
|
59
|
2 |
|
if ($entity->getId() === null) { |
|
60
|
1 |
|
$this->createCookie($entity); |
|
61
|
1 |
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$this->updateCookie($entity); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
View Code Duplication |
private function createCookie(Entity\CookieIdentity $entity) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
1 |
|
$sql = "INSERT INTO {$this->table} |
|
71
|
|
|
(account_id, parent_id, type, status, identifier, fingerprint, hash, created_on, expires_on) |
|
72
|
|
|
VALUES (:account, :parent, :type, :status, :identifier, :fingerprint, :hash, :created, :expires)"; |
|
73
|
|
|
|
|
74
|
1 |
|
$statement = $this->connection->prepare($sql); |
|
75
|
|
|
|
|
76
|
1 |
|
$statement->bindValue(':parent', $entity->getParentId()); |
|
77
|
1 |
|
$statement->bindValue(':hash', $entity->getHash()); |
|
78
|
1 |
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
|
79
|
1 |
|
$statement->bindValue(':created', time()); |
|
80
|
|
|
|
|
81
|
1 |
|
$this->bindCommonParameters($statement, $entity); |
|
82
|
|
|
|
|
83
|
1 |
|
$statement->execute(); |
|
84
|
|
|
|
|
85
|
1 |
|
$entity->setId($this->connection->lastInsertId()); |
|
86
|
1 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
1 |
|
private function updateCookie(Entity\CookieIdentity $entity) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$active = Entity\Identity::STATUS_ACTIVE; |
|
92
|
|
|
|
|
93
|
1 |
|
$sql = "UPDATE {$this->table} |
|
94
|
|
|
SET status = :status, |
|
95
|
|
|
hash = :hash, |
|
96
|
|
|
used_on = :used, |
|
97
|
|
|
expires_on = :expires |
|
98
|
|
|
WHERE identity_id = :id |
|
99
|
1 |
|
AND status = {$active}"; |
|
100
|
|
|
|
|
101
|
1 |
|
$statement = $this->connection->prepare($sql); |
|
102
|
|
|
|
|
103
|
1 |
|
$statement->bindValue(':id', $entity->getId()); |
|
104
|
1 |
|
$statement->bindValue(':status', $entity->getStatus()); |
|
105
|
1 |
|
$statement->bindValue(':hash', $entity->getHash()); |
|
106
|
1 |
|
$statement->bindValue(':expires', $entity->getExpiresOn()); |
|
107
|
1 |
|
$statement->bindValue(':used', time()); |
|
108
|
|
|
|
|
109
|
1 |
|
$statement->execute(); |
|
110
|
1 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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.