|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the haveibeenpwned-bundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2019 WEBEWEB |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WBW\Bundle\HaveIBeenPwnedBundle\EventListener; |
|
13
|
|
|
|
|
14
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\AbstractEvent; |
|
15
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachedAccountEvent; |
|
16
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachesEvent; |
|
17
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachEvent; |
|
18
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\DataClassesEvent; |
|
19
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\PasteAccountEvent; |
|
20
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\RangeEvent; |
|
21
|
|
|
use WBW\Library\HaveIBeenPwned\Exception\APIException; |
|
22
|
|
|
use WBW\Library\HaveIBeenPwned\Factory\RequestFactory; |
|
23
|
|
|
use WBW\Library\HaveIBeenPwned\Model\AbstractRequest; |
|
24
|
|
|
use WBW\Library\HaveIBeenPwned\Model\AbstractResponse; |
|
25
|
|
|
use WBW\Library\HaveIBeenPwned\Provider\APIProviderV2; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* HaveIBeenPwned event listener. |
|
29
|
|
|
* |
|
30
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
31
|
|
|
* @package WBW\Bundle\HaveIBeenPwnedBundle\EventListener |
|
32
|
|
|
*/ |
|
33
|
|
|
class HaveIBeenPwnedEventListener { |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Service name. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
const SERVICE_NAME = "wbw.haveibeenpwned.event_listener"; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* API provider. |
|
44
|
|
|
* |
|
45
|
|
|
* @var APIProviderV2 |
|
46
|
|
|
*/ |
|
47
|
|
|
private $apiProvider; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct() { |
|
53
|
|
|
$this->setApiProvider(new APIProviderV2()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Before return an event. |
|
58
|
|
|
* |
|
59
|
|
|
* @param AbstractEvent $event The event. |
|
60
|
|
|
* @param AbstractRequest $request The request. |
|
61
|
|
|
* @param AbstractResponse $response The response. |
|
62
|
|
|
* @return AbstractEvent Returns the event. |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function beforeReturnEvent(AbstractEvent $event, AbstractRequest $request, AbstractResponse $response) { |
|
65
|
|
|
|
|
66
|
|
|
$event->setRequest($request); |
|
67
|
|
|
$event->setResponse($response); |
|
68
|
|
|
|
|
69
|
|
|
return $event; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the API provider. |
|
74
|
|
|
* |
|
75
|
|
|
* @return APIProviderV2 Returns the API provider. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getApiProvider() { |
|
78
|
|
|
return $this->apiProvider; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* On breach. |
|
83
|
|
|
* |
|
84
|
|
|
* @param BreachEvent $event The breach event. |
|
85
|
|
|
* @return BreachEvent Returns the breach event. |
|
86
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
|
87
|
|
|
*/ |
|
88
|
|
|
public function onBreach(BreachEvent $event) { |
|
89
|
|
|
|
|
90
|
|
|
$request = RequestFactory::newBreachRequest($event->getBreach()); |
|
91
|
|
|
$response = $this->getApiProvider()->breach($request); |
|
92
|
|
|
|
|
93
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* On breached account. |
|
98
|
|
|
* |
|
99
|
|
|
* @param BreachedAccountEvent $event The breached account event. |
|
100
|
|
|
* @return BreachedAccountEvent Returns the breached account event. |
|
101
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function onBreachedAccount(BreachedAccountEvent $event) { |
|
104
|
|
|
|
|
105
|
|
|
$request = RequestFactory::newBreachedAccountRequest($event->getBreachedAccount()); |
|
106
|
|
|
$response = $this->getApiProvider()->breachedAccount($request); |
|
107
|
|
|
|
|
108
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* On breaches. |
|
113
|
|
|
* |
|
114
|
|
|
* @param BreachesEvent $event The breaches event. |
|
115
|
|
|
* @return BreachesEvent Returns the breaches event. |
|
116
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function onBreaches(BreachesEvent $event) { |
|
119
|
|
|
|
|
120
|
|
|
$request = RequestFactory::newBreachesRequest($event->getBreaches()); |
|
121
|
|
|
$response = $this->getApiProvider()->breaches($request); |
|
122
|
|
|
|
|
123
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* On data classes. |
|
128
|
|
|
* |
|
129
|
|
|
* @param DataClassesEvent $event The data classes event. |
|
130
|
|
|
* @return DataClassesEvent Returns the data classes event. |
|
131
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
|
132
|
|
|
*/ |
|
133
|
|
|
public function onDataClasses(DataClassesEvent $event) { |
|
134
|
|
|
|
|
135
|
|
|
$request = RequestFactory::newDataClassesRequest(); |
|
136
|
|
|
$response = $this->getApiProvider()->dataClasses($request); |
|
137
|
|
|
|
|
138
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* On paste account. |
|
143
|
|
|
* |
|
144
|
|
|
* @param PasteAccountEvent $event The paste account event. |
|
145
|
|
|
* @return PasteAccountEvent Returns the paste account event. |
|
146
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
|
147
|
|
|
*/ |
|
148
|
|
|
public function onPasteAccount(PasteAccountEvent $event) { |
|
149
|
|
|
|
|
150
|
|
|
$request = RequestFactory::newPasteAccountRequest($event->getPasteAccount()); |
|
151
|
|
|
$response = $this->getApiProvider()->pasteAccount($request); |
|
152
|
|
|
|
|
153
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* On range. |
|
158
|
|
|
* |
|
159
|
|
|
* @param RangeEvent $event The breach event. |
|
160
|
|
|
* @return RangeEvent Returns the breach event. |
|
161
|
|
|
* @throws APIException Throws an API exception if an error occurs. |
|
162
|
|
|
*/ |
|
163
|
|
|
public function onRange(RangeEvent $event) { |
|
164
|
|
|
|
|
165
|
|
|
$request = RequestFactory::newRangeRequest($event->getRange()); |
|
166
|
|
|
$response = $this->getApiProvider()->range($request); |
|
167
|
|
|
|
|
168
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set the API provider. |
|
173
|
|
|
* |
|
174
|
|
|
* @param APIProviderV2 $apiProvider The API provider. |
|
175
|
|
|
* @return HaveIBeenPwnedEventListener Returns this event listener. |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function setApiProvider(APIProviderV2 $apiProvider) { |
|
178
|
|
|
$this->apiProvider = $apiProvider; |
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|