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 GuzzleHttp\Exception\GuzzleException; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\AbstractEvent; |
18
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachedAccountEvent; |
19
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachesEvent; |
20
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\BreachEvent; |
21
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\DataClassesEvent; |
22
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\PasteAccountEvent; |
23
|
|
|
use WBW\Bundle\HaveIBeenPwnedBundle\Event\RangeEvent; |
24
|
|
|
use WBW\Library\HaveIBeenPwned\Factory\RequestFactory; |
25
|
|
|
use WBW\Library\HaveIBeenPwned\Provider\APIv2Provider; |
26
|
|
|
use WBW\Library\HaveIBeenPwned\Request\AbstractRequest; |
27
|
|
|
use WBW\Library\HaveIBeenPwned\Response\AbstractResponse; |
28
|
|
|
use WBW\Library\Logger\LoggerTrait; |
29
|
|
|
use WBW\Library\Provider\Exception\ApiException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* HaveIBeenPwned event listener. |
33
|
|
|
* |
34
|
|
|
* @author webeweb <https://github.com/webeweb> |
35
|
|
|
* @package WBW\Bundle\HaveIBeenPwnedBundle\EventListener |
36
|
|
|
*/ |
37
|
|
|
class HaveIBeenPwnedEventListener { |
38
|
|
|
|
39
|
|
|
use LoggerTrait; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Service name. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
const SERVICE_NAME = "wbw.haveibeenpwned.event_listener"; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* API provider. |
50
|
|
|
* |
51
|
|
|
* @var APIv2Provider |
52
|
|
|
*/ |
53
|
|
|
private $apiProvider; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
* |
58
|
|
|
* @param LoggerInterface $logger The logger. |
59
|
|
|
*/ |
60
|
|
|
public function __construct(LoggerInterface $logger) { |
61
|
|
|
$this->setApiProvider(new APIv2Provider($logger)); |
62
|
|
|
$this->setLogger($logger); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Before return an event. |
67
|
|
|
* |
68
|
|
|
* @param AbstractEvent $event The event. |
69
|
|
|
* @param AbstractRequest $request The request. |
70
|
|
|
* @param AbstractResponse $response The response. |
71
|
|
|
* @return AbstractEvent Returns the event. |
72
|
|
|
*/ |
73
|
|
|
protected function beforeReturnEvent(AbstractEvent $event, AbstractRequest $request, AbstractResponse $response): AbstractEvent { |
74
|
|
|
|
75
|
|
|
$event->setRequest($request); |
76
|
|
|
$event->setResponse($response); |
77
|
|
|
|
78
|
|
|
return $event; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the API provider. |
83
|
|
|
* |
84
|
|
|
* @return APIv2Provider Returns the API provider. |
85
|
|
|
*/ |
86
|
|
|
public function getApiProvider(): APIv2Provider { |
87
|
|
|
return $this->apiProvider; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* On breach. |
92
|
|
|
* |
93
|
|
|
* @param BreachEvent $event The breach event. |
94
|
|
|
* @return BreachEvent Returns the breach event. |
95
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
96
|
|
|
* @throws GuzzleException Throws a GUzzle exception if an error occurs. |
97
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
98
|
|
|
*/ |
99
|
|
|
public function onBreach(BreachEvent $event): BreachEvent { |
100
|
|
|
|
101
|
|
|
$request = RequestFactory::newBreachRequest($event->getBreach()); |
102
|
|
|
$response = $this->getApiProvider()->breach($request); |
103
|
|
|
|
104
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* On breached account. |
109
|
|
|
* |
110
|
|
|
* @param BreachedAccountEvent $event The breached account event. |
111
|
|
|
* @return BreachedAccountEvent Returns the breached account event. |
112
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
113
|
|
|
* @throws GuzzleException Throws a GUzzle exception if an error occurs. |
114
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
115
|
|
|
*/ |
116
|
|
|
public function onBreachedAccount(BreachedAccountEvent $event): BreachedAccountEvent { |
117
|
|
|
|
118
|
|
|
$request = RequestFactory::newBreachedAccountRequest($event->getBreachedAccount()); |
119
|
|
|
$response = $this->getApiProvider()->breachedAccount($request); |
120
|
|
|
|
121
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* On breaches. |
126
|
|
|
* |
127
|
|
|
* @param BreachesEvent $event The breaches event. |
128
|
|
|
* @return BreachesEvent Returns the breaches event. |
129
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
130
|
|
|
* @throws GuzzleException Throws a GUzzle exception if an error occurs. |
131
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
132
|
|
|
*/ |
133
|
|
|
public function onBreaches(BreachesEvent $event): BreachesEvent { |
134
|
|
|
|
135
|
|
|
$request = RequestFactory::newBreachesRequest($event->getBreaches()); |
136
|
|
|
$response = $this->getApiProvider()->breaches($request); |
137
|
|
|
|
138
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* On data classes. |
143
|
|
|
* |
144
|
|
|
* @param DataClassesEvent $event The data classes event. |
145
|
|
|
* @return DataClassesEvent Returns the data classes event. |
146
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
147
|
|
|
* @throws GuzzleException Throws a GUzzle exception if an error occurs. |
148
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
149
|
|
|
*/ |
150
|
|
|
public function onDataClasses(DataClassesEvent $event): DataClassesEvent { |
151
|
|
|
|
152
|
|
|
$request = RequestFactory::newDataClassesRequest(); |
153
|
|
|
$response = $this->getApiProvider()->dataClasses($request); |
154
|
|
|
|
155
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* On paste account. |
160
|
|
|
* |
161
|
|
|
* @param PasteAccountEvent $event The paste account event. |
162
|
|
|
* @return PasteAccountEvent Returns the paste account event. |
163
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
164
|
|
|
* @throws GuzzleException Throws a GUzzle exception if an error occurs. |
165
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
166
|
|
|
*/ |
167
|
|
|
public function onPasteAccount(PasteAccountEvent $event): PasteAccountEvent { |
168
|
|
|
|
169
|
|
|
$request = RequestFactory::newPasteAccountRequest($event->getPasteAccount()); |
170
|
|
|
$response = $this->getApiProvider()->pasteAccount($request); |
|
|
|
|
171
|
|
|
|
172
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* On range. |
177
|
|
|
* |
178
|
|
|
* @param RangeEvent $event The breach event. |
179
|
|
|
* @return RangeEvent Returns the breach event. |
180
|
|
|
* @throws InvalidArgumentException Throws an invalid argument exception if a parameter is missing. |
181
|
|
|
* @throws GuzzleException Throws a GUzzle exception if an error occurs. |
182
|
|
|
* @throws ApiException Throws an API exception if an error occurs. |
183
|
|
|
*/ |
184
|
|
|
public function onRange(RangeEvent $event): RangeEvent { |
185
|
|
|
|
186
|
|
|
$request = RequestFactory::newRangeRequest($event->getRange()); |
187
|
|
|
$response = $this->getApiProvider()->range($request); |
188
|
|
|
|
189
|
|
|
return $this->beforeReturnEvent($event, $request, $response); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the API provider. |
194
|
|
|
* |
195
|
|
|
* @param APIv2Provider $apiProvider The API provider. |
196
|
|
|
* @return HaveIBeenPwnedEventListener Returns this event listener. |
197
|
|
|
*/ |
198
|
|
|
protected function setApiProvider(APIv2Provider $apiProvider): HaveIBeenPwnedEventListener { |
199
|
|
|
$this->apiProvider = $apiProvider; |
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.