Completed
Pull Request — master (#7)
by
unknown
01:16
created

AbstractNetgsmReport::getReports()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
cc 4
nc 3
nop 0
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
     * @param $value
133
     * @param $format
134
     * @return int|string
135
     */
136
    protected function formatValue($value, $format)
137
    {
138
        switch ($format) {
139
            case 'integer':
140
                $value = (int) $value;
141
                break;
142
            case 'string':
143
                $value = ''.$value;
144
                break;
145
        }
146
147
        return $value;
148
    }
149
150
    /**
151
     * @return Collection
152
     * @throws GuzzleException
153
     * @throws ReportException
154
     */
155
    public function getReports(): Collection
156
    {
157
        $data = [
158
            'page'     => 1,
159
        ];
160
161
        $data = array_merge($data, $this->filters);
162
        $keep = true;
163
        $allResults = new Collection();
164
        do {
165
            $rawResponse = $this->callApi('GET', $this->url, $data);
166
167
            if ($this->paginated) {
168
                if (in_array($rawResponse, $this->noResultCodes)) {
169
                    $keep = false;
170
                } else {
171
                    $data['page']++;
172
                    $response = $this->parseResponse($rawResponse);
173
                    $allResults = $allResults->merge($response);
174
                }
175
            } else {
176
                $response = $this->parseResponse($rawResponse);
177
                $allResults = $allResults->merge($response);
178
                $keep = false;
179
            }
180
        } while ($keep);
181
182
        return $allResults;
183
    }
184
185
    /**
186
     * @param $response
187
     * @return bool
188
     * @throws ReportException
189
     */
190
    public function validateResponse($response): bool
191
    {
192
        if (in_array(intval($response), $this->errorCodes)) {
193
            throw new ReportException('Netgsm report error', $response);
194
        }
195
196
        if (in_array($response, $this->noResultCodes)) {
197
            return false;
198
        }
199
200
        return true;
201
    }
202
}
203