InternetDocument::getPDF()   C
last analyzed

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
     * @since 2022-11-07
50
     *
51
     * @return array
52
     */
53
    public function getDocumentList(): array
54
    {
55
        $this->calledMethod = 'getDocumentList';
56
57
        $this->getPage();
58
        $this->getLimit();
59
60
        //DateTime
61
        $this->getDateTime();
62
        $this->getDateTimeFromTo();
63
64
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
65
    }
66
67
    /**
68
     * Создать экспресс-накладную.
69
     *
70
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a965630e-8512-11ec-8ced-005056b2dbe1 Создать экспресс-накладную
71
     * @since 2022-11-07
72
     *
73
     * @param  string|null  $description  Описание посылки
74
     * @return array
75
     */
76
    public function save(string $description = null): array
77
    {
78
        $this->calledMethod = 'save';
79
80
        $this->getPayerType();
81
        $this->getServiceType();
82
        $this->getPaymentMethod();
83
        $this->getCargoType();
84
85
        $this->getDateTime();
86
        $this->setDescription($description);
87
        $this->getSeatsAmount();
88
        $this->getCost();
89
        $this->getOptionsSeat();
90
91
        //Отправитель и другое
92
        $this->getSender();
93
        $this->getRecipientType();
94
        $this->getBackwardDeliveryData();
95
        $this->getNote();
96
        $this->getAdditionalInformation();
97
98
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
99
    }
100
101
    /**
102
     * Удаление экспресс-накладной.
103
     *
104
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a9f43ff1-8512-11ec-8ced-005056b2dbe1 Удаление экспресс-накладной
105
     * @since 2022-11-07
106
     *
107
     * @param  string|array  $DocumentRefs  Ref или массив Ref ТТН
108
     * @return array
109
     */
110
    public function delete($DocumentRefs): array
111
    {
112
        $this->calledMethod = 'delete';
113
114
        if (is_array($DocumentRefs) === false) {
115
            $DocumentRefs = explode(', ', /** @scrutinizer ignore-type */ $DocumentRefs);
116
        }
117
118
        $this->methodProperties['DocumentRefs'] = array_values(/** @scrutinizer ignore-type */ $DocumentRefs);
119
120
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
121
    }
122
123
    /**
124
     * Получить данные о платежах за определенный период.
125
     *
126
     * @since 2022-11-07 НЕ ДОКУМЕНТИРОВАНО
127
     *
128
     * @param  null|string|Carbon|date  $dateFrom  Начиная с текущей даты
129
     * @param  null|string|Carbon|date  $dateTo  До текущей даты
130
     * @return array
131
     */
132
    public function getMoneyTransferDocuments($dateFrom = null, $dateTo = null): array
133
    {
134
        $this->calledMethod = 'getMoneyTransferDocuments';
135
136
        $this->getLimit();
137
        $this->getPage();
138
        $this->getDateFromTo($dateFrom, $dateTo);
139
140
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
141
    }
142
143
    /**
144
     * Редактирование экспресс-накладной.
145
     *
146
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a98a4354-8512-11ec-8ced-005056b2dbe1 Редактирование экспресс-накладной
147
     *
148
     * @author https://github.com/seriklav/NovaPoshta
149
     *
150
     * TODO need tested
151
     *
152
     * @since НЕ ПРОВЕРЕНО
153
     *
154
     * @param  string|null  $description  Описание
155
     * @return array
156
     */
157
    public function edit(?string $description = null): array
158
    {
159
        $this->calledMethod = 'update';
160
161
        $this->getRef();
162
163
        $this->getPayerType();
164
        $this->getServiceType();
165
        $this->getPaymentMethod();
166
        $this->getCargoType();
167
168
        $this->getDateTime();
169
        $this->setDescription($description);
170
        $this->getSeatsAmount();
171
        $this->getCost();
172
        $this->getWeight();
173
//        $this->getOptionsSeat();
174
175
        //Отправитель и другое
176
        $this->getSender();
177
        $this->getRecipientType();
178
        $this->getBackwardDeliveryData();
179
        $this->getNote();
180
        $this->getAdditionalInformation();
181
182
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
183
    }
184
185
    /**
186
     * Прогноз даты доставки груза.
187
     *
188
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a941c714-8512-11ec-8ced-005056b2dbe1 Прогноз даты доставки груза
189
     * @since 2022-11-07
190
     *
191
     * @author https://github.com/seriklav/NovaPoshta
192
     *
193
     * @param  string  $CitySender  Ref города отправителя
194
     * @param  string  $CityRecipient  Ref города получателя
195
     * @return array
196
     */
197
    public function getDocumentDeliveryDate(string $CitySender, string $CityRecipient): array
198
    {
199
        $this->calledMethod = 'getDocumentDeliveryDate';
200
201
        $this->methodProperties['CitySender'] = $CitySender;
202
        $this->methodProperties['CityRecipient'] = $CityRecipient;
203
204
        $this->getDateTime();
205
        $this->getServiceType();
206
207
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, false);
208
    }
209
210
    /**
211
     * Прогноз стоимости доставки груза.
212
     *
213
     * @see https://developers.novaposhta.ua/view/model/a90d323c-8512-11ec-8ced-005056b2dbe1/method/a91f115b-8512-11ec-8ced-005056b2dbe1 Прогноз стоимости доставки груза
214
     * @since 2022-11-07
215
     *
216
     * @param  string  $CitySender  Ref города отправителя
217
     * @param  string  $CityRecipient  Ref города получателя
218
     * @return array
219
     */
220
    public function getDocumentPrice(string $CitySender, string $CityRecipient): array
221
    {
222
        $this->calledMethod = 'getDocumentPrice';
223
224
        $this->methodProperties['CitySender'] = $CitySender;
225
        $this->methodProperties['CityRecipient'] = $CityRecipient;
226
227
        $this->getWeight();
228
        $this->getDateTime();
229
        $this->getCargoType();
230
        $this->getServiceType();
231
        $this->getCost();
232
233
        /**
234
         * TODO Есть еще куча всяких штук, я не добавил.
235
         */
236
237
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, false);
238
    }
239
240
    /**
241
     * Печать накладной.
242
     *
243
     * @since 2022-11-07
244
     *
245
     * @param  string|array  $DocumentRefs  Ref либо ТТН (можно и массивом Ref или ТТН)
246
     * @param  bool  $getStreamFile  Получать файл сразу (false - в массиве в ключе result)
247
     *
248
     * @throws BindingResolutionException
249
     */
250
    public function getPDF($DocumentRefs, bool $getStreamFile)
251
    {
252
        $this->calledMethod = 'printFull';
253
254
        if (is_array($DocumentRefs) === false) {
255
            $DocumentRefs = explode(', ', /** @scrutinizer ignore-type */ $DocumentRefs);
256
        }
257
258
        $this->getPrintForm();
259
260
        /**
261
         * Форсирование параметров, при определенных условиях.
262
         */
263
        if ($this->printForm == 'ScanSheet') {
264
            $this->setThisIsScansheet();
265
266
            $this->methodProperties['PrintOrientation'] = $this->PrintOrientation;
267
            $this->Type = 'pdf';
268
269
            $this->PageFormat = null;
270
            $this->Position = 1;
271
            $this->Copies = 1;
272
        }
273
274
        if ($this->printForm == 'Document_new') {
275
            $this->Type = 'pdf';
276
277
            $this->Copies = $this->Copies ?: 1;
278
            $this->PageFormat = $this->PageFormat ?: 'A4';
279
        }
280
281
        if ($this->printForm == 'Marking_85x85') {
282
            $this->PageFormat = 'A4';
283
            $this->Type = 'pdf8';
284
        }
285
286
        if ($this->printForm == 'Marking_100x100') {
287
            $this->PageFormat = null;
288
            $this->Type = 'pdf';
289
            $this->Position = '';
290
        }
291
292
        if ($this->isScansheet) {
293
            $this->methodProperties['ScanSheetRefs'] = array_values(/** @scrutinizer ignore-type */ $DocumentRefs);
294
        } else {
295
            $this->methodProperties['DocumentRefs'] = array_values(/** @scrutinizer ignore-type */ $DocumentRefs);
296
        }
297
298
        $this->getPageFormat();
299
        $this->getCopies();
300
        $this->getPosition();
301
        $this->getType();
302
303
        if ($getStreamFile) {
304
            $data = $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
305
306
            $file = null;
307
            $status = 500;
308
            $filename = microtime(false).'.pdf';
309
310
            if (isset($data['info']['file']) && $data['info']['file'] && $data['success']) {
311
                $file = $data['result'];
312
                $status = 200;
313
            }
314
315
            return response()->make($file, $status, [
316
                'Content-Type' => 'application/pdf',
317
                'Content-Disposition' => 'inline; filename="'.$filename.'"',
318
            ]);
319
        }
320
321
        return $this->getResponse($this->model, $this->calledMethod, $this->methodProperties, true);
322
    }
323
}
324