Passed
Push — master ( ff7c30...ced429 )
by Jhao
02:29
created

PostOffices::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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 GuzzleHttp\Psr7\UploadedFile;
17
use Appwilio\RussianPostSDK\Core\ArrayOf;
18
use Appwilio\RussianPostSDK\Core\GenericRequest;
19
use Appwilio\RussianPostSDK\Dispatching\Enum\OpsType;
20
use Appwilio\RussianPostSDK\Dispatching\Http\ApiClient;
21
use Appwilio\RussianPostSDK\Dispatching\Entities\Coordinates;
22
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Responses\Service;
23
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Responses\PostOffice;
24
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Requests\FindByAddressRequest;
25
use Appwilio\RussianPostSDK\Dispatching\Endpoints\PostOffices\Requests\FindByCoordinatesRequest;
26
27
final class PostOffices
28
{
29
    /** @var ApiClient */
30
    private $client;
31
32 1
    public function __construct(ApiClient $client)
33
    {
34 1
        $this->client = $client;
35 1
    }
36
37
    /**
38
     * Получение информации о почтовом отделении по индексу.
39
     *
40
     * https://otpravka.pochta.ru/specification#/services-postoffice
41
     *
42
     * Упомянутые в документации `current-date-time`, `filter-by-office-type` и `ufps-postal-code` ни на что не влияют.
43
     *
44
     * @param  string            $postalCode   индекс ОПС
45
     * @param  Coordinates|null  $coordinates  для расчёта дистанции до ОПС (в км)
46
     *
47
     * @return PostOffice
48
     */
49
    public function get(string $postalCode, ?Coordinates $coordinates = null): PostOffice
50
    {
51
        $query = $coordinates ? \http_build_query($coordinates->toArray()) : null;
52
53
        return $this->client->get("/postoffice/1.0/{$postalCode}".($query ? "?{$query}" : null), null, PostOffice::class);
54
    }
55
56
    /**
57
     * Поиск почтовых отделений по координатам.
58
     *
59
     * https://otpravka.pochta.ru/specification#/services-postoffice-nearby
60
     *
61
     * @param  FindByCoordinatesRequest  $request
62
     *
63
     * @return iterable|PostOffice[]
64
     */
65
    public function findByCoordinates(FindByCoordinatesRequest $request): iterable
66
    {
67
        return $this->client->get('/postoffice/1.0/nearby', $request, new ArrayOf(PostOffice::class));
68
    }
69
70
    /**
71
     * Поиск индексов обслуживающих ОПС по адресу.
72
     *
73
     * https://otpravka.pochta.ru/specification#/services-postoffice-by-address
74
     *
75
     * @param  FindByAddressRequest  $request
76
     *
77
     * @return iterable|string[]
78
     */
79
    public function findPostalCodesByAddress(FindByAddressRequest $request): iterable
80
    {
81
        return $this->client->get('/postoffice/1.0/by-address', $request);
82
    }
83
84
    /**
85
     * Поиск индексов ОПС в населённом пункте.
86
     *
87
     * https://otpravka.pochta.ru/specification#/services-postoffice-settlement.offices.codes
88
     *
89
     * @param  string       $settlement
90
     * @param  string|null  $region
91
     * @param  string|null  $district
92
     *
93
     * @return iterable|string[]
94
     */
95
    public function findPostalCodesForSettlement(string $settlement, ?string $region = null, ?string $district = null): iterable
96
    {
97
        $query = \http_build_query(\compact('settlement', 'region', 'district'));
98
99
        return $this->client->get("/postoffice/1.0/settlement.offices.codes?{$query}");
100
    }
101
102
    /**
103
     * Получение почтовых сервисов ОПС c опциональной фильтрацией по группе сервисов.
104
     *
105
     * @see https://otpravka.pochta.ru/specification#/services-postoffice-service
106
     * @see https://otpravka.pochta.ru/specification#/services-postoffice-service-group
107
     *
108
     * @param  string    $postalCode
109
     * @param  int|null  $group
110
     *
111
     * @return iterable|Service[]
112
     */
113
    public function getServices(string $postalCode, ?int $group = null): iterable
114
    {
115
        $path = "/postoffice/1.0/{$postalCode}/services".($group ? "/{$group}" : null);
116
117
        return $this->client->get($path, null, new ArrayOf(Service::class));
118
    }
119
120
    /**
121
     * Выгрузка информации об объектах почтовой связи в zip-архиве.
122
     *
123
     * @param  OpsType  $type
124
     *
125
     * @return UploadedFile
126
     */
127
    public function all(OpsType $type): UploadedFile
128
    {
129
        $request = GenericRequest::create([
130
            'type' => $type->getValue(),
131
        ]);
132
133
        return $this->client->get('/1.0/unloading-passport/zip', $request);
134
    }
135
}
136