Passed
Push — master ( f67d35...d5862f )
by Jhao
02:18
created

PostOffices::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of RussianPost SDK package.
5
 *
6
 * © Appwilio (http://appwilio.com), JhaoDa (https://github.com/jhaoda)
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices;
15
16
use Appwilio\RussianPostSDK\Dispatching\Http\ArrayOf;
17
use Appwilio\RussianPostSDK\Dispatching\Http\ApiClient;
18
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Responses\Service;
19
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Responses\PostOffice;
20
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Responses\Coordinates;
21
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Requests\FindByAddressRequest;
22
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Requests\FindByCoordinatesRequest;
23
24
final class PostOffices
25
{
26
    /** @var ApiClient */
27
    private $client;
28
29 1
    public function __construct(ApiClient $client)
30
    {
31 1
        $this->client = $client;
32 1
    }
33
34
    /**
35
     * Получение информации о почтовом отделении по индексу.
36
     *
37
     * https://otpravka.pochta.ru/specification#/services-postoffice
38
     *
39
     * Упомянутые в документации `current-date-time`, `filter-by-office-type` и `ufps-postal-code` ни на что не влияют.
40
     *
41
     * @param  string            $postalCode   индекс ОПС
42
     * @param  Coordinates|null  $coordinates  для расчёта дистанции до ОПС (в км)
43
     *
44
     * @return PostOffice
45
     */
46
    public function get(string $postalCode, ?Coordinates $coordinates = null): PostOffice
47
    {
48
        $query = null;
49
50
        if ($coordinates) {
51
            $query = \http_build_query($coordinates->toArray());
52
        }
53
54
        return $this->client->get("/postoffice/1.0/{$postalCode}".($query ? "?{$query}" : null), null, PostOffice::class);
55
    }
56
57
    /**
58
     * Поиск почтовых отделений по координатам.
59
     *
60
     * https://otpravka.pochta.ru/specification#/services-postoffice-nearby
61
     *
62
     * @param  FindByCoordinatesRequest  $request
63
     *
64
     * @return iterable|PostOffice[]
65
     */
66
    public function findByCoordinates(FindByCoordinatesRequest $request): iterable
67
    {
68
        return $this->client->get('/postoffice/1.0/nearby', $request, new ArrayOf(PostOffice::class));
69
    }
70
71
    /**
72
     * Поиск индексов обслуживающих ОПС по адресу.
73
     *
74
     * https://otpravka.pochta.ru/specification#/services-postoffice-by-address
75
     *
76
     * @param  FindByAddressRequest  $request
77
     *
78
     * @return iterable|string[]
79
     */
80
    public function findPostalCodesByAddress(FindByAddressRequest $request): iterable
81
    {
82
        return $this->client->get('/postoffice/1.0/by-address', $request);
83
    }
84
85
    /**
86
     * Поиск индексов ОПС в населённом пункте.
87
     *
88
     * https://otpravka.pochta.ru/specification#/services-postoffice-settlement.offices.codes
89
     *
90
     * @param  string       $settlement
91
     * @param  string|null  $region
92
     * @param  string|null  $district
93
     *
94
     * @return iterable|string[]
95
     */
96
    public function findPostalCodesForSettlement(string $settlement, ?string $region = null, ?string $district = null): iterable
97
    {
98
        $query = \http_build_query(\compact('settlement', 'region', 'district'));
99
100
        return $this->client->get("/postoffice/1.0/settlement.offices.codes?{$query}");
101
    }
102
103
    /**
104
     * Получение почтовых сервисов ОПС c опциональной фильтрацией по группе сервисов.
105
     *
106
     * https://otpravka.pochta.ru/specification#/services-postoffice-service
107
     * https://otpravka.pochta.ru/specification#/services-postoffice-service-group
108
     *
109
     * @param  string    $postalCode
110
     * @param  int|null  $group
111
     *
112
     * @return iterable|Service[]
113
     */
114
    public function getServices(string $postalCode, ?int $group = null): iterable
115
    {
116
        $path = "/postoffice/1.0/{$postalCode}/services".($group ? "/{$group}" : null);
117
118
        return $this->client->get($path, null, new ArrayOf(Service::class));
119
    }
120
}
121