|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Palladium\Service; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class for finding indentities based on various conditions |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use Palladium\Mapper as Mapper; |
|
11
|
|
|
use Palladium\Entity as Entity; |
|
12
|
|
|
use Palladium\Exception\UserNotFound; |
|
13
|
|
|
use Palladium\Exception\IdentityNotFound; |
|
14
|
|
|
|
|
15
|
|
|
use Palladium\Contract\CanCreateMapper; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
class Search |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
private $mapperFactory; |
|
23
|
|
|
private $logger; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
7 |
|
public function __construct(CanCreateMapper $mapperFactory, LoggerInterface $logger) |
|
27
|
|
|
{ |
|
28
|
7 |
|
$this->mapperFactory = $mapperFactory; |
|
29
|
7 |
|
$this->logger = $logger; |
|
30
|
7 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $identifier |
|
35
|
|
|
* |
|
36
|
|
|
* @return Palladium\Entity\EmailIdentity |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function findEmailIdenityByIdentifier($identifier) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$identity = new Entity\EmailIdentity; |
|
41
|
2 |
|
$identity->setIdentifier($identifier); |
|
42
|
|
|
|
|
43
|
2 |
|
$mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class); |
|
44
|
2 |
|
$mapper->fetch($identity); |
|
45
|
|
|
|
|
46
|
2 |
View Code Duplication |
if ($identity->getId() === null) { |
|
|
|
|
|
|
47
|
1 |
|
$this->logger->warning('identity not found', [ |
|
48
|
|
|
'input' => [ |
|
49
|
1 |
|
'identifier' => $identifier, |
|
50
|
|
|
], |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
1 |
|
throw new IdentityNotFound; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
1 |
|
return $identity; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $token |
|
62
|
|
|
* @param int $action |
|
63
|
|
|
* |
|
64
|
|
|
* @return Palladium\Entity\EmailIdentity |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function findEmailIdenityByToken($token, $action = Entity\Identity::ACTION_ANY) |
|
67
|
|
|
{ |
|
68
|
2 |
|
$identity = new Entity\EmailIdentity; |
|
69
|
|
|
|
|
70
|
2 |
|
$identity->setToken($token); |
|
71
|
2 |
|
$identity->setTokenAction($action); |
|
72
|
2 |
|
$identity->setTokenEndOfLife(time()); |
|
73
|
|
|
|
|
74
|
2 |
|
$mapper = $this->mapperFactory->create(Mapper\Identity::class); |
|
75
|
2 |
|
$mapper->fetch($identity); |
|
76
|
|
|
|
|
77
|
2 |
View Code Duplication |
if ($identity->getId() === null) { |
|
|
|
|
|
|
78
|
1 |
|
$this->logger->warning('identity not found', [ |
|
79
|
|
|
'input' => [ |
|
80
|
1 |
|
'token' => $token, |
|
81
|
|
|
], |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
1 |
|
throw new IdentityNotFound; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $identity; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $accountId |
|
93
|
|
|
* @param string $series |
|
94
|
|
|
* |
|
95
|
|
|
* @return Palladium\Entity\CookieIdentity |
|
96
|
|
|
*/ |
|
97
|
1 |
|
public function findCookieIdenity($accountId, $series) |
|
98
|
|
|
{ |
|
99
|
1 |
|
$cookie = new Entity\CookieIdentity; |
|
100
|
1 |
|
$cookie->setStatus(Entity\Identity::STATUS_ACTIVE); |
|
101
|
1 |
|
$cookie->setAccountId($accountId); |
|
102
|
1 |
|
$cookie->setSeries($series); |
|
103
|
|
|
|
|
104
|
1 |
|
$mapper = $this->mapperFactory->create(Mapper\CookieIdentity::class); |
|
105
|
1 |
|
$mapper->fetch($cookie); |
|
106
|
|
|
|
|
107
|
1 |
|
return $cookie; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Palladium\Entity\IdentityCollection |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE) |
|
115
|
|
|
{ |
|
116
|
1 |
|
$collection = new Entity\IdentityCollection; |
|
117
|
1 |
|
$collection->forAccountId($accountId); |
|
118
|
1 |
|
$collection->forType($type); |
|
119
|
|
|
|
|
120
|
1 |
|
return $this->fetchIdentitiesByStatus($collection, $status); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return Palladium\Entity\IdentityCollection |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE) |
|
128
|
|
|
{ |
|
129
|
1 |
|
$collection = new Entity\IdentityCollection; |
|
130
|
1 |
|
$collection->forParentId($parentId); |
|
131
|
|
|
|
|
132
|
1 |
|
return $this->fetchIdentitiesByStatus($collection, $status); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @return Palladium\Entity\IdentityCollection |
|
138
|
|
|
*/ |
|
139
|
2 |
|
private function fetchIdentitiesByStatus(Entity\IdentityCollection $collection, $status) |
|
140
|
|
|
{ |
|
141
|
2 |
|
$collection->forStatus($status); |
|
142
|
|
|
|
|
143
|
2 |
|
$mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class); |
|
144
|
2 |
|
$mapper->fetch($collection); |
|
145
|
|
|
|
|
146
|
2 |
|
return $collection; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
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.