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