Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
60 / 70
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EUVATBffOnline
85.71% covered (warning)
85.71%
60 / 70
0.00% covered (danger)
0.00%
0 / 2
13.49
0.00% covered (danger)
0.00%
0 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 validate
83.33% covered (warning)
83.33%
25 / 30
0.00% covered (danger)
0.00%
0 / 1
6.17
 validateQualified
87.50% covered (warning)
87.50%
35 / 40
0.00% covered (danger)
0.00%
0 / 1
6.07
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Api\EUVAT
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\EUVAT;
16
17use phpOMS\Message\Http\HttpRequest;
18use phpOMS\Message\Http\RequestMethod;
19use phpOMS\Message\Http\Rest;
20use phpOMS\Uri\HttpUri;
21
22/**
23 * Check EU VAT.
24 *
25 * @package phpOMS\Api\EUVAT
26 * @license OMS License 2.0
27 * @link    https://jingga.app
28 * @since   1.0.0
29 */
30final class EUVATBffOnline implements EUVATInterface
31{
32    /**
33     * Constructor.
34     *
35     * @since 1.0.0
36     * @codeCoverageIgnore
37     */
38    private function __construct()
39    {
40    }
41
42    /**
43     * {@inheritdoc}
44     */
45    public static function validate(string $otherVAT, string $ownVAT = '') : array
46    {
47        $result = [
48            'status'  => -1,
49            'vat'     => 'B',
50            'name'    => '',
51            'city'    => '',
52            'postal'  => '',
53            'address' => '',
54            'body'    => '',
55        ];
56
57        if (empty($otherVAT) || empty($ownVAT)) {
58            return $result;
59        }
60
61        $request = new HttpRequest(
62            new HttpUri(
63                'https://evatr.bff-online.de/evatrRPC?UstId_1=' . $ownVAT . '&UstId_2=' . $otherVAT
64            )
65        );
66        $request->setMethod(RequestMethod::GET);
67
68        $matches = [];
69        try {
70            $body           = Rest::request($request)->getBody();
71            $result['body'] = $body;
72
73            \preg_match('/ErrorCode.*?(\d+)/s', $body, $matches);
74
75            switch ((int) ($matches[1] ?? 1)) {
76                case 200:
77                    $result['vat'] = 'A';
78                    break;
79                default:
80                    $result['vat'] = 'B';
81            }
82
83            $result['status'] = 0;
84        } catch (\Throwable $_) {
85            return $result;
86        }
87
88        return $result;
89    }
90
91    /**
92     * {@inheritdoc}
93     */
94    public static function validateQualified(
95        string $otherVAT,
96        string $ownVAT,
97        string $otherName,
98        string $otherCity,
99        string $otherPostal,
100        string $otherStreet
101    ) : array
102    {
103        $result = [
104            'status'  => -1,
105            'vat'     => 'B',
106            'name'    => 'C',
107            'city'    => 'C',
108            'postal'  => 'C',
109            'address' => 'C',
110            'body'    => '',
111        ];
112
113        if (empty($otherVAT) || empty($ownVAT)) {
114            return $result;
115        }
116
117        $request = new HttpRequest(new HttpUri(
118            'https://evatr.bff-online.de/evatrRPC?UstId_1=' . $ownVAT . '&UstId_2=' . $otherVAT . '&Firmenname=' . \urlencode($otherName) . '&Ort=' . \urlencode($otherCity) . '&PLZ=' . \urlencode($otherPostal) . '&Strasse=' . \urlencode($otherStreet))
119        );
120        $request->setMethod(RequestMethod::GET);
121
122        try {
123            $body           = Rest::request($request)->getBody();
124            $result['body'] = $body;
125
126            $matches = [];
127            \preg_match('/ErrorCode.*?(\d+)/s', $body, $matches);
128
129            switch ((int) ($matches[1] ?? 1)) {
130                case 200:
131                    $result['vat'] = 'A';
132                    break;
133                default:
134                    $result['vat'] = 'B';
135            }
136
137            $matches = [];
138            \preg_match('/Erg_PLZ.*?<string>(A|B|C|D)/s', $body, $matches);
139            $result['postal'] = $matches[1] ?? 'B';
140
141            $matches = [];
142            \preg_match('/Erg_Ort.*?<string>(A|B|C|D)/s', $body, $matches);
143            $result['city'] = $matches[1] ?? 'B';
144
145            $matches = [];
146            \preg_match('/Erg_Str.*?<string>(A|B|C|D)/s', $body, $matches);
147            $result['address'] = $matches[1] ?? 'B';
148
149            $matches = [];
150            \preg_match('/Erg_Name.*?<string>(A|B|C|D)/s', $body, $matches);
151            $result['name'] = $matches[1] ?? 'B';
152
153            $result['status'] = 0;
154        } catch (\Throwable $_) {
155            return $result;
156        }
157
158        return $result;
159    }
160}