NetgsmSmsReport::setBulkId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace TarfinLabs\Netgsm\Report;
4
5
use Illuminate\Support\Collection;
6
7
class NetgsmSmsReport extends AbstractNetgsmReport
8
{
9
    protected $url = 'sms/report';
10
11
    /**
12
     * @var array
13
     */
14
    protected $errorCodes = [
15
        30, 70, 100, 101,
16
    ];
17
18
    /**
19
     * @var array
20
     */
21
    protected $noResultCodes = [
22
        60,
23
    ];
24
    /**
25
     * @var array
26
     */
27
    protected $filters = [
28
        'type'    => 2,
29
        'version' => 2,
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    protected $columnMap = [
36
        0 => [
37
            'jobId'  => 'integer',
38
            'phone'  => 'string',
39
            'status' => 'integer',
40
        ],
41
        1 => [
42
            'jobId'  => 'integer',
43
            'phone'  => 'string',
44
            'status' => 'integer',
45
        ],
46
        2 => [
47
            'jobId'        => 'integer',
48
            'phone'        => 'string',
49
            'status'       => 'integer',
50
            'operatorCode' => 'integer',
51
            'length'       => 'integer',
52
            'startDate'    => 'date',
53
            'startTime'    => 'date',
54
            'errorCode'    => 'integer',
55
        ],
56
        3 => [
57
            'jobId'  => 'integer',
58
            'phone'  => 'string',
59
            'status' => 'integer',
60
        ],
61
    ];
62
63
    /**
64
     * @var array
65
     */
66
    protected $columns = [
67
        'jobId'        => null,
68
        'phone'        => null,
69
        'status'       => null,
70
        'operatorCode' => null,
71
        'length'       => null,
72
        'startDate'    => null,
73
        'startTime'    => null,
74
        'errorCode'    => null,
75
    ];
76
77
    /**
78
     * Sets the Netgsm service bulkId
79
     * If bulkId is set, type value is set to 1.
80
     * @see https://www.netgsm.com.tr/dokuman/#http-get-rapor
81
     *
82
     * @param  string  $bulkId
83
     * @return AbstractNetgsmReport
84
     */
85
    public function setBulkId($bulkId): AbstractNetgsmReport
86
    {
87
        parent::setBulkId($bulkId);
88
89
        $this->setType(1);
90
91
        return $this;
92
    }
93
94
    /**
95
     * Formats the fields and adds them according to the api version defined.
96
     *
97
     * @param  string  $line
98
     * @return array
99
     */
100
    protected function processRow(string $line): array
101
    {
102
        $item = [];
103
        $version = $this->filters['version'];
104
        $columnMapByVersion = $this->columnMap[$version];
105
        $lineColumns = explode(' ', $line);
106
        foreach ($this->columns as $column => $val) {
107
            $columnPos = array_search($column, array_keys($columnMapByVersion));
108
            $columnValue = $columnPos !== false ?
109
                $this->formatValue($lineColumns[$columnPos],
110
                    $columnMapByVersion[$column]) : null;
111
            $item[$column] = $columnValue;
112
        }
113
114
        return $item;
115
    }
116
117
    /**
118
     * parses the report result from the api response and returns the sms report as a collection.
119
     *
120
     * @param  string  $response
121
     * @return Collection
122
     * @throws \TarfinLabs\Netgsm\Exceptions\ReportException
123
     */
124
    public function parseResponse(string $response): Collection
125
    {
126
        $collection = new Collection();
127
        if ($this->validateResponse($response)) {
128
            $response = rtrim($response, '<br>');
129
            $lines = explode('<br>', $response);
130
            foreach ($lines as $line) {
131
                $item = $this->processRow($line);
132
                $collection->push($item);
133
            }
134
        }
135
136
        return $collection;
137
    }
138
}
139