GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AddressValidator::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\BpostAddressWebservice;
4
5
use Spatie\BpostAddressWebservice\Gateways\Gateway;
6
use Spatie\BpostAddressWebservice\Gateways\BpostGateway;
7
use Spatie\BpostAddressWebservice\Requests\ValidateAddressesRequest;
8
use Spatie\BpostAddressWebservice\Exceptions\CouldNotValidateAddress;
9
10
class AddressValidator
11
{
12
    const OPTION_INCLUDE_FORMATTING = 'IncludeFormatting';
13
    const OPTION_INCLUDE_SUGGESTIONS = 'IncludeSuggestions';
14
    const OPTION_INCLUDE_SUBMITTED_ADDRESS = 'IncludeSubmittedAddress';
15
    const OPTION_INCLUDE_DEFAULT_GEO_LOCATION = 'IncludeDefaultGeoLocation';
16
    const OPTION_INCLUDE_SUFFIX_LIST = 'IncludeSuffixList';
17
    const OPTION_INCLUDE_NUMBER_OF_SUFFIXES = 'IncludeNumberOfSuffixes';
18
    const OPTION_INCLUDE_LIST_OF_BOXES = 'IncludeListOfBoxes';
19
    const OPTION_INCLUDE_NUMBER_OF_BOXES = 'IncludeNumberOfBoxes';
20
21
    /** @var \Spatie\BpostAddressWebservice\Gateways\Gateway */
22
    protected $gateway;
23
24
    /** @var array */
25
    protected $options = [];
26
27
    public function __construct(Gateway $gateway)
28
    {
29
        $this->gateway = $gateway;
30
    }
31
32
    public static function create(): AddressValidator
33
    {
34
        return new static(new BpostGateway());
35
    }
36
37
    public function withOptions(array $options): AddressValidator
38
    {
39
        $this->options = $options;
40
41
        return $this;
42
    }
43
44
    public function validate(Address $address): ValidatedAddress
45
    {
46
        $validateAddressesResponse = $this->gateway->validateAddresses(
47
            new ValidateAddressesRequest([$address], $this->options)
48
        );
49
50
        return $validateAddressesResponse->validatedAddresses()[0];
51
    }
52
53
    public function validateMany(array $addresses): array
54
    {
55
        $maximumAddresCount = 100;
56
57
        if (count($addresses) > $maximumAddresCount) {
58
            throw CouldNotValidateAddress::tooManyAddresses($addresses, $maximumAddresCount);
59
        }
60
61
        $validateAddressesResponse = $this->gateway->validateAddresses(
62
            new ValidateAddressesRequest($addresses, $this->options)
63
        );
64
65
        return $validateAddressesResponse->validatedAddresses();
66
    }
67
}
68