|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Mapper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SQL code repsonsible for locating all of the identities, that have been associated |
|
7
|
|
|
* to a given account and discarding them in bulk. |
|
8
|
|
|
* Used mostly in case of password reset or, if cookie has been compromised. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Palladium\Component\SqlMapper; |
|
13
|
|
|
use Palladium\Entity as Entity; |
|
14
|
|
|
|
|
15
|
|
|
class IdentityCollection extends SqlMapper |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param Entity\IdentityCollection $collection |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function store(Entity\IdentityCollection $collection) |
|
22
|
|
|
{ |
|
23
|
1 |
|
$sql = "UPDATE {$this->table} |
|
24
|
|
|
SET status = :status |
|
25
|
1 |
|
WHERE identity_id = :id"; |
|
26
|
1 |
|
$statement = $this->connection->prepare($sql); |
|
27
|
|
|
|
|
28
|
1 |
|
foreach ($collection as $entity) { |
|
29
|
1 |
|
$statement->execute([ |
|
30
|
1 |
|
':id' => $entity->getId(), |
|
31
|
1 |
|
':status' => $entity->getStatus(), |
|
32
|
|
|
]); |
|
33
|
|
|
} |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Entity\IdentityCollection $collection |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public function fetch(Entity\IdentityCollection $collection) |
|
41
|
|
|
{ |
|
42
|
2 |
|
if ($collection->getParentId() !== null) { |
|
43
|
1 |
|
$this->fetchByParent($collection); |
|
44
|
1 |
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
$this->fetchByAccount($collection); |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
1 |
View Code Duplication |
private function fetchByAccount(Entity\IdentityCollection $collection) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$sql = "SELECT identity_id AS id |
|
54
|
1 |
|
FROM {$this->table} |
|
55
|
|
|
WHERE status = :status |
|
56
|
|
|
AND account_id = :account |
|
57
|
1 |
|
AND type = :type"; |
|
58
|
|
|
|
|
59
|
1 |
|
$statement = $this->connection->prepare($sql); |
|
60
|
|
|
|
|
61
|
1 |
|
$statement->bindValue(':account', $collection->getAccountId()); |
|
62
|
1 |
|
$statement->bindValue(':status', $collection->getStatus()); |
|
63
|
1 |
|
$statement->bindValue(':type', $collection->getType()); |
|
64
|
|
|
|
|
65
|
1 |
|
$statement->execute(); |
|
66
|
|
|
|
|
67
|
1 |
|
foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $parameters) { |
|
68
|
1 |
|
$collection->addBlueprint($parameters); |
|
69
|
|
|
} |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
1 |
|
private function fetchByParent(Entity\IdentityCollection $collection) |
|
74
|
|
|
{ |
|
75
|
|
|
$sql = "SELECT identity_id AS id |
|
76
|
1 |
|
FROM {$this->table} |
|
77
|
|
|
WHERE status = :status |
|
78
|
1 |
|
AND parent_id = :parent"; |
|
79
|
|
|
|
|
80
|
1 |
|
$statement = $this->connection->prepare($sql); |
|
81
|
|
|
|
|
82
|
1 |
|
$statement->bindValue(':parent', $collection->getParentId()); |
|
83
|
1 |
|
$statement->bindValue(':status', $collection->getStatus()); |
|
84
|
|
|
|
|
85
|
1 |
|
$statement->execute(); |
|
86
|
|
|
|
|
87
|
1 |
|
foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $parameters) { |
|
88
|
1 |
|
$collection->addBlueprint($parameters); |
|
89
|
|
|
} |
|
90
|
1 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
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.