Passed
Push — master ( a99763...fbfe14 )
by Andrey
03:27
created

InternetDocument::getPDF()   C

Complexity

Conditions 13
Paths 192

Size

Total Lines 72
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 42
nc 192
nop 2
dl 0
loc 72
rs 5.85
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Daaner\NovaPoshta\Models;
4
5
use Carbon\Carbon;
6
use Daaner\NovaPoshta\NovaPoshta;
7
use Daaner\NovaPoshta\Traits\DateTimes;
8
use Daaner\NovaPoshta\Traits\DocumentList;
9
use Daaner\NovaPoshta\Traits\InternetDocumentProperty;
10
use Daaner\NovaPoshta\Traits\Limit;
11
use Daaner\NovaPoshta\Traits\OptionsSeatProperty;
12
use Daaner\NovaPoshta\Traits\PDFDocumentProperty;
13
use Daaner\NovaPoshta\Traits\RecipientProperty;
14
use Daaner\NovaPoshta\Traits\SenderProperty;
15
use Illuminate\Contracts\Container\BindingResolutionException;
16
use Illuminate\Support\Facades\Date;
17
18
class InternetDocument extends NovaPoshta
19
{
20
    /**
21
     * getDocumentList
22
     * getMoneyTransferDocuments.
23
     */
24
    use Limit, DateTimes;
25
26
    /**
27
     * getDocumentList.
28
     */
29
    use DocumentList;
30
31
    /**
32
     * save.
33
     */
34
    use InternetDocumentProperty, SenderProperty, OptionsSeatProperty, RecipientProperty;
35
36
    /**
37
     * getPDF.
38
     */
39
    use PDFDocumentProperty;
40
41
    protected $model = 'InternetDocument';
42
    protected $calledMethod;
43
    protected $methodProperties = [];
44
45
    /**
46
     * Получить список ЭН.
47
     *
48
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a9d22b34-8512-11ec-8ced-005056b2dbe1 Получить список ЭН
49
     *
50
     * @return array
51
     */
52
    public function getDocumentList(): array
53
    {
54
        $this->calledMethod = 'getDocumentList';
55
56
        $this->getPage();
57
        $this->getLimit();
58
59
        //DateTime
60
        $this->getDateTime();
61
        $this->getDateTimeFromTo();
62
63
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties);
64
    }
65
66
    /**
67
     * Создать экспресс-накладную.
68
     *
69
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a965630e-8512-11ec-8ced-005056b2dbe1 Создать экспресс-накладную
70
     *
71
     * @param  string|null  $description  Описание посылки
72
     * @return array
73
     */
74
    public function save(string $description = null): array
75
    {
76
        $this->calledMethod = 'save';
77
78
        $this->getPayerType();
79
        $this->getServiceType();
80
        $this->getPaymentMethod();
81
        $this->getCargoType();
82
83
        $this->getDateTime();
84
        $this->setDescription($description);
85
        $this->getSeatsAmount();
86
        $this->getCost();
87
        $this->getOptionsSeat();
88
89
        //Отправитель и другое
90
        $this->getSender();
91
        $this->getRecipientType();
92
        $this->getBackwardDeliveryData();
93
        $this->getNote();
94
        $this->getAdditionalInformation();
95
96
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties);
97
    }
98
99
    /**
100
     * Удаление экспресс-накладной.
101
     *
102
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a9f43ff1-8512-11ec-8ced-005056b2dbe1 Удаление экспресс-накладной
103
     *
104
     * @param  string|array  $DocumentRefs  Ref или массив Ref ТТН
105
     * @return array
106
     */
107
    public function delete($DocumentRefs): array
108
    {
109
        $this->calledMethod = 'delete';
110
111
        if (is_array($DocumentRefs) === false) {
112
            $DocumentRefs = explode(', ', /** @scrutinizer ignore-type */ $DocumentRefs);
113
        }
114
115
        $this->methodProperties['DocumentRefs'] = array_values(/** @scrutinizer ignore-type */ $DocumentRefs);
116
117
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties);
118
    }
119
120
    /**
121
     * Получить данные о платежах за определенный период.
122
     *
123
     * @since 2022-11-06 НЕ ДОКУМЕНТИРОВАНО
124
     *
125
     * @param  null|string|Carbon|date  $dateFrom  Начиная с текущей даты
126
     * @param  null|string|Carbon|date  $dateTo  До текущей даты
127
     * @return array
128
     */
129
    public function getMoneyTransferDocuments($dateFrom = null, $dateTo = null): array
130
    {
131
        $this->calledMethod = 'getMoneyTransferDocuments';
132
133
        $this->getLimit();
134
        $this->getPage();
135
        $this->getDateFromTo($dateFrom, $dateTo);
136
137
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties);
138
    }
139
140
    /**
141
     * Редактирование экспресс-накладной.
142
     *
143
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a98a4354-8512-11ec-8ced-005056b2dbe1 Редактирование экспресс-накладной
144
     *
145
     * @author https://github.com/seriklav/NovaPoshta
146
     *
147
     * TODO need tested
148
     *
149
     * @since НЕ ПРОВЕРЕНО
150
     *
151
     * @param  string|null  $description  Описание
152
     * @return array
153
     */
154
    public function edit(?string $description = null): array
155
    {
156
        $this->calledMethod = 'update';
157
158
        $this->getRef();
159
160
        $this->getPayerType();
161
        $this->getServiceType();
162
        $this->getPaymentMethod();
163
        $this->getCargoType();
164
165
        $this->getDateTime();
166
        $this->setDescription($description);
167
        $this->getSeatsAmount();
168
        $this->getCost();
169
        $this->getWeight();
170
//        $this->getOptionsSeat();
171
172
        //Отправитель и другое
173
        $this->getSender();
174
        $this->getRecipientType();
175
        $this->getBackwardDeliveryData();
176
        $this->getNote();
177
        $this->getAdditionalInformation();
178
179
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties);
180
    }
181
182
    /**
183
     * Прогноз даты доставки груза.
184
     *
185
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a941c714-8512-11ec-8ced-005056b2dbe1 Прогноз даты доставки груза
186
     *
187
     * @author https://github.com/seriklav/NovaPoshta
188
     *
189
     * @param  string  $CitySender  Ref города отправителя
190
     * @param  string  $CityRecipient  Ref города получателя
191
     * @param  string|null  $DateTime  Дата ориентировочной отправки
192
     * @param  string|null  $ServiceType  Тип доставки ('DoorsDoors', 'DoorsWarehouse', 'WarehouseWarehouse', 'WarehouseDoors')
193
     * @return array
194
     */
195
    public function getDocumentDeliveryDate(
196
        string $CitySender,
197
        string $CityRecipient,
198
        ?string $DateTime = null,
199
        ?string $ServiceType = null
200
    ): array {
201
        $this->calledMethod = 'getDocumentDeliveryDate';
202
203
        $this->methodProperties['CitySender'] = $CitySender;
204
        $this->methodProperties['CityRecipient'] = $CityRecipient;
205
206
        if ($DateTime) {
207
            $this->methodProperties['DateTime'] = $this->checkDate($DateTime, 'd.m.Y');
208
        }
209
210
        $this->methodProperties['ServiceType'] = $ServiceType ?? config('novaposhta.service_type');
211
212
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, false);
213
    }
214
215
    /**
216
     * Печать накладной.
217
     *
218
     * @param string|array $DocumentRefs Ref либо ТТН (можно и массивом Ref или ТТН)
219
     * @param bool $getStreamFile Получать файл сразу (false - в массиве в ключе result)
220
     * @throws BindingResolutionException
221
     */
222
    public function getPDF($DocumentRefs, bool $getStreamFile)
223
    {
224
        $this->calledMethod = 'printFull';
225
226
        if (is_array($DocumentRefs) === false) {
227
            $DocumentRefs = explode(', ', /** @scrutinizer ignore-type */ $DocumentRefs);
228
        }
229
230
        $this->getPrintForm();
231
232
        /**
233
         * Форсирование параметров, при определенных условиях.
234
         */
235
        if($this->printForm == 'ScanSheet') {
236
            $this->setThisIsScansheet();
237
238
            $this->methodProperties['PrintOrientation'] = $this->PrintOrientation;
239
            $this->Type = 'pdf';
240
241
            $this->PageFormat = null;
242
            $this->Position = 1;
243
            $this->Copies = 1;
244
        }
245
246
        if($this->printForm == 'Document_new') {
247
            $this->Type = 'pdf';
248
249
            $this->Copies = $this->Copies ?: 1;
250
            $this->PageFormat = $this->PageFormat ?: 'A4';
251
        }
252
253
        if($this->printForm == 'Marking_85x85') {
254
            $this->PageFormat = 'A4';
255
            $this->Type = 'pdf8';
256
        }
257
258
        if($this->printForm == 'Marking_100x100') {
259
            $this->PageFormat = null;
260
            $this->Type = 'pdf';
261
            $this->Position = '';
262
        }
263
264
        if ($this->isScansheet) {
265
            $this->methodProperties['ScanSheetRefs'] = $DocumentRefs;
266
        } else {
267
            $this->methodProperties['DocumentRefs'] = $DocumentRefs;
268
        }
269
270
        $this->getPageFormat();
271
        $this->getCopies();
272
        $this->getPosition();
273
        $this->getType();
274
275
        if ($getStreamFile) {
276
            $data = $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
277
278
            $file = null;
279
            $status = 500;
280
            $filename = microtime(false).'.pdf';
281
282
            if (isset($data['info']['file']) && $data['info']['file'] && $data['success']) {
283
                $file = $data['result'];
284
                $status = 200;
285
            }
286
287
            return response()->make($file, $status, [
288
                'Content-Type' => 'application/pdf',
289
                'Content-Disposition' => 'inline; filename="'.$filename.'"'
290
            ]);
291
        }
292
293
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
294
    }
295
}
296