-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRatsit.php
206 lines (174 loc) · 5.25 KB
/
Ratsit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
declare(strict_types=1);
namespace JGI\Ratsit;
use Http\Client\HttpClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use JGI\Ratsit\Event\CompanyInformationResultEvent;
use JGI\Ratsit\Event\PersonInformationResultEvent;
use JGI\Ratsit\Event\PersonSearchResultEvent;
use JGI\Ratsit\Model\Company;
use JGI\Ratsit\Model\SearchResult;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Ratsit
{
/**
* @var HttpClient
*/
private $httpClient;
/**
* @var RequestFactory
*/
private $messageFactory;
/**
* @var array
*/
private $options = [];
/**
* @var Denormalizer
*/
private $denormalizer;
/**
* @var EventDispatcherInterface|null
*/
private $eventDispatcher;
/**
* @var array
*/
private static $defaultOptions = [
'url' => 'https://api.checkbiz.se/api/v1/',
'token' => '',
];
/**
* @param string $token
* @param array $options
*/
public function __construct(string $token, array $options = [])
{
$options['token'] = $token;
$this->setOptions($options);
}
/**
* @param HttpClient $httpClient
*/
public function setHttpClient(HttpClient $httpClient): void
{
$this->httpClient = $httpClient;
}
/**
* @param null|EventDispatcherInterface $eventDispatcher
*/
public function setEventDispatcher(?EventDispatcherInterface $eventDispatcher): void
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param array $options
*/
private function setOptions(array $options)
{
$this->options = self::$defaultOptions;
$this->options = array_merge($this->options, $options);
}
/**
* @return RequestFactory
*/
private function getMessageFactory()
{
if (!$this->messageFactory) {
$this->messageFactory = MessageFactoryDiscovery::find();
}
return $this->messageFactory;
}
/**
* @return Denormalizer
*/
private function getDenormalizer()
{
if (!$this->denormalizer) {
$this->denormalizer = new Denormalizer();
}
return $this->denormalizer;
}
/**
* @param string $method
* @param string $package
* @param array $parameters
*
* @return \Psr\Http\Message\ResponseInterface
* @throws \Exception
* @throws \Http\Client\Exception
*/
private function request(string $method, string $package, array $parameters = [])
{
$request = call_user_func_array(
[$this, 'buildRequestInstance'],
[$method, $package, $parameters]
);
return $this->httpClient->sendRequest($request);
}
/**
* @param string $method
* @param string $package
* @param array $parameters
*
* @return \Psr\Http\Message\RequestInterface
*/
public function buildRequestInstance(string $method, string $package, array $parameters)
{
$uri = $this->options['url'] . $method . '?' . http_build_query($parameters);
return $this->getMessageFactory()->createRequest('GET', $uri, [
'Authorization' => sprintf('Basic %s', $this->options['token']),
'package' => $package,
]);
}
/**
* @param string|null $ssn
*
* @return Model\Person
*/
public function findPersonBySocialSecurityNumber(?string $ssn)
{
$json = $this->request('personinformation', 'personinformation', ['ssn' => $ssn])->getBody()->getContents();
$person = $this->getDenormalizer()->denormalizerPersonInformation(json_decode($json, true));
if ($person && $this->eventDispatcher) {
$this->eventDispatcher->dispatch(
new PersonInformationResultEvent($person), PersonInformationResultEvent::NAME
);
}
return $person;
}
/**
* @param null|string $who
* @param null|string $where
*
* @return Model\Person[]|SearchResult
*/
public function searchPerson(?string $who, ?string $where = null)
{
$json = $this->request('personsearch', 'personsok', ['who' => $who, 'where' => $where])->getBody()->getContents();
$searchResult = $this->getDenormalizer()->denormalizerPersonSearch(json_decode($json, true));
if ($this->eventDispatcher) {
$this->eventDispatcher->dispatch(
new PersonSearchResultEvent($searchResult), PersonSearchResultEvent::NAME
);
}
return $searchResult;
}
/**
* @param string|null $organisationNumber
*
* @return Model\Company|null
*/
public function findCompanyByOrganisationNumber(?string $organisationNumber): ?Company
{
$json = $this->request('companyinformation', 'companyinformation', ['OrganizationNumber' => $organisationNumber])->getBody()->getContents();
$company = $this->getDenormalizer()->denormalizerCompanyInformation(json_decode($json, true));
if ($company && $this->eventDispatcher) {
$this->eventDispatcher->dispatch(
new CompanyInformationResultEvent($company), CompanyInformationResultEvent::NAME
);
}
return $company;
}
}