Issues (2)

src/Response/ValidVatNumber.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Th3Mouk\VatLayer\Response;
6
7
use Psr\Http\Message\ResponseInterface;
8
use Th3Mouk\VatLayer\Exception\BadResponse;
9
use Th3Mouk\VatLayer\Exception\InvalidVatNumber;
10
use Webmozart\Assert\Assert;
11
12
/** @psalm-immutable  */
13
final class ValidVatNumber
14
{
15
    public string $query;
16
    public string $country_code;
17
    public string $vat_vumber;
18
    public string $company_name;
19
    public string $company_address;
20
21
    private function __construct(
22
        string $query,
23
        string $country_code,
24
        string $vat_vumber,
25
        string $company_name,
26
        string $company_address
27
    ) {
28
        $this->query           = $query;
29
        $this->country_code    = $country_code;
30
        $this->vat_vumber      = $vat_vumber;
31
        $this->company_name    = $company_name;
32
        $this->company_address = $company_address;
33
    }
34
35
    public static function create(
36
        string $query,
37
        string $country_code,
38
        string $vat_vumber,
39
        string $company_name,
40
        string $company_address
41
    ): self {
42
        return new self($query, $country_code, $vat_vumber, $company_name, $company_address);
43
    }
44
45
    /**
46
     * @throws BadResponse
47
     * @throws InvalidVatNumber
48
     *
49
     * @psalm-pure
50
     */
51
    public static function createFromResponse(ResponseInterface $response): self
52
    {
53
        try {
54
            /** @var array<string, scalar> $data */
55
            $data = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
56
57
            Assert::keyExists($data, 'valid');
58
            $is_valid = (bool) $data['valid'];
59
60
            if (!$is_valid) {
61
                throw new InvalidVatNumber();
62
            }
63
64
            $query           = $data['query'] ?? '';
65
            $country_code    = $data['country_code'] ?? '';
66
            $vat_vumber      = $data['vat_number'] ?? '';
67
            $company_name    = $data['company_name'] ?? '';
68
            $company_address = $data['company_address'] ?? '';
69
70
            Assert::stringNotEmpty($query);
71
            Assert::stringNotEmpty($country_code);
72
            Assert::stringNotEmpty($vat_vumber);
73
            Assert::string($company_name);
74
            Assert::string($company_address);
75
76
            return new self($query, $country_code, $vat_vumber, $company_name, $company_address);
0 ignored issues
show
It seems like $query can also be of type boolean; however, parameter $query of Th3Mouk\VatLayer\Respons...atNumber::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
            return new self(/** @scrutinizer ignore-type */ $query, $country_code, $vat_vumber, $company_name, $company_address);
Loading history...
77
        } catch (\JsonException | \InvalidArgumentException $exception) {
78
            throw BadResponse::dueToInvalidResponseFormat($response, $exception);
79
        }
80
    }
81
}
82