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

ImportEndpoint::fetchPageInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
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\Exception\ClubCollectApiException;
7
use SetBased\ClubCollect\Resource\BaseResource;
8
use SetBased\ClubCollect\Resource\Import;
9
10
/**
11
 * Endpoint for imports.
12
 */
13
class ImportEndpoint extends Endpoint
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * Creates an import.
18
   *
19
   * @param string|null $title                The title of the import.
20
   * @param int|null    $expectedInvoiceCount Number of invoices expected to be added to this import. If provided the
21
   *                                          Import cannot be transmitted from the ClubCollect User Interface until
22
   *                                          all invoices are created.
23
   *
24
   * @return Import
25
   *
26
   * @throws ClubCollectApiException
27
   */
28
  public function create(?string $title = null, ?int $expectedInvoiceCount = null): Import
29
  {
30
    /** @var Import $resource */
31
    $resource = parent::restPost(['imports'],
32
                                 ['api_key' => $this->client->getApiKey()],
33
                                 ['title'                   => $title,
34
                                  'company_id'              => $this->client->getCompanyId(),
35
                                  'expected_invoices_count' => $expectedInvoiceCount]);
36
    if (!is_a($resource, Import::class))
37
    {
38
      throw new ClubCollectApiException('Expected an Import object, got a %s', get_class($resource));
39
    }
40
41
    return $resource;
42
  }
43
44
  //--------------------------------------------------------------------------------------------------------------------
45
  /**
46
   * Deletes an import from ClubCollect.
47
   *
48
   * @param string $id The ID (supplied by ClubCollect) of the import.
49
   *
50
   * @throws ClubCollectApiException
51
   */
52
  public function delete(string $id): void
53
  {
54
    parent::restDelete(['imports', $id], ['api_key' => $this->client->getApiKey()]);
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  /**
59
   * Fetches an import from ClubCollect.
60
   *
61
   * @param string $importId The ID (supplied by ClubCollect) of the import.
62
   *
63
   * @return Import
64
   *
65
   * @throws ClubCollectApiException
66
   */
67
  public function fetch(string $importId): Import
68
  {
69
    /** @var Import $resource */
70
    $resource = parent::restGet(['imports', $importId], ['api_key' => $this->client->getApiKey()]);
71
    if (!is_a($resource, Import::class))
72
    {
73
      throw new ClubCollectApiException('Expected an Import object, got a %s', get_class($resource));
74
    }
75
76
    return $resource;
77
  }
78
79
  //--------------------------------------------------------------------------------------------------------------------
80
  /**
81
   * Fetches a list of imports.
82
   *
83
   * @param int|null $from The first to fetch. Defaults to the first page.
84
   * @param int|null $to   The last to fetch. Defaults to the last page.
85
   *
86
   * @return Import[]
87
   *
88
   * @throws ClubCollectApiException
89
   */
90
  public function fetchAll(?int $from = null, ?int $to = null): array
91
  {
92
    return parent::restGetPages('imports',
93
                                $from,
94
                                $to,
95
                                ['companies', $this->client->getCompanyId(), 'imports'],
96
                                ['api_key' => $this->client->getApiKey()]);
97
  }
98
99
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * Fetches info about available pages.
102
   *
103
   * @return array Has the following keys: total_pages.
104
   *
105
   * @throws ClubCollectApiException
106
   */
107
  public function fetchPageInfo(): array
108
  {
109
    return parent::restGetPageInfo(['companies', $this->client->getCompanyId(), 'imports'],
110
                                   ['api_key' => $this->client->getApiKey()]);
111
  }
112
113
  //--------------------------------------------------------------------------------------------------------------------
114
  /**
115
   * Instructs ClubCollect to transmit the Import, initiating the invoice collection process.
116
   *
117
   * @param string $id The ID (supplied by ClubCollect) of the import.
118
   *
119
   * @return Import
120
   *
121
   * @throws ClubCollectApiException
122
   */
123
  public function transmit(string $id): Import
124
  {
125
    /** @var Import $resource */
126
    $resource = parent::restPut(['imports', $id, 'transmit'],
127
                                ['api_key' => $this->client->getApiKey()]);
128
    if (!is_a($resource, Import::class))
129
    {
130
      throw new ClubCollectApiException('Expected an Import object, got a %s', get_class($resource));
131
    }
132
133
    return $resource;
134
  }
135
136
  //--------------------------------------------------------------------------------------------------------------------
137
  /**
138
   * Updates an import.
139
   *
140
   * @param string   $id                      The ID (supplied by ClubCollect) of the import.
141
   * @param int|null $expectedInvoiceCount    Number of invoices expected to be added to this import. If provided the
142
   *                                          Import cannot be transmitted from the ClubCollect User Interface until
143
   *                                          all invoices are created.
144
   *
145
   * @return Import
146
   *
147
   * @throws ClubCollectApiException
148
   */
149
  public function update(string $id, ?int $expectedInvoiceCount = null): Import
150
  {
151
    /** @var Import $resource */
152
    $resource = parent::restPut(['imports', $id],
153
                                ['api_key' => $this->client->getApiKey()],
154
                                ['expected_invoices_count' => $expectedInvoiceCount]);
155
    if (!is_a($resource, Import::class))
156
    {
157
      throw new ClubCollectApiException('Expected an Import object, got a %s', get_class($resource));
158
    }
159
160
    return $resource;
161
  }
162
163
  //--------------------------------------------------------------------------------------------------------------------
164
  /**
165
   * Returns an instance of this class.
166
   *
167
   * @param array $response The API response.
168
   *
169
   * @return Import
170
   *
171
   * @throws ClubCollectApiException
172
   */
173
  protected function createResourceObject(array $response): BaseResource
174
  {
175
    return new Import($this->client, $response);
176
  }
177
178
  //--------------------------------------------------------------------------------------------------------------------
179
}
180
181
//----------------------------------------------------------------------------------------------------------------------
182