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

ImportEndpoint::transmit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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