AbstractNetgsmReport::setView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\Netgsm\Report;
4
5
use GuzzleHttp\Exception\GuzzleException;
6
use Illuminate\Support\Carbon;
7
use Illuminate\Support\Collection;
8
use TarfinLabs\Netgsm\Exceptions\ReportException;
9
use TarfinLabs\Netgsm\NetgsmApiClient;
10
11
abstract class AbstractNetgsmReport extends NetgsmApiClient
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $errorCodes = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $noResultCodes = [];
22
23
    /**
24
     * @var bool
25
     */
26
    protected $paginated = false;
27
28
    /**
29
     * @var array
30
     */
31
    protected $columns = [];
32
33
    /**
34
     * @var array
35
     */
36
    protected $columnMap = [];
37
38
    /**
39
     * @var string endpoint url
40
     */
41
    protected $url;
42
43
    /**
44
     * @var array
45
     */
46
    protected $filters = [];
47
48
    /**
49
     * @param  string  $response
50
     * @return Collection
51
     */
52
    abstract protected function parseResponse(string $response): Collection;
53
54
    /**
55
     * @param  string  $type
56
     * @return AbstractNetgsmReport
57
     */
58
    protected function setType(string $type): self
59
    {
60
        $this->filters['type'] = $type;
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param  Carbon|\Carbon\Carbon  $startDate
67
     * @return $this
68
     */
69
    public function setStartDate($startDate): self
70
    {
71
        $this->filters['bastar'] = $startDate->format('dmY');
72
73
        return $this;
74
    }
75
76
    /**
77
     * @param  Carbon|\Carbon\Carbon  $endDate
78
     * @return $this
79
     */
80
    public function setEndDate($endDate): self
81
    {
82
        $this->filters['bittar'] = $endDate->format('dmY');
83
84
        return $this;
85
    }
86
87
    /**
88
     * @param  mixed  $bulkId
89
     * @return AbstractNetgsmReport
90
     */
91
    public function setBulkId($bulkId): self
92
    {
93
        $this->filters['bulkid'] = is_array($bulkId) ? implode(',', $bulkId) : $bulkId;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @param  string  $version
100
     * @return AbstractNetgsmReport
101
     */
102
    public function setVersion(string $version): self
103
    {
104
        $this->filters['version'] = $version;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param  string  $view
111
     * @return AbstractNetgsmReport
112
     */
113
    public function setView(string $view): self
114
    {
115
        $this->filters['view'] = $view;
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param  int  $page
122
     * @return AbstractNetgsmReport
123
     */
124
    public function setPage(int $page): self
125
    {
126
        $this->filters['page'] = $page;
127
128
        return $this;
129
    }
130
131
    /**
132
     * formats the value by specified type.
133
     *
134
     * @param $value
135
     * @param $format
136
     * @return int|string
137
     */
138
    protected function formatValue($value, $format)
139
    {
140
        switch ($format) {
141
            case 'integer':
142
                $value = (int) $value;
143
                break;
144
            case 'string':
145
                $value = ''.$value;
146
                break;
147
        }
148
149
        return $value;
150
    }
151
152
    /**
153
     * returns the netgsm basic sms reports as a collection.
154
     *
155
     * @return Collection
156
     * @throws GuzzleException
157
     * @throws ReportException
158
     */
159
    public function getReports(): Collection
160
    {
161
        $data = [
162
            'page'     => 1,
163
        ];
164
165
        $data = array_merge($data, $this->filters);
166
        $keep = true;
167
        $allResults = new Collection();
168
        do {
169
            $rawResponse = $this->callApi('GET', $this->url, $data);
170
171
            if ($this->paginated) {
172
                if (in_array($rawResponse, $this->noResultCodes)) {
173
                    $keep = false;
174
                } else {
175
                    $data['page']++;
176
                    $response = $this->parseResponse($rawResponse);
177
                    $allResults = $allResults->merge($response);
178
                }
179
            } else {
180
                $response = $this->parseResponse($rawResponse);
181
                $allResults = $allResults->merge($response);
182
                $keep = false;
183
            }
184
        } while ($keep);
185
186
        return $allResults;
187
    }
188
189
    /**
190
     * validates the response returned from netgsm report api.
191
     *
192
     * @param $response
193
     * @return bool
194
     * @throws ReportException
195
     */
196
    public function validateResponse($response): bool
197
    {
198
        if (in_array(intval($response), $this->errorCodes)) {
199
            throw new ReportException('Netgsm report error', $response);
200
        }
201
202
        if (in_array($response, $this->noResultCodes)) {
203
            return false;
204
        }
205
206
        return true;
207
    }
208
}
209