|
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
|
2 |
|
public function findEmailIdenityByIdentifier($identifier) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$identity = new Entity\EmailIdentity; |
|
39
|
2 |
|
$identity->setIdentifier($identifier); |
|
40
|
|
|
|
|
41
|
2 |
|
$mapper = $this->mapperFactory->create(Mapper\EmailIdentity::class); |
|
42
|
2 |
|
$mapper->fetch($identity); |
|
43
|
|
|
|
|
44
|
2 |
View Code Duplication |
if ($identity->getId() === null) { |
|
|
|
|
|
|
45
|
1 |
|
$this->logger->warning('identity not found', [ |
|
46
|
|
|
'input' => [ |
|
47
|
1 |
|
'identifier' => $identifier, |
|
48
|
|
|
], |
|
49
|
|
|
]); |
|
50
|
|
|
|
|
51
|
1 |
|
throw new IdentityNotFound; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $identity; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $token |
|
60
|
|
|
* @param int $action |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function findEmailIdenityByToken($token, $action = Entity\Identity::ACTION_ANY) |
|
63
|
|
|
{ |
|
64
|
2 |
|
$identity = new Entity\EmailIdentity; |
|
65
|
|
|
|
|
66
|
2 |
|
$identity->setToken($token); |
|
67
|
2 |
|
$identity->setTokenAction($action); |
|
68
|
2 |
|
$identity->setTokenEndOfLife(time()); |
|
69
|
|
|
|
|
70
|
2 |
|
$mapper = $this->mapperFactory->create(Mapper\Identity::class); |
|
71
|
2 |
|
$mapper->fetch($identity); |
|
72
|
|
|
|
|
73
|
2 |
View Code Duplication |
if ($identity->getId() === null) { |
|
|
|
|
|
|
74
|
1 |
|
$this->logger->warning('identity not found', [ |
|
75
|
|
|
'input' => [ |
|
76
|
1 |
|
'token' => $token, |
|
77
|
|
|
], |
|
78
|
|
|
]); |
|
79
|
|
|
|
|
80
|
1 |
|
throw new IdentityNotFound; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
return $identity; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param int $accountId |
|
89
|
|
|
* @param string $series |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function findCookieIdenity($accountId, $series) |
|
92
|
|
|
{ |
|
93
|
1 |
|
$cookie = new Entity\CookieIdentity; |
|
94
|
1 |
|
$cookie->setStatus(Entity\Identity::STATUS_ACTIVE); |
|
95
|
1 |
|
$cookie->setAccountId($accountId); |
|
96
|
1 |
|
$cookie->setSeries($series); |
|
97
|
|
|
|
|
98
|
1 |
|
$mapper = $this->mapperFactory->create(Mapper\CookieIdentity::class); |
|
99
|
1 |
|
$mapper->fetch($cookie); |
|
100
|
|
|
|
|
101
|
1 |
|
return $cookie; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
1 |
View Code Duplication |
public function findIdentitiesByAccountId($accountId, $type = Entity\Identity::TYPE_ANY, $status = Entity\Identity::STATUS_ACTIVE) |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
1 |
|
$collection = new Entity\IdentityCollection; |
|
108
|
1 |
|
$collection->forAccountId($accountId); |
|
109
|
1 |
|
$collection->forType($type); |
|
110
|
1 |
|
$collection->forStatus($status); |
|
111
|
|
|
|
|
112
|
1 |
|
$mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class); |
|
113
|
1 |
|
$mapper->fetch($collection); |
|
114
|
|
|
|
|
115
|
1 |
|
return $collection; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
1 |
View Code Duplication |
public function findIdentitiesByParentId($parentId, $status = Entity\Identity::STATUS_ACTIVE) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
1 |
|
$collection = new Entity\IdentityCollection; |
|
122
|
1 |
|
$collection->forParentId($parentId); |
|
123
|
1 |
|
$collection->forStatus($status); |
|
124
|
|
|
|
|
125
|
1 |
|
$mapper = $this->mapperFactory->create(Mapper\IdentityCollection::class); |
|
126
|
1 |
|
$mapper->fetch($collection); |
|
127
|
|
|
|
|
128
|
1 |
|
return $collection; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
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.