Counterparty   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 3
Metric Value
wmc 9
eloc 32
c 3
b 0
f 3
dl 0
loc 149
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCounterpartyOptions() 0 4 1
A delete() 0 3 1
A getCounterparties() 0 8 1
A updatePrivatePerson() 0 5 1
A saveOrganisation() 0 5 1
A __construct() 0 3 1
A getCounterpartyContactPersons() 0 7 1
A savePrivatePerson() 0 5 1
A getCounterpartyAddresses() 0 7 1
1
<?php
2
3
/** @noinspection PhpUnused */
4
5
declare(strict_types=1);
6
7
namespace SergeyNezbritskiy\NovaPoshta\Models;
8
9
use SergeyNezbritskiy\NovaPoshta\Connection;
10
use SergeyNezbritskiy\NovaPoshta\ModelInterface;
11
use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException;
12
13
/**
14
 * Class Counterparty
15
 * This class is for managing your counterparties
16
 *
17
 * @see https://new.novaposhta.ua/dashboard/contacts
18
 */
19
class Counterparty implements ModelInterface
20
{
21
    public const COUNTERPARTY_PROPERTY_SENDER = 'Sender';
22
    public const COUNTERPARTY_PROPERTY_RECIPIENT = 'Recipient';
23
    public const COUNTERPARTY_PROPERTY_THIRD_PERSON = 'ThirdPerson';
24
25
    private const MODEL_NAME = 'Counterparty';
26
27
    private Connection $connection;
28
29
    /**
30
     * @param Connection $connection
31
     */
32
    public function __construct(Connection $connection)
33
    {
34
        $this->connection = $connection;
35
    }
36
37
    /**
38
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/0ae5dd75-8a5f-11ec-8ced-005056b2dbe1
39
     * @param array $counterparty Array containing the necessary params.
40
     *    $counterparty = [
41
     *      'FirstName'             => (string) First name. Required.
42
     *      'MiddleName'            => (string) Middle name. Required.
43
     *      'LastName'              => (string) Last name. Required.
44
     *      'Phone'                 => (string) Phone number. Required.
45
     *      'Email'                 => (string) Email. Required.
46
     *      'CounterpartyProperty'  => (string) Counterparty property. Required.
47
     *    ];
48
     * @return array
49
     * @throws NovaPoshtaApiException
50
     */
51
    public function savePrivatePerson(array $counterparty): array
52
    {
53
        $counterparty['CounterpartyType'] = 'PrivatePerson';
54
        $result = $this->connection->post(self::MODEL_NAME, 'save', $counterparty);
55
        return array_shift($result);
56
    }
57
58
    /**
59
     * https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/bc3c44c7-8a8a-11ec-8ced-005056b2dbe1
60
     * @param array $counterparty Array containing the necessary params.
61
     *    $counterparty = [
62
     *      'EDRPOU'                => (string) EDRPOU. Required.
63
     *      'CounterpartyProperty'  => (string) Counterparty property. Optional.
64
     *    ];
65
     * @return array
66
     * @throws NovaPoshtaApiException
67
     */
68
    public function saveOrganisation(array $counterparty): array
69
    {
70
        $counterparty['CounterpartyType'] = 'Organization';
71
        $result = $this->connection->post(self::MODEL_NAME, 'save', $counterparty);
72
        return array_shift($result);
73
    }
74
75
    /**
76
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/0ae5dd75-8a5f-11ec-8ced-005056b2dbe1
77
     * @param array $counterparty Array containing the necessary params.
78
     *    $counterparty = [
79
     *      'Ref'                   => (string) Identifier. Required.
80
     *      'FirstName'             => (string) First name. Required.
81
     *      'MiddleName'            => (string) Middle name. Required.
82
     *      'LastName'              => (string) Last name. Required.
83
     *      'Phone'                 => (string) Phone number. Optional.
84
     *      'Email'                 => (string) Email. Optional.
85
     *      'CounterpartyProperty'  => (string) Counterparty property. Required.
86
     *    ];
87
     * @return array
88
     * @throws NovaPoshtaApiException
89
     */
90
    public function updatePrivatePerson(array $counterparty): array
91
    {
92
        $counterparty['CounterpartyType'] = 'PrivatePerson';
93
        $result = $this->connection->post(self::MODEL_NAME, 'update', $counterparty);
94
        return array_shift($result);
95
    }
96
97
    /**
98
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/a2eb27e8-8512-11ec-8ced-005056b2dbe1
99
     * @param string $ref
100
     * @return void
101
     * @throws NovaPoshtaApiException
102
     */
103
    public function delete(string $ref): void
104
    {
105
        $this->connection->post(self::MODEL_NAME, 'delete', ['Ref' => $ref]);
106
    }
107
108
    /**
109
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/a332efbf-8512-11ec-8ced-005056b2dbe1
110
     * @param string $ref
111
     * @return array
112
     * @throws NovaPoshtaApiException
113
     */
114
    public function getCounterpartyOptions(string $ref): array
115
    {
116
        $result = $this->connection->post(self::MODEL_NAME, 'getCounterpartyOptions', ['Ref' => $ref]);
117
        return array_shift($result);
118
    }
119
120
    /**
121
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/a3575a67-8512-11ec-8ced-005056b2dbe1
122
     * @param string $ref
123
     * @param int $page
124
     * @return array
125
     * @throws NovaPoshtaApiException
126
     */
127
    public function getCounterpartyContactPersons(string $ref, int $page = 1): array
128
    {
129
        $params = [
130
            'Ref' => $ref,
131
            'Page' => $page
132
        ];
133
        return $this->connection->post(self::MODEL_NAME, 'getCounterpartyContactPersons', $params);
134
    }
135
136
    /**
137
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/a37a06df-8512-11ec-8ced-005056b2dbe1
138
     * @param string $counterpartyProperty
139
     * @param string|null $search
140
     * @param int $page
141
     * @return array
142
     * @throws NovaPoshtaApiException
143
     */
144
    public function getCounterparties(string $counterpartyProperty, string $search = null, int $page = 1): array
145
    {
146
        $params = array_filter([
147
            'CounterpartyProperty' => $counterpartyProperty,
148
            'FindByString' => $search,
149
            'Page' => $page
150
        ]);
151
        return $this->connection->post(self::MODEL_NAME, 'getCounterparties', $params);
152
    }
153
154
    /**
155
     * @see https://developers.novaposhta.ua/view/model/a28f4b04-8512-11ec-8ced-005056b2dbe1/method/a30dbb7c-8512-11ec-8ced-005056b2dbe1
156
     * @param string $counterpartyRef
157
     * @param string $counterpartyProperty
158
     * @return array
159
     * @throws NovaPoshtaApiException
160
     */
161
    public function getCounterpartyAddresses(string $counterpartyRef, string $counterpartyProperty): array
162
    {
163
        $params = [
164
            'Ref' => $counterpartyRef,
165
            'CounterpartyProperty' => $counterpartyProperty,
166
        ];
167
        return $this->connection->post(self::MODEL_NAME, 'getCounterpartyAddresses', $params);
168
    }
169
}
170