Test Failed
Push — master ( 4a2444...e6139c )
by P.R.
02:32
created

ImportEndpoint::createResourceObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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 fetchAll.
84
   * @param int|null $to   The last fetchAll.
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
   * Instructs ClubCollect to transmit the Import, initiating the invoice collection process.
102
   *
103
   * @param string $id The ID (supplied by ClubCollect) of the import.
104
   *
105
   * @return Import
106
   *
107
   * @throws ClubCollectApiException
108
   */
109
  public function transmit(string $id): Import
110
  {
111
    /** @var Import $resource */
112
    $resource = parent::restPut(['imports', $id, 'transmit'],
113
                                ['api_key' => $this->client->getApiKey()]);
114
    if (!is_a($resource, Import::class))
115
    {
116
      throw new ClubCollectApiException('Expected an Import object, got a %s', get_class($resource));
117
    }
118
119
    return $resource;
120
  }
121
122
  //--------------------------------------------------------------------------------------------------------------------
123
  /**
124
   * Updates an import.
125
   *
126
   * @param string   $id                      The ID (supplied by ClubCollect) of the import.
127
   * @param int|null $expectedInvoiceCount    Number of invoices expected to be added to this import. If provided the
128
   *                                          Import cannot be transmitted from the ClubCollect User Interface until
129
   *                                          all invoices are created.
130
   *
131
   * @return Import
132
   *
133
   * @throws ClubCollectApiException
134
   */
135
  public function update(string $id, ?int $expectedInvoiceCount = null): Import
136
  {
137
    /** @var Import $resource */
138
    $resource = parent::restPut(['imports', $id],
139
                                ['api_key' => $this->client->getApiKey()],
140
                                ['expected_invoices_count' => $expectedInvoiceCount]);
141
    if (!is_a($resource, Import::class))
142
    {
143
      throw new ClubCollectApiException('Expected an Import object, got a %s', get_class($resource));
144
    }
145
146
    return $resource;
147
  }
148
149
  //--------------------------------------------------------------------------------------------------------------------
150
  /**
151
   * Returns an instance of this class.
152
   *
153
   * @param array $response The API response.
154
   *
155
   * @return Import
156
   *
157
   * @throws ClubCollectApiException
158
   */
159
  protected function createResourceObject(array $response): BaseResource
160
  {
161
    return new Import($this->client, $response);
162
  }
163
164
  //--------------------------------------------------------------------------------------------------------------------
165
}
166
167
//----------------------------------------------------------------------------------------------------------------------
168