Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreditSafe
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 6
650
0.00% covered (danger)
0.00%
0 / 1
 auth
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 findCompanies
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
210
 creditReport
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 investigate
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
 showInvestigations
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getInvestigation
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Api\CreditRating
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Api\CreditRating;
16
17use phpOMS\Message\Http\HttpRequest;
18use phpOMS\Message\Http\RequestMethod;
19use phpOMS\Message\Http\Rest;
20use phpOMS\Uri\HttpUri;
21
22/**
23 * Creditsafe Api.
24 *
25 * @package phpOMS\Api\CreditRating
26 * @license OMS License 2.0
27 * @link    https://jingga.app
28 * @since   1.0.0
29 */
30final class CreditSafe implements CreditRatingInterface
31{
32    /**
33     * CreditSafe API link
34     *
35     * @var string
36     * @since 1.0.0
37     */
38    public const API_URL = 'https://connect.creditsafe.com/v1';
39    //public const API_URL = 'https://connect.sandbox.creditsafe.com/v1';
40
41    /**
42     * {@inheritdoc}
43     */
44    public function auth(string $username, string $password) : string
45    {
46        $url = '/authenticate';
47
48        $request = new HttpRequest(new HttpUri(self::API_URL . $url));
49        $request->setMethod(RequestMethod::POST);
50
51        $response = Rest::request($request);
52
53        return $response->header->status === 200
54            ? ($response->getDataString('token') ?? '')
55            : '';
56    }
57
58    /**
59     * {@inheritdoc}
60     */
61    public function findCompanies(
62        string $token,
63        string $name = '',
64        string $address = '',
65        string $street = '',
66        string $city = '',
67        string $postal = '',
68        string $province = '',
69        string $phoneNo = '',
70        string $houseNo = '',
71        string $vatNo = '',
72        string $localRegistrationNo = '',
73        array $countries = [],
74        int $threshold = 0,
75    ) : array
76    {
77        $url = '/companies';
78        if ($threshold > 0) {
79            $url .= '/matches';
80        }
81
82        $request = new HttpRequest(new HttpUri(self::API_URL . $url));
83        $request->setMethod(RequestMethod::GET);
84
85        $request->header->set('Authorization', $token);
86
87        $request->setData('page', 1);
88        $request->setData('pageSize', 100);
89        $request->setData('language', 'en');
90
91        if ($threshold > 0) {
92            $request->setData('matchThreshold', $threshold);
93            $request->setData('country', \implode(',', $countries));
94        } else {
95            $request->setData('countries', empty($countries) ? 'PLC' : \implode(',', $countries));
96        }
97
98        if ($localRegistrationNo !== '') {
99            $request->setData('regNo', $localRegistrationNo);
100        }
101
102        if ($vatNo !== '') {
103            $request->setData('vatNo', $vatNo);
104        }
105
106        if ($name !== '') {
107            $request->setData('name', $name);
108        }
109
110        if ($address !== '') {
111            $request->setData('address', $address);
112        }
113
114        if ($street !== '') {
115            $request->setData('street', $street);
116        }
117
118        if ($province !== '') {
119            $request->setData('province', $province);
120        }
121
122        if ($postal !== '') {
123            $request->setData('postal', $postal);
124        }
125
126        if ($city !== '') {
127            $request->setData('city', $city);
128        }
129
130        if ($houseNo !== '') {
131            $request->setData('houseNo', $houseNo);
132        }
133
134        if ($phoneNo !== '') {
135            $request->setData('phoneNo', $phoneNo);
136        }
137
138        $response = Rest::request($request);
139
140        return $response->getDataArray('companies') ?? ($response->getDataArray('matchedCompanies') ?? []);
141    }
142
143    /**
144     * {@inheritdoc}
145     */
146    public function creditReport(string $token, string $id, string $template = 'full', string $language = 'en') : array
147    {
148        $url = '/companies/' . $id;
149
150        $request = new HttpRequest(new HttpUri(self::API_URL . $url));
151        $request->setMethod(RequestMethod::GET);
152
153        $request->header->set('Authorization', $token);
154
155        $request->setData('connectId', $id);
156        $request->setData('language', $language);
157        $request->setData('template', $template);
158
159        $response = Rest::request($request);
160
161        return $response->getDataArray('report') ?? [];
162    }
163
164    /**
165     * {@inheritdoc}
166     */
167    public function investigate(
168        string $token,
169        string $ownName = '',
170        string $ownCompanyName = '',
171        string $ownCompanyRegistrationNo = '',
172        string $ownEmail = '',
173        string $name = '',
174        string $address = '',
175        string $street = '',
176        string $city = '',
177        string $postal = '',
178        string $province = '',
179        string $phoneNo = '',
180        string $houseNo = '',
181        string $vatNo = '',
182        string $localRegistrationNo = '',
183        string $country = ''
184    ) : string
185    {
186        $url = '/freshinvestigations';
187
188        $request = new HttpRequest(new HttpUri(self::API_URL . $url));
189        $request->setMethod(RequestMethod::POST);
190
191        $request->header->set('Authorization', $token);
192
193        $request->setData('contactInfo', [
194            'name'    => $ownName,
195            'company' => [
196                'name'   => $ownCompanyName,
197                'number' => $ownCompanyRegistrationNo,
198            ],
199            'emailAddress'   => $ownEmail,
200            'searchCriteria' => [
201                'name'    => $name,
202                'address' => [
203                    'simple'   => empty($address) ? null : $address,
204                    'postcode' => empty($postal) ? null : $postal,
205                    'city'     => empty($city) ? null : $city,
206                ],
207                'regNo'       => empty(${$localRegistrationNo}) ? null : ${$localRegistrationNo},
208                'vatNo'       => empty($vatNo) ? null : $vatNo,
209                'countryCode' => $country,
210            ],
211        ]);
212
213        $response = Rest::request($request);
214
215        return $response->getDataString('orderID') ?? '';
216    }
217
218    /**
219     * {@inheritdoc}
220     */
221    public function showInvestigations(string $token, \DateTime $start) : array
222    {
223        $url = '/freshinvestigations';
224
225        $request = new HttpRequest(new HttpUri(self::API_URL . $url));
226        $request->setMethod(RequestMethod::GET);
227
228        $request->header->set('Authorization', $token);
229
230        $request->setData('page', 1);
231        $request->setData('pageSize', 100);
232        $request->setData('createdSince', $start->format('c'));
233
234        $response = Rest::request($request);
235
236        return $response->getDataArray('orders') ?? [];
237    }
238
239    /**
240     * {@inheritdoc}
241     */
242    public function getInvestigation(string $token, string $id) : array
243    {
244        $url = '/freshinvestigations/' . $id;
245
246        $request = new HttpRequest(new HttpUri(self::API_URL . $url));
247        $request->setMethod(RequestMethod::GET);
248
249        $request->header->set('Authorization', $token);
250
251        $request->setData('orderId', $id);
252
253        $response = Rest::request($request);
254
255        return $response->toArray();
256    }
257}