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

TicketEndpoint::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
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\Ticket;
9
10
/**
11
 * Endpoint for tickets.
12
 */
13
class TicketEndpoint extends Endpoint
14
{
15
  //--------------------------------------------------------------------------------------------------------------------
16
  /**
17
   * The ID of the current invoice for which we are manipulating tickets.
18
   *
19
   * @var string|null
20
   */
21
  private $invoiceId;
22
23
  //--------------------------------------------------------------------------------------------------------------------
24
  /**
25
   * Archives all tickets for an invoice from ClubCollect.
26
   *
27
   * @param string $invoiceId The ID of Invoice for the tickets are archived.
28
   *
29
   * @throws ClubCollectApiException
30
   */
31
  public function archive(string $invoiceId): void
32
  {
33
    $this->invoiceId = $invoiceId;
34
35
    parent::restPost(['invoices', $invoiceId, 'tickets', 'actions', 'archive'],
36
                     ['api_key' => $this->client->getApiKey()]);
37
  }
38
39
  //--------------------------------------------------------------------------------------------------------------------
40
  /**
41
   * Assigns all tickets for an invoice to the ClubCollect support team.
42
   *
43
   * @param string $invoiceId The ID of Invoice for the tickets are assigned.
44
   *
45
   * @throws ClubCollectApiException
46
   */
47
  public function assignToSupport(string $invoiceId): void
48
  {
49
    $this->invoiceId = $invoiceId;
50
51
    parent::restPost(['invoices', $invoiceId, 'tickets', 'actions', 'assign_to_support'],
52
                     ['api_key' => $this->client->getApiKey()]);
53
  }
54
55
  //--------------------------------------------------------------------------------------------------------------------
56
  /**
57
   * Creates a ticket for an invoice from ClubCollect.
58
   *
59
   * @param string $invoiceId The ID of Invoice for the ticket is created.
60
   * @param string $message   The message of the ticket.
61
   *
62
   * @return Ticket
63
   *
64
   * @throws ClubCollectApiException
65
   */
66
  public function create(string $invoiceId, string $message): Ticket
67
  {
68
    $this->invoiceId = $invoiceId;
69
70
    /** @var Ticket $resource */
71
    $resource = parent::restPost(['invoices', $invoiceId, 'tickets'],
72
                                 ['api_key' => $this->client->getApiKey()],
73
                                 ['message' => $message]);
74
    if (!is_a($resource, Ticket::class))
75
    {
76
      throw new ClubCollectApiException('Expected an Ticket object, got a %s', get_class($resource));
77
    }
78
79
    return $resource;
80
  }
81
82
  //--------------------------------------------------------------------------------------------------------------------
83
  /**
84
   * Fetches all tickets for an invoice from ClubCollect.
85
   *
86
   * @param string $invoiceId The ID of Invoice for which tickets to be fetched.
87
   *
88
   * @return Ticket[]
89
   *
90
   * @throws ClubCollectApiException
91
   */
92
  public function fetch(string $invoiceId): array
93
  {
94
    $this->invoiceId = $invoiceId;
95
96
    return parent::restGetList('tickets',
97
                               ['invoices', $invoiceId, 'tickets'],
98
                               ['api_key' => $this->client->getApiKey()]);
99
  }
100
101
  //--------------------------------------------------------------------------------------------------------------------
102
  /**
103
   * Returns the list of tickets linked to a company, filtered by Ticket status, paginated and sorted in ascending
104
   * order, i.e. from oldest to newest.
105
   *
106
   * @param string   $status The status of the ticket. One of
107
   *                         <ul>
108
   *                         <li> unanswered
109
   *                         <li> answered
110
   *                         <li> archived
111
   *                         </ul>
112
   * @param int|null $from   The first fetchAll.
113
   * @param int|null $to     The last fetchAll.
114
   *
115
   * @return Ticket[]
116
   *
117
   * @throws ClubCollectApiException
118
   */
119
  public function fetchAll(string $status, ?int $from = null, ?int $to = null): array
120
  {
121
    $this->invoiceId = null;
122
123
    return parent::restGetPages('tickets',
124
                                $from,
125
                                $to,
126
                                ['companies', $this->client->getCompanyId(), 'tickets', $status],
127
                                ['api_key' => $this->client->getApiKey()]);
128
  }
129
130
  //--------------------------------------------------------------------------------------------------------------------
131
  /**
132
   * Returns an instance of this class.
133
   *
134
   * @param array $response The API response.
135
   *
136
   * @return Ticket
137
   *
138
   * @throws ClubCollectApiException
139
   */
140
  protected function createResourceObject(array $response): BaseResource
141
  {
142
    return new Ticket($this->client, $response, $this->invoiceId);
143
  }
144
145
  //--------------------------------------------------------------------------------------------------------------------
146
}
147
148
//----------------------------------------------------------------------------------------------------------------------
149