Test Failed
Push — master ( 6512d2...c62a67 )
by P.R.
02:58 queued 11s
created

Endpoint::restPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 5
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 getResourceObject(array $response);
43
44
  //--------------------------------------------------------------------------------------------------------------------
45
  /**
46
   * Creates or updates a single object from 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
   * @param array|null $body  The body parameters. A map from key to value.
51
   *
52
   * @return BaseResource
53
   *
54
   * @throws ClubCollectApiException
55
   */
56
  protected function restPost(array $path, ?array $query = null, ?array $body = null)
57
  {
58
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_POST, $path, $query, $body);
59
60
    return $this->getResourceObject($result);
61
  }
62
63
  //--------------------------------------------------------------------------------------------------------------------
64
  /**
65
   * Sends a DELETE request for a single object to the REST API.
66
   *
67
   * @param array      $path  The parts of the path.
68
   * @param array|null $query The query parameters. A map from key to value.
69
   *
70
   * @throws ClubCollectApiException
71
   */
72
  protected function restDelete(array $path, ?array $query = null): void
73
  {
74
    $this->client->performHttpCall(ClubCollectApiClient::HTTP_DELETE, $path, $query);
75
  }
76
77
  //--------------------------------------------------------------------------------------------------------------------
78
  /**
79
   * Retrieves a list of objects from the REST API.
80
   *
81
   * @param int|null   $from  The first page.
82
   * @param int|null   $to    The last page.
83
   * @param array      $path  The parts of the path.
84
   * @param array|null $query The query parameters. A map from key to value.
85
   *
86
   * @return array
87
   *
88
   * @throws ClubCollectApiException
89
   */
90
  protected function restList(?int $from, ?int $to, array $path, ?array $query = null): array
91
  {
92
    $list = [];
93
    $page = $from ?? 0;
94
    do
95
    {
96
      ++$page;
97
98
      $query['page_number'] = $page;
99
      $result               = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query);
100
101
      foreach ($result['imports'] as $import)
102
      {
103
        $list[] = $this->getResourceObject($import);
104
      }
105
    } while ($page<($to ?? $result['page']['total_pages']));
106
107
    return $list;
108
  }
109
110
  //--------------------------------------------------------------------------------------------------------------------
111
  /**
112
   * Updates a single object on the REST API.
113
   *
114
   * @param array      $path  The parts of the path.
115
   * @param array|null $query The query parameters. A map from key to value.
116
   * @param array|null $body  The body parameters. A map from key to value.
117
   *
118
   * @return BaseResource The update resource.
119
   *
120
   * @throws ClubCollectApiException
121
   */
122
  protected function restPut(array $path, ?array $query = null, ?array $body = null)
123
  {
124
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_PUT, $path, $query, $body);
125
126
    return $this->getResourceObject($result);
127
  }
128
129
  //--------------------------------------------------------------------------------------------------------------------
130
  /**
131
   * Retrieves a single object from the REST API.
132
   *
133
   * @param array      $path  The parts of the path.
134
   * @param array|null $query The query parameters. A map from key to value.
135
   *
136
   * @return BaseResource
137
   *
138
   * @throws ClubCollectApiException
139
   */
140
  protected function restRead(array $path, ?array $query = null)
141
  {
142
    $result = $this->client->performHttpCall(ClubCollectApiClient::HTTP_GET, $path, $query);
143
144
    return $this->getResourceObject($result);
145
  }
146
147
  //--------------------------------------------------------------------------------------------------------------------
148
}
149
150
//----------------------------------------------------------------------------------------------------------------------
151