Test Failed
Push — master ( a4ccfc...b2466b )
by P.R.
02:10
created

Endpoint::restGetPageInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\ClubCollect\Endpoint;
5
6
use SetBased\ClubCollect\ClubCollectApiClient;
7
use SetBased\ClubCollect\Exception\ClubCollectApiException;
8
use SetBased\ClubCollect\Resource\BaseResource;
9
10
/**
11
 * Abstract parent class for all end points.
12
 */
13
abstract class Endpoint
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * The ClubCollect API client.
18
   *
19
   * @var ClubCollectApiClient
20
   */
21
  protected $client;
22
23
  //--------------------------------------------------------------------------------------------------------------------
24
  /**
25
   * Object constructor.
26
   *
27
   * @param ClubCollectApiClient $client The ClubCollect API client.
28
   */
29
  public function __construct(ClubCollectApiClient $client)
30
  {
31
    $this->client = $client;
32
  }
33
34
  //--------------------------------------------------------------------------------------------------------------------
35
  /**
36
   * Get the object that is used by this API endpoint. Every API endpoint uses one type of object.
37
   *
38
   * @param array $response The response from the API.
39
   *
40
   * @return BaseResource
41
   */
42
  abstract protected function createResourceObject(array $response);
43
44
  //--------------------------------------------------------------------------------------------------------------------
45
  /**
46
   * Sends a DELETE request for a single object to the REST API.
47
   *
48
   * @param array      $path  The parts of the path.
49
   * @param array|null $query The query parameters. A map from key to value.
50
   *
51
   * @throws ClubCollectApiException
52
   */
53
  protected function restDelete(array $path, ?array $query = null): void
54
  {
55
    $this->client->performHttpCall(ClubCollectApiClient::HTTP_DELETE, $path, $query);
56
  }
57
58
  //--------------------------------------------------------------------------------------------------------------------
59
  /**
60
   * Retrieves a single object from the REST API.
61
   *
62
   * @param array      $path  The parts of the path.
63
   * @param array|null $query The query parameters. A map from key to value.
64
   *
65
   * @return BaseResource
66
   *
67
   * @throws ClubCollectApiException
68
   */
69
  protected function restGet(array $path, ?array $query = null): BaseResource
70
  {
71
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query);
72
    if ($result===null)
73
    {
74
      throw new ClubCollectApiException('Null response received from ClubCollect');
75
    }
76
77
    return $this->createResourceObject($result);
78
  }
79
80
  //--------------------------------------------------------------------------------------------------------------------
81
  /**
82
   * Retrieves a list of objects from the REST API.
83
   *
84
   * @param string     $key   The key of the objects in the response.
85
   * @param array      $path  The parts of the path.
86
   * @param array|null $query The query parameters. A map from key to value.
87
   *
88
   * @return array
89
   *
90
   * @throws ClubCollectApiException
91
   */
92
  protected function restGetList(string $key, array $path, ?array $query = null): array
93
  {
94
    $list   = [];
95
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query);
96
    if ($result===null)
97
    {
98
      throw new ClubCollectApiException('Null response received from ClubCollect');
99
    }
100
101
    foreach ($result[$key] as $import)
102
    {
103
      $list[] = $this->createResourceObject($import);
104
    }
105
106
    return $list;
107
  }
108
109
  //--------------------------------------------------------------------------------------------------------------------
110
  /**
111
   * Retrieves a list of objects from the REST API.
112
   *
113
   * @param array      $path  The parts of the path.
114
   * @param array|null $query The query parameters. A map from key to value.
115
   *
116
   * @return array
117
   *
118
   * @throws ClubCollectApiException
119
   */
120
  protected function restGetPageInfo(array $path, ?array $query = null): array
121
  {
122
    $query['page_number'] = 1;
123
    $result               = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query);
124
    if ($result===null)
125
    {
126
      throw new ClubCollectApiException('Null response received from ClubCollect');
127
    }
128
129
    $info = $result['page'];
130
    unset($info['page_number']);
131
132
    return $info;
133
  }
134
135
  //--------------------------------------------------------------------------------------------------------------------
136
  /**
137
   * Retrieves a list of objects from the REST API.
138
   *
139
   * @param string     $key   The key of the objects in the response.
140
   * @param int|null   $from  The first page.
141
   * @param int|null   $to    The last page.
142
   * @param array      $path  The parts of the path.
143
   * @param array|null $query The query parameters. A map from key to value.
144
   *
145
   * @return array
146
   *
147
   * @throws ClubCollectApiException
148
   */
149
  protected function restGetPages(string $key, ?int $from, ?int $to, array $path, ?array $query = null): array
150
  {
151
    $list = [];
152
    $page = $from ?? 0;
153
    do
154
    {
155
      ++$page;
156
157
      $query['page_number'] = $page;
158
      $result               = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query);
159
      if ($result===null)
160
      {
161
        throw new ClubCollectApiException('Null response received from ClubCollect');
162
      }
163
164
      foreach ($result[$key] as $import)
165
      {
166
        $list[] = $this->createResourceObject($import);
167
      }
168
    } while ($page<($to ?? $result['page']['total_pages']));
169
170
    return $list;
171
  }
172
173
  //--------------------------------------------------------------------------------------------------------------------
174
  /**
175
   * Creates or updates a single object from the REST API.
176
   *
177
   * @param array      $path  The parts of the path.
178
   * @param array|null $query The query parameters. A map from key to value.
179
   * @param array|null $body  The body parameters. A map from key to value.
180
   *
181
   * @return BaseResource|null
182
   *
183
   * @throws ClubCollectApiException
184
   */
185
  protected function restPost(array $path, ?array $query = null, ?array $body = null): ?BaseResource
186
  {
187
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_POST, $path, $query, $body);
188
189
    if ($result===null) return null;
190
191
    return $this->createResourceObject($result);
192
  }
193
194
  //--------------------------------------------------------------------------------------------------------------------
195
  /**
196
   * Updates a single object on the REST API.
197
   *
198
   * @param array      $path  The parts of the path.
199
   * @param array|null $query The query parameters. A map from key to value.
200
   * @param array|null $body  The body parameters. A map from key to value.
201
   *
202
   * @return BaseResource The update resource.
203
   *
204
   * @throws ClubCollectApiException
205
   */
206
  protected function restPut(array $path, ?array $query = null, ?array $body = null): BaseResource
207
  {
208
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_PUT, $path, $query, $body);
209
    if ($result===null)
210
    {
211
      throw new ClubCollectApiException('Null response received from ClubCollect');
212
    }
213
214
    return $this->createResourceObject($result);
215
  }
216
217
  //--------------------------------------------------------------------------------------------------------------------
218
}
219
220
//----------------------------------------------------------------------------------------------------------------------
221