Test Failed
Push — master ( 8ec047...237146 )
by Vítězslav
06:06
created

FlexiBeeRO::loadFromFlexiBee()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * FlexiPeeHP - Read Only Access to FlexiBee class.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2015-2017 Spoje.Net
7
 */
8
9
namespace FlexiPeeHP;
10
11
/**
12
 * Základní třída pro čtení z FlexiBee
13
 *
14
 * @url https://demo.flexibee.eu/devdoc/
15
 */
16
class FlexiBeeRO extends \Ease\Brick
17
{
18
    /**
19
     * Version of FlexiPeeHP library
20
     *
21
     * @var string
22
     */
23
    public static $libVersion = '1.6.4.2';
24
25
    /**
26
     * Základní namespace pro komunikaci s FlexiBee.
27
     * Basic namespace for communication with FlexiBee
28
     *
29
     * @var string Jmený prostor datového bloku odpovědi
30
     */
31
    public $nameSpace = 'winstrom';
32
33
    /**
34
     * URL of object data in FlexiBee
35
     * @var string url
36
     */
37
    public $apiURL = null;
38
39
    /**
40
     * Datový blok v poli odpovědi.
41
     * Data block in response field.
42
     *
43
     * @var string
44
     */
45
    public $resultField = 'results';
46
47
    /**
48
     * Verze protokolu použitého pro komunikaci.
49
     * Communication protocol version used.
50
     *
51
     * @var string Verze použitého API
52
     */
53
    public $protoVersion = '1.0';
54
55
    /**
56
     * Evidence užitá objektem.
57
     * Evidence used by object
58
     *
59
     * @link https://demo.flexibee.eu/c/demo/evidence-list Přehled evidencí
60
     * @var string
61
     */
62
    public $evidence = null;
63
64
    /**
65
     * Výchozí formát pro komunikaci.
66
     * Default communication format.
67
     *
68
     * @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů
69
     *
70
     * @var string json|xml|...
71
     */
72
    public $format = 'json';
73
74
    /**
75
     * formát příchozí odpovědi
76
     * response format
77
     *
78
     * @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů
79
     *
80
     * @var string json|xml|...
81
     */
82
    public $responseFormat = 'json';
83
84
    /**
85
     * Curl Handle.
86
     *
87
     * @var resource
88
     */
89
    public $curl = null;
90
91
    /**
92
     * @link https://demo.flexibee.eu/devdoc/company-identifier Identifikátor firmy
93
     * @var string
94
     */
95
    public $company = null;
96
97
    /**
98
     * Server[:port]
99
     * @var string
100
     */
101
    public $url = null;
102
103
    /**
104
     * REST API Username
105
     * @var string
106
     */
107
    public $user = null;
108
109
    /**
110
     * REST API Password
111
     * @var string
112
     */
113
    public $password = null;
114
115
    /**
116
     * @var array Pole HTTP hlaviček odesílaných s každým požadavkem
117
     */
118
    public $defaultHttpHeaders = ['User-Agent' => 'FlexiPeeHP'];
119
120
    /**
121
     * Default additional request url parameters after question mark
122
     *
123
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls   Common params
124
     * @link https://www.flexibee.eu/api/dokumentace/ref/paging Paging params
125
     * @var array
126
     */
127
    public $defaultUrlParams = ['limit' => 0];
128
129
    /**
130
     * Identifikační řetězec.
131
     *
132
     * @var string
133
     */
134
    public $init = null;
135
136
    /**
137
     * Sloupeček s názvem.
138
     *
139
     * @var string
140
     */
141
    public $nameColumn = 'nazev';
142
143
    /**
144
     * Sloupeček obsahující datum vložení záznamu do shopu.
145
     *
146
     * @var string
147
     */
148
    public $myCreateColumn = 'false';
149
150
    /**
151
     * Slopecek obsahujici datum poslení modifikace záznamu do shopu.
152
     *
153
     * @var string
154
     */
155
    public $myLastModifiedColumn = 'lastUpdate';
156
157
    /**
158
     * Klíčový idendifikátor záznamu.
159
     *
160
     * @var string
161
     */
162
    public $fbKeyColumn = 'id';
163
164
    /**
165
     * Informace o posledním HTTP requestu.
166
     *
167
     * @var *
168
     */
169
    public $curlInfo;
170
171
    /**
172
     * Informace o poslední HTTP chybě.
173
     *
174
     * @var string
175
     */
176
    public $lastCurlError = null;
177
178
    /**
179
     * Used codes storage.
180
     *
181
     * @var array
182
     */
183
    public $codes = null;
184
185
    /**
186
     * Last Inserted ID.
187
     *
188
     * @var int
189
     */
190
    public $lastInsertedID = null;
191
192
    /**
193
     * Default Line Prefix.
194
     *
195
     * @var string
196
     */
197
    public $prefix = '/c/';
198
199
    /**
200
     * Raw Content of last curl response
201
     *
202
     * @var string
203
     */
204
    public $lastCurlResponse;
205
206
    /**
207
     * HTTP Response code of last request
208
     *
209
     * @var int
210
     */
211
    public $lastResponseCode = null;
212
213
    /**
214
     * Body data  for next curl POST operation
215
     *
216
     * @var string
217
     */
218
    protected $postFields = null;
219
220
    /**
221
     * Last operation result data or message(s)
222
     *
223
     * @var array
224
     */
225
    public $lastResult = null;
226
227
    /**
228
     * Nuber from  @rowCount
229
     * @var int
230
     */
231
    public $rowCount = null;
232
233
    /**
234
     * @link https://www.flexibee.eu/api/dokumentace/ref/zamykani-odemykani/
235
     * @var string filter query
236
     */
237
    public $filter;
238
239
    /**
240
     * @link https://demo.flexibee.eu/devdoc/actions Provádění akcí
241
     * @var string
242
     */
243
    protected $action;
244
245
    /**
246
     * Pole akcí které podporuje ta která evidence
247
     * @link https://demo.flexibee.eu/c/demo/faktura-vydana/actions.json Např. Akce faktury
248
     * @var array
249
     */
250
    public $actionsAvailable = null;
251
252
    /**
253
     * Parmetry pro URL
254
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Všechny podporované parametry
255
     * @var array
256
     */
257
    public $urlParams = [
258
        'idUcetniObdobi',
259
        'dry-run',
260
        'fail-on-warning',
261
        'report-name',
262
        'report-lang',
263
        'report-sign',
264
        'detail', //See: https://www.flexibee.eu/api/dokumentace/ref/detail-levels
265
        'mode',
266
        'limit',
267
        'start',
268
        'order',
269
        'sort',
270
        'add-row-count',
271
        'relations',
272
        'includes',
273
        'use-ext-id',
274
        'use-internal-id',
275
        'stitky-as-ids',
276
        'only-ext-ids',
277
        'no-ext-ids',
278
        'no-ids',
279
        'code-as-id',
280
        'no-http-errors',
281
        'export-settings',
282
        'as-gui',
283
        'code-in-response',
284
        'add-global-version',
285
        'encoding',
286
        'delimeter',
287
        'format',
288
        'auth',
289
        'skupina-stitku',
290
        'dir',
291
        'relations',
292
        'relations',
293
        'xpath', // See: https://www.flexibee.eu/api/dokumentace/ref/xpath/
294
        'dry-run', // See: https://www.flexibee.eu/api/dokumentace/ref/dry-run/
295
        'inDesktopApp' // Note: Undocumented function (html only)
296
    ];
297
298
    /**
299
     * Save 404 results to log ?
300
     * @var boolean
301
     */
302
    protected $ignoreNotFound = false;
303
304
    /**
305
     * Array of errors caused by last request
306
     * @var array
307
     */
308
    private $errors = [];
309
310
    /**
311
     * List of Error500 reports sent
312
     * @var array
313
     */
314
    private $reports = [];
315
316
    /**
317
     * Send Error500 Report to
318
     * @var string email address
319
     */
320
    public $reportRecipient = '[email protected]';
321
322
    /**
323
     * Class for read only interaction with FlexiBee.
324
     *
325
     * @param mixed $init default record id or initial data
326
     * @param array $options Connection settings override
327
     */
328
    public function __construct($init = null, $options = [])
329
    {
330
        $this->init = $init;
331
332
        parent::__construct();
333
        $this->setUp($options);
334
        $this->curlInit();
335
        if (!empty($init)) {
336
            $this->processInit($init);
337
        }
338
    }
339
340
    /**
341
     * SetUp Object to be ready for connect
342
     *
343
     * @param array $options Object Options (company,url,user,password,evidence,
344
     *                                       prefix,defaultUrlParams,debug)
345
     */
346
    public function setUp($options = [])
347
    {
348
        $this->setupProperty($options, 'company', 'FLEXIBEE_COMPANY');
349
        $this->setupProperty($options, 'url', 'FLEXIBEE_URL');
350
        $this->setupProperty($options, 'user', 'FLEXIBEE_LOGIN');
351
        $this->setupProperty($options, 'password', 'FLEXIBEE_PASSWORD');
352
        if (isset($options['evidence'])) {
353
            $this->setEvidence($options['evidence']);
354
        }
355
        $this->setupProperty($options, 'defaultUrlParams');
356
        if (isset($options['prefix'])) {
357
            $this->setPrefix($options['prefix']);
358
        }
359
        $this->setupProperty($options, 'debug');
360
        $this->updateApiURL();
361
    }
362
363
    /**
364
     * Set up one of properties
365
     *
366
     * @param array  $options  array of given properties
367
     * @param string $name     name of property to process
368
     * @param string $constant load default property value from constant
369
     */
370
    public function setupProperty($options, $name, $constant = null)
371
    {
372
        if (isset($options[$name])) {
373
            $this->$name = $options[$name];
374
        } else {
375
            if (is_null($this->$name) && !empty($constant) && defined($constant)) {
376
                $this->$name = constant($constant);
377
            }
378
        }
379
    }
380
381
    /**
382
     * Inicializace CURL
383
     */
384
    public function curlInit()
385
    {
386
        $this->curl = \curl_init(); // create curl resource
387
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); // return content as a string from curl_exec
388
        curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); // follow redirects (compatibility for future changes in FlexiBee)
389
        curl_setopt($this->curl, CURLOPT_HTTPAUTH, true);       // HTTP authentication
390
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); // FlexiBee by default uses Self-Signed certificates
391
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
392
        curl_setopt($this->curl, CURLOPT_VERBOSE, ($this->debug === true)); // For debugging
393
        curl_setopt($this->curl, CURLOPT_USERPWD,
394
            $this->user.':'.$this->password); // set username and password
395
    }
396
397
    /**
398
     * Zinicializuje objekt dle daných dat. Možné hodnoty:
399
     *
400
     *  * 234                              - interní číslo záznamu k načtení
401
     *  * code:LOPATA                      - kód záznamu
402
     *  * BAGR                             - kód záznamu ka načtení
403
     *  * ['id'=>24,'nazev'=>'hoblík']     - pole hodnot k předvyplnění
404
     *  * 743.json?relations=adresa,vazby  - část url s parametry k načtení
405
     *
406
     * @param mixed $init číslo/"(code:)kód"/(část)URI záznamu k načtení | pole hodnot k předvyplnění
407
     */
408
    public function processInit($init)
409
    {
410
        if (is_integer($init)) {
411
            $this->loadFromFlexiBee($init);
412
        } elseif (is_array($init)) {
413
            $this->takeData($init);
414
        } elseif (preg_match('/\.(json|xml|csv)/', $init)) {
415
            $this->takeData($this->getFlexiData((($init[0] != '/') ? $this->getEvidenceURL($init)
0 ignored issues
show
Unused Code introduced by
The call to FlexiBeeRO::getEvidenceURL() has too many arguments starting with $init.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
416
                            : $init)));
417
        } else {
418
            $this->loadFromFlexiBee('code:'.str_replace('code:', '', $init));
419
        }
420
    }
421
422
    /**
423
     * Set URL prefix
424
     *
425
     * @param string $prefix
426
     */
427
    public function setPrefix($prefix)
428
    {
429
        switch ($prefix) {
430
            case 'a': //Access
431
            case 'c': //Company
432
            case 'u': //User
433
            case 'g': //License Groups
434
            case 'admin':
435
            case 'status':
436
            case 'login-logout':
437
                $this->prefix = '/'.$prefix.'/';
438
                break;
439
            case null:
440
            case '':
441
            case '/':
442
                $this->prefix = '';
443
                break;
444
            default:
445
                throw new \Exception(sprintf('Unknown prefix %s', $prefix));
446
        }
447
    }
448
449
    /**
450
     * Set communication format.
451
     * One of html|xml|json|csv|dbf|xls|isdoc|isdocx|edi|pdf|pdf|vcf|ical
452
     *
453
     * @param string $format
454
     * @return boolen format is availble
455
     */
456
    public function setFormat($format)
457
    {
458
        $result = true;
459
        if (($this->debug === true) && !empty($this->evidence) && isset(Formats::$$this->evidence)) {
460
            if (array_key_exists($format, array_flip(Formats::$$this->evidence))
461
                === false) {
462
                $result = false;
463
            }
464
        }
465
        if ($result === true) {
466
            $this->format = $format;
467
            $this->updateApiURL();
468
        }
469
        return $result;
470
    }
471
472
    /**
473
     * Nastaví Evidenci pro Komunikaci.
474
     * Set evidence for communication
475
     *
476
     * @param string $evidence evidence pathName to use
477
     * @return boolean evidence switching status
478
     */
479
    public function setEvidence($evidence)
480
    {
481
        switch ($this->prefix) {
482
            case '/c/':
483
                if (array_key_exists($evidence, EvidenceList::$name)) {
484
                    $this->evidence = $evidence;
485
                    $result         = true;
486
                } else {
487
                    throw new \Exception(sprintf('Try to set unsupported evidence %s',
488
                        $evidence));
489
                }
490
                break;
491
            default:
492
                $this->evidence = $evidence;
493
                $result         = true;
494
                break;
495
        }
496
        $this->updateApiURL();
497
        return $result;
498
    }
499
500
    /**
501
     * Vrací právě používanou evidenci pro komunikaci
502
     * Obtain current used evidence
503
     *
504
     * @return string
505
     */
506
    public function getEvidence()
507
    {
508
        return $this->evidence;
509
    }
510
511
    /**
512
     * Set used company.
513
     * Nastaví Firmu.
514
     *
515
     * @param string $company
516
     */
517
    public function setCompany($company)
518
    {
519
        $this->company = $company;
520
    }
521
522
    /**
523
     * Obtain company now used
524
     * Vrací právě používanou firmu
525
     *
526
     * @return string
527
     */
528
    public function getCompany()
529
    {
530
        return $this->company;
531
    }
532
533
    /**
534
     * Vrací název evidence použité v odpovědích z FlexiBee
535
     *
536
     * @return string
537
     */
538
    public function getResponseEvidence()
539
    {
540
        switch ($this->evidence) {
541
            case 'c':
542
                $evidence = 'company';
543
                break;
544
            case 'evidence-list':
545
                $evidence = 'evidence';
546
                break;
547
            default:
548
                $evidence = $this->getEvidence();
549
                break;
550
        }
551
        return $evidence;
552
    }
553
554
    /**
555
     * Převede rekurzivně Objekt na pole.
556
     *
557
     * @param object|array $object
558
     *
559
     * @return array
560
     */
561
    public static function object2array($object)
562
    {
563
        $result = null;
564
        if (is_object($object)) {
565
            $objectData = get_object_vars($object);
566
            if (is_array($objectData) && count($objectData)) {
567
                $result = array_map('self::object2array', $objectData);
568
            }
569
        } else {
570
            if (is_array($object)) {
571
                foreach ($object as $item => $value) {
572
                    $result[$item] = self::object2array($value);
573
                }
574
            } else {
575
                $result = $object;
576
            }
577
        }
578
579
        return $result;
580
    }
581
582
    /**
583
     * Převede rekurzivně v poli všechny objekty na jejich identifikátory.
584
     *
585
     * @param object|array $object
586
     *
587
     * @return array
588
     */
589
    public static function objectToID($object)
590
    {
591
        $resultID = null;
592
        if (is_object($object)) {
593
            $resultID = $object->__toString();
594
        } else {
595
            if (is_array($object)) {
596
                foreach ($object as $item => $value) {
597
                    $resultID[$item] = self::objectToID($value);
598
                }
599
            } else { //String
600
                $resultID = $object;
601
            }
602
        }
603
604
        return $resultID;
605
    }
606
607
    /**
608
     * Return basic URL for used Evidence
609
     *
610
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
611
     *
612
     * @return string Evidence URL
613
     */
614
    public function getEvidenceURL()
615
    {
616
        $evidenceUrl = $this->url.$this->prefix.$this->company;
617
        $evidence    = $this->getEvidence();
618
        if (!empty($evidence)) {
619
            $evidenceUrl .= '/'.$evidence;
620
        }
621
        return $evidenceUrl;
622
    }
623
624
    /**
625
     * Add suffix to Evidence URL
626
     *
627
     * @param string $urlSuffix
628
     *
629
     * @return string
630
     */
631
    public function evidenceUrlWithSuffix($urlSuffix)
632
    {
633
        $evidenceUrl = $this->getEvidenceUrl();
634
        if (!empty($urlSuffix)) {
635
            if (($urlSuffix[0] != '/') && ($urlSuffix[0] != ';') && ($urlSuffix[0]
636
                != '?')) {
637
                $evidenceUrl .= '/';
638
            }
639
            $evidenceUrl .= $urlSuffix;
640
        }
641
        return $evidenceUrl;
642
    }
643
644
    /**
645
     * Update $this->apiURL
646
     */
647
    public function updateApiURL()
648
    {
649
        $this->apiURL = $this->getEvidenceURL();
650
        $id           = $this->__toString();
651
        if (!empty($id)) {
652
            $this->apiURL .= '/'.urlencode($id);
653
        }
654
        $this->apiURL .= '.'.$this->format;
655
    }
656
657
    /**
658
     * Funkce, která provede I/O operaci a vyhodnotí výsledek.
659
     *
660
     * @param string $urlSuffix část URL za identifikátorem firmy.
661
     * @param string $method    HTTP/REST metoda
662
     * @param string $format    Requested format
663
     * @return array|boolean Výsledek operace
664
     */
665
    public function performRequest($urlSuffix = null, $method = 'GET',
666
                                   $format = null)
667
    {
668
        $this->rowCount = null;
669
670
        if (preg_match('/^http/', $urlSuffix)) {
671
            $url = $urlSuffix;
672
        } elseif ($urlSuffix[0] == '/') {
673
            $url = $this->url.$urlSuffix;
674
        } else {
675
            $url = $this->evidenceUrlWithSuffix($urlSuffix);
676
        }
677
678
        $responseCode = $this->doCurlRequest($url, $method, $format);
679
680
        return strlen($this->lastCurlResponse) ? $this->parseResponse($this->rawResponseToArray($this->lastCurlResponse,
681
                    $this->responseFormat), $responseCode) : null;
682
    }
683
684
    /**
685
     * Parse Raw FlexiBee response in several formats
686
     *
687
     * @param string $responseRaw raw response body
688
     * @param string $format      Raw Response format json|xml|etc
689
     *
690
     * @return array
691
     */
692
    public function rawResponseToArray($responseRaw, $format)
693
    {
694
        switch ($format) {
695
            case 'json':
696
                $responseDecoded = json_decode($responseRaw, true, 10);
697
                $decodeError     = json_last_error_msg();
698
                if ($decodeError == 'No error') {
699
                    if (array_key_exists($this->nameSpace, $responseDecoded)) {
700
                        $responseDecoded = $responseDecoded[$this->nameSpace];
701
                    }
702
                } else {
703
                    $this->addStatusMessage('JSON Decoder: '.$decodeError,
704
                        'error');
705
                    $this->addStatusMessage($responseRaw, 'debug');
706
                }
707
                break;
708
            case 'xml':
709
                $responseDecoded = self::xml2array($this->lastCurlResponse);
710
                break;
711
            case 'txt':
712
            default:
713
                $responseDecoded = $this->lastCurlResponse;
714
                break;
715
        }
716
        return $responseDecoded;
717
    }
718
719
    /**
720
     * Parse Response array
721
     *
722
     * @param array $responseDecoded
723
     * @param int $responseCode Request Response Code
724
     *
725
     * @return array main data part of response
726
     */
727
    public function parseResponse($responseDecoded, $responseCode)
728
    {
729
        $response = null;
730
        switch ($responseCode) {
731
            case 201: //Success Write
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
732
                if (isset($responseDecoded[$this->resultField][0]['id'])) {
733
                    $this->lastInsertedID = $responseDecoded[$this->resultField][0]['id'];
734
                    $this->setMyKey($this->lastInsertedID);
735
                    $this->apiURL         = $this->getEvidenceURL().'/'.$this->lastInsertedID;
736
                } else {
737
                    $this->lastInsertedID = null;
738
                }
739
            case 200: //Success Read
740
                $response         = $this->lastResult = $this->unifyResponseFormat($responseDecoded);
741
                if (isset($responseDecoded['@rowCount'])) {
742
                    $this->rowCount = (int) $responseDecoded['@rowCount'];
743
                }
744
                break;
745
746
            case 500: // Internal Server Error
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
747
                if ($this->debug === true) {
748
                    $this->error500Reporter($responseDecoded);
749
                }
750
            case 404: // Page not found
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
751
                if ($this->ignoreNotFound === true) {
752
                    break;
753
                }
754
            case 400: //Bad Request parameters
755
            default: //Something goes wrong
756
                $this->addStatusMessage($this->curlInfo['url'], 'warning');
757
                if (is_array($responseDecoded)) {
758
                    $this->parseError($responseDecoded);
759
                }
760
                $this->logResult($responseDecoded, $this->curlInfo['url']);
761
                break;
762
        }
763
        return $response;
764
    }
765
766
    /**
767
     * Parse error message response
768
     *
769
     * @param array $responseDecoded
770
     * @return int number of errors processed
771
     */
772
    public function parseError(array $responseDecoded)
773
    {
774
        if (array_key_exists('results', $responseDecoded)) {
775
            $this->errors = $responseDecoded['results'][0]['errors'];
776
        } else {
777
            if (array_key_exists('message', $responseDecoded)) {
778
                $this->errors = [['message' => $responseDecoded['message']]];
779
            }
780
        }
781
        return count($this->errors);
782
    }
783
784
    /**
785
     * Vykonej HTTP požadavek
786
     *
787
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
788
     * @param string $url    URL požadavku
789
     * @param string $method HTTP Method GET|POST|PUT|OPTIONS|DELETE
790
     * @param string $format požadovaný formát komunikace
791
     * @return int HTTP Response CODE
792
     */
793
    public function doCurlRequest($url, $method, $format = null)
794
    {
795
        if (is_null($format)) {
796
            $format = $this->format;
797
        }
798
        curl_setopt($this->curl, CURLOPT_URL, $url);
799
// Nastavení samotné operace
800
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
801
//Vždy nastavíme byť i prázná postdata jako ochranu před chybou 411
802
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postFields);
803
804
        $httpHeaders = $this->defaultHttpHeaders;
805
806
        $formats = Formats::bySuffix();
807
808
        if (!isset($httpHeaders['Accept'])) {
809
            $httpHeaders['Accept'] = $formats[$format]['content-type'];
810
        }
811
        if (!isset($httpHeaders['Content-Type'])) {
812
            $httpHeaders['Content-Type'] = $formats[$format]['content-type'];
813
        }
814
        $httpHeadersFinal = [];
815
        foreach ($httpHeaders as $key => $value) {
816
            if (($key == 'User-Agent') && ($value == 'FlexiPeeHP')) {
817
                $value .= ' v'.self::$libVersion;
818
            }
819
            $httpHeadersFinal[] = $key.': '.$value;
820
        }
821
822
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $httpHeadersFinal);
823
824
// Proveď samotnou operaci
825
        $this->lastCurlResponse            = curl_exec($this->curl);
826
        $this->curlInfo                    = curl_getinfo($this->curl);
827
        $this->curlInfo['when']            = microtime();
828
        $this->curlInfo['request_headers'] = $httpHeadersFinal;
829
        $this->responseFormat              = Formats::contentTypeToSuffix($this->curlInfo['content_type']);
830
        $this->lastResponseCode            = $this->curlInfo['http_code'];
831
        $this->lastCurlError               = curl_error($this->curl);
832
        if (strlen($this->lastCurlError)) {
833
            $this->addStatusMessage(sprintf('Curl Error (HTTP %d): %s',
834
                    $this->lastResponseCode, $this->lastCurlError), 'error');
835
        }
836
837
        if ($this->debug === true) {
838
            $this->saveDebugFiles();
839
        }
840
841
        return $this->lastResponseCode;
842
    }
843
844
    /**
845
     * Nastaví druh prováděné akce.
846
     *
847
     * @link https://demo.flexibee.eu/devdoc/actions Provádění akcí
848
     * @param string $action
849
     * @return boolean
850
     */
851
    public function setAction($action)
852
    {
853
        $result           = false;
854
        $actionsAvailable = $this->getActionsInfo();
855
        if (is_array($actionsAvailable) && array_key_exists($action,
856
                $actionsAvailable)) {
857
            $this->action = $action;
858
            $result       = true;
859
        }
860
        return $result;
861
    }
862
863
    /**
864
     * Convert XML to array.
865
     *
866
     * @param string $xml
867
     *
868
     * @return array
869
     */
870
    public static function xml2array($xml)
871
    {
872
        $arr = [];
873
874
        if (is_string($xml)) {
875
            $xml = simplexml_load_string($xml);
876
        }
877
878
        foreach ($xml->children() as $r) {
879
            if (count($r->children()) == 0) {
880
                $arr[$r->getName()] = strval($r);
881
            } else {
882
                $arr[$r->getName()][] = self::xml2array($r);
883
            }
884
        }
885
886
        return $arr;
887
    }
888
889
    /**
890
     * Odpojení od FlexiBee.
891
     */
892
    public function disconnect()
893
    {
894
        if (is_resource($this->curl)) {
895
            curl_close($this->curl);
896
        }
897
        $this->curl = null;
898
    }
899
900
    /**
901
     * Disconnect CURL befere pass away
902
     */
903
    public function __destruct()
904
    {
905
        $this->disconnect();
906
    }
907
908
    /**
909
     * Načte řádek dat z FlexiBee.
910
     *
911
     * @param int $recordID id požadovaného záznamu
912
     *
913
     * @return array
914
     */
915
    public function getFlexiRow($recordID)
916
    {
917
        $record   = null;
918
        $response = $this->performRequest($this->evidence.'/'.$recordID.'.json');
919
        if (isset($response[$this->evidence])) {
920
            $record = $response[$this->evidence][0];
921
        }
922
923
        return $record;
924
    }
925
926
    /**
927
     * Oddělí z pole podmínek ty jenž patří za ? v URL požadavku
928
     *
929
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
930
     * @param array $conditions pole podmínek   - rendrují se do ()
931
     * @param array $urlParams  pole parametrů  - rendrují za ?
932
     */
933
    public function extractUrlParams(&$conditions, &$urlParams)
934
    {
935
        foreach ($this->urlParams as $urlParam) {
936
            if (isset($conditions[$urlParam])) {
937
                \Ease\Sand::divDataArray($conditions, $urlParams, $urlParam);
938
            }
939
        }
940
    }
941
942
    /**
943
     * Načte data z FlexiBee.
944
     *
945
     * @param string $suffix     dotaz
946
     * @param string|array $conditions Volitelný filtrovací výraz
947
     */
948
    public function getFlexiData($suffix = null, $conditions = null)
949
    {
950
        $urlParams = $this->defaultUrlParams;
951
952
        if (!is_null($conditions)) {
953
            if (is_array($conditions)) {
954
                $this->extractUrlParams($conditions, $urlParams);
955
                $conditions = $this->flexiUrl($conditions);
956
            }
957
958
            if (strlen($conditions) && ($conditions[0] != '/')) {
959
                $conditions = rawurlencode('('.($conditions).')');
960
            }
961
        }
962
963
        if (strlen($suffix)) {
964
            if (preg_match('/^http/', $suffix) || ($suffix[0] == '/')) {
965
                $transactions = $this->performRequest($suffix, 'GET');
966
            } else {
967
                $transactions = $this->performRequest($conditions.'?'.$suffix.'&'.http_build_query($urlParams),
968
                    'GET');
969
            }
970
        } else {
971
            $transactions = $this->performRequest($conditions.'?'.http_build_query($urlParams),
972
                'GET');
973
        }
974
975
        $responseEvidence = $this->getResponseEvidence();
976
        if (is_array($transactions) && array_key_exists($responseEvidence,
977
                $transactions)) {
978
            $result = $transactions[$responseEvidence];
979
            if ((count($result) == 1) && (count(current($result)) == 0 )) {
980
                $result = null; // Response is empty Array
981
            }
982
        } else {
983
            $result = $transactions;
984
        }
985
986
        return $result;
987
    }
988
989
    /**
990
     * Načte záznam z FlexiBee a uloží v sobě jeho data
991
     * Read FlexiBee record and store it inside od object
992
     *
993
     * @param int $id ID or conditions
994
     *
995
     * @return int počet načtených položek
996
     */
997
    public function loadFromFlexiBee($id = null)
998
    {
999
        $data = [];
1000
        if (is_null($id)) {
1001
            $id = $this->getMyKey();
1002
        }
1003
        $flexidata    = $this->getFlexiData($this->getEvidenceUrl().'/'.$id);
1004
        $this->apiURL = $this->curlInfo['url'];
1005
        if (is_array($flexidata) && (count($flexidata) == 1)) {
1006
            $data = current($flexidata);
1007
        }
1008
        return $this->takeData($data);
1009
    }
1010
1011
    /**
1012
     * Převede data do Json formátu pro FlexiBee.
1013
     * Convert data to FlexiBee like Json format
1014
     *
1015
     * @param array $data
1016
     *
1017
     * @return string
1018
     */
1019
    public function jsonizeData($data)
1020
    {
1021
        $dataToJsonize = [
1022
            $this->nameSpace => [
1023
                '@version' => $this->protoVersion,
1024
                $this->evidence => $this->objectToID($data),
1025
            ],
1026
        ];
1027
1028 View Code Duplication
        if (!is_null($this->action)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1029
            $dataToJsonize[$this->nameSpace][$this->evidence.'@action'] = $this->action;
1030
            $this->action                                               = null;
1031
        }
1032
1033 View Code Duplication
        if (!is_null($this->filter)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1034
            $dataToJsonize[$this->nameSpace][$this->evidence.'@filter'] = $this->filter;
1035
        }
1036
1037
        return json_encode($dataToJsonize);
1038
    }
1039
1040
    /**
1041
     * Test if given record ID exists in FlexiBee.
1042
     *
1043
     * @param string|int $identifer
1044
     */
1045
    public function idExists($identifer = null)
1046
    {
1047
        if (is_null($identifer)) {
1048
            $identifer = $this->getMyKey();
1049
        }
1050
        $flexiData = $this->getFlexiData(
1051
            'detail=custom:'.$this->getmyKeyColumn(),
1052
            [$this->getmyKeyColumn() => $identifer]);
1053
1054
        return $flexiData;
1055
    }
1056
1057
    /**
1058
     * Test if given record exists in FlexiBee.
1059
     *
1060
     * @param array $data
1061
     * @return boolean Record presence status
1062
     */
1063
    public function recordExists($data = [])
1064
    {
1065
1066
        if (empty($data)) {
1067
            $data = $this->getData();
1068
        }
1069
1070
        $res = $this->getColumnsFromFlexibee([$this->myKeyColumn],
1071
            self::flexiUrl($data));
0 ignored issues
show
Documentation introduced by
self::flexiUrl($data) is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1072
1073
        if (!count($res) || (isset($res['success']) && ($res['success'] == 'false'))
1074
            || !count($res[0])) {
1075
            $found = false;
1076
        } else {
1077
            $found = true;
1078
        }
1079
        return $found;
1080
    }
1081
1082
    /**
1083
     * Vrací z FlexiBee sloupečky podle podmínek.
1084
     *
1085
     * @param array|int|string $conditions pole podmínek nebo ID záznamu
1086
     * @param string           $indexBy    klice vysledku naplnit hodnotou ze
1087
     *                                     sloupečku
1088
     * @return array
1089
     */
1090
    public function getAllFromFlexibee($conditions = null, $indexBy = null)
1091
    {
1092
        if (is_int($conditions)) {
1093
            $conditions = [$this->getmyKeyColumn() => $conditions];
1094
        }
1095
1096
        $flexiData = $this->getFlexiData('', $conditions);
1097
1098
        if (!is_null($indexBy)) {
1099
            $flexiData = $this->reindexArrayBy($flexiData);
1100
        }
1101
1102
        return $flexiData;
1103
    }
1104
1105
    /**
1106
     * Vrací z FlexiBee sloupečky podle podmínek.
1107
     *
1108
     * @param string[] $columnsList seznam položek
1109
     * @param array    $conditions  pole podmínek nebo ID záznamu
1110
     * @param string   $indexBy     Sloupeček podle kterého indexovat záznamy
1111
     *
1112
     * @return array
1113
     */
1114
    public function getColumnsFromFlexibee($columnsList, $conditions = null,
1115
                                           $indexBy = null)
1116
    {
1117
        $detail = 'full';
1118
        switch (gettype($columnsList)) {
1119
            case 'integer': //Record ID
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
1120
                $conditions = [$this->getmyKeyColumn() => $conditions];
1121
            case 'array': //Few Conditions
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
1122
                if (!is_null($indexBy) && !array_key_exists($indexBy,
1123
                        $columnsList)) {
1124
                    $columnsList[] = $indexBy;
1125
                }
1126
                $columns = implode(',', array_unique($columnsList));
1127
                $detail  = 'custom:'.$columns;
1128
            default:
1129
                switch ($columnsList) {
1130
                    case 'id':
1131
                        $detail = 'id';
1132
                        break;
1133
                    case 'summary':
1134
                        $detail = 'summary';
1135
                        break;
1136
                    default:
1137
                        break;
1138
                }
1139
                break;
1140
        }
1141
1142
        $flexiData = $this->getFlexiData('detail='.$detail, $conditions);
1143
1144
        if (!is_null($indexBy) && count($flexiData) && count(current($flexiData))) {
1145
            $flexiData = $this->reindexArrayBy($flexiData, $indexBy);
1146
        }
1147
1148
        return $flexiData;
1149
    }
1150
1151
    /**
1152
     * Vrací kód záznamu.
1153
     * Obtain record CODE
1154
     *
1155
     * @param mixed $data
1156
     *
1157
     * @return string
1158
     */
1159
    public function getKod($data = null, $unique = true)
1160
    {
1161
        $kod = null;
1162
1163
        if (is_null($data)) {
1164
            $data = $this->getData();
1165
        }
1166
1167
        if (is_string($data)) {
1168
            $data = [$this->nameColumn => $data];
1169
        }
1170
1171
        if (isset($data['kod'])) {
1172
            $kod = $data['kod'];
1173
        } else {
1174
            if (isset($data[$this->nameColumn])) {
1175
                $kod = preg_replace('/[^a-zA-Z0-9]/', '',
1176
                    \Ease\Sand::rip($data[$this->nameColumn]));
1177
            } else {
1178
                if (isset($data[$this->myKeyColumn])) {
1179
                    $kod = \Ease\Sand::rip($data[$this->myKeyColumn]);
1180
                }
1181
            }
1182
        }
1183
1184
        if (!strlen($kod)) {
1185
            $kod = 'NOTSET';
1186
        }
1187
1188
        if (strlen($kod) > 18) {
1189
            $kodfinal = strtoupper(substr($kod, 0, 18));
1190
        } else {
1191
            $kodfinal = strtoupper($kod);
1192
        }
1193
1194
        if ($unique) {
1195
            $counter = 0;
1196
            if (count($this->codes)) {
1197
                foreach ($this->codes as $codesearch => $keystring) {
1198
                    if (strstr($codesearch, $kodfinal)) {
1199
                        ++$counter;
1200
                    }
1201
                }
1202
            }
1203
            if ($counter) {
1204
                $kodfinal = $kodfinal.$counter;
1205
            }
1206
1207
            $this->codes[$kodfinal] = $kod;
1208
        }
1209
1210
        return $kodfinal;
1211
    }
1212
1213
    /**
1214
     * Write Operation Result.
1215
     *
1216
     * @param array  $resultData
1217
     * @param string $url        URL
1218
     * @return boolean Log save success
1219
     */
1220
    public function logResult($resultData = null, $url = null)
1221
    {
1222
        $logResult = false;
1223
        if (isset($resultData['success']) && ($resultData['success'] == 'false')) {
1224
            if (isset($resultData['message'])) {
1225
                $this->addStatusMessage($resultData['message'], 'warning');
1226
            }
1227
            $this->addStatusMessage('Error '.$this->lastResponseCode.': '.urldecode($url),
1228
                'warning');
1229
            unset($url);
1230
        }
1231
        if (is_null($resultData)) {
1232
            $resultData = $this->lastResult;
1233
        }
1234
        if (isset($url)) {
1235
            $this->logger->addStatusMessage(urldecode($url));
1236
        }
1237
1238
        if (isset($resultData['results'])) {
1239
            if ($resultData['success'] == 'false') {
1240
                $status = 'error';
1241
            } else {
1242
                $status = 'success';
1243
            }
1244
            foreach ($resultData['results'] as $result) {
1245
                if (isset($result['request-id'])) {
1246
                    $rid = $result['request-id'];
1247
                } else {
1248
                    $rid = '';
1249
                }
1250
                if (isset($result['errors'])) {
1251
                    foreach ($result['errors'] as $error) {
1252
                        $message = $error['message'];
1253
                        if (isset($error['for'])) {
1254
                            $message .= ' for: '.$error['for'];
1255
                        }
1256
                        if (isset($error['value'])) {
1257
                            $message .= ' value:'.$error['value'];
1258
                        }
1259
                        if (isset($error['code'])) {
1260
                            $message .= ' code:'.$error['code'];
1261
                        }
1262
                        $this->addStatusMessage($rid.': '.$message, $status);
1263
                    }
1264
                }
1265
            }
1266
        }
1267
        return $logResult;
1268
    }
1269
1270
    /**
1271
     * Save RAW Curl Request & Response to files in Temp directory
1272
     */
1273
    public function saveDebugFiles()
1274
    {
1275
        $tmpdir   = sys_get_temp_dir();
1276
        $fname    = $this->evidence.'-'.$this->curlInfo['when'].'.'.$this->format;
1277
        $reqname  = $tmpdir.'/request-'.$fname;
1278
        $respname = $tmpdir.'/response-'.$fname;
1279
        file_put_contents($reqname, $this->postFields);
1280
        file_put_contents($respname, $this->lastCurlResponse);
1281
    }
1282
1283
    /**
1284
     * Připraví data pro odeslání do FlexiBee
1285
     *
1286
     * @param string $data
1287
     */
1288
    public function setPostFields($data)
1289
    {
1290
        $this->postFields = $data;
1291
    }
1292
1293
    /**
1294
     * Generuje fragment url pro filtrování.
1295
     *
1296
     * @see https://www.flexibee.eu/api/dokumentace/ref/filters
1297
     *
1298
     * @param array  $data
1299
     * @param string $joiner default and/or
1300
     * @param string $defop  default operator
1301
     *
1302
     * @return string
1303
     */
1304
    public static function flexiUrl(array $data, $joiner = 'and', $defop = 'eq')
1305
    {
1306
        $parts = [];
1307
1308
        foreach ($data as $column => $value) {
1309
            if (is_integer($data[$column]) || is_float($data[$column])) {
1310
                $parts[$column] = $column.' eq \''.$data[$column].'\'';
1311
            } elseif (is_bool($data[$column])) {
1312
                $parts[$column] = $data[$column] ? $column.' eq true' : $column.' eq false';
1313
            } elseif (is_null($data[$column])) {
1314
                $parts[$column] = $column." is null";
1315
            } else {
1316
                switch ($value) {
1317
                    case '!null':
1318
                        $parts[$column] = $column." is not null";
1319
                        break;
1320
                    case 'is empty':
1321
                    case 'is not empty':
1322
                        $parts[$column] = $column.' '.$value;
1323
                        break;
1324
                    default:
1325
                        if ($column == 'stitky') {
1326
                            $parts[$column] = $column."='code:".$data[$column]."'";
1327
                        } else {
1328
                            $parts[$column] = $column." $defop '".$data[$column]."'";
1329
                        }
1330
                        break;
1331
                }
1332
            }
1333
        }
1334
        return implode(' '.$joiner.' ', $parts);
1335
    }
1336
1337
    /**
1338
     * Obtain record/object identificator code: or id:
1339
     * Vrací identifikátor objektu code: nebo id:
1340
     *
1341
     * @link https://demo.flexibee.eu/devdoc/identifiers Identifikátory záznamů
1342
     * @return string|int indentifikátor záznamu reprezentovaného objektem
1343
     */
1344
    public function getRecordID()
1345
    {
1346
        $myCode = $this->getDataValue('kod');
1347
        if ($myCode) {
1348
            $id = 'code:'.$myCode;
1349
        } else {
1350
            $id = $this->getDataValue('id');
1351
            if (($this->debug === true) && is_null($id)) {
1352
                $this->addToLog('Object Data does not contain code: or id: cannot match with statement!',
1353
                    'warning');
1354
            }
1355
        }
1356
        return is_numeric($id) ? intval($id) : strval($id);
1357
    }
1358
1359
    /**
1360
     * Obtain record/object identificator code: or id:
1361
     * Vrací identifikátor objektu code: nebo id:
1362
     *
1363
     * @link https://demo.flexibee.eu/devdoc/identifiers Identifikátory záznamů
1364
     * @return string indentifikátor záznamu reprezentovaného objektem
1365
     */
1366
    public function __toString()
1367
    {
1368
        return strval($this->getRecordID());
1369
    }
1370
1371
    /**
1372
     * Gives you FlexiPeeHP class name for Given Evidence
1373
     *
1374
     * @param string $evidence
1375
     * @return string Class name
1376
     */
1377
    public static function evidenceToClassName($evidence)
1378
    {
1379
        return str_replace(' ', '', ucwords(str_replace('-', ' ', $evidence)));
1380
    }
1381
1382
1383
    /**
1384
     * Obtain ID of first record in evidence
1385
     *
1386
     * @return string|null id or null if no records
1387
     */
1388
    public function getFirstRecordID()
1389
    {
1390
        $firstID    = null;
1391
        $firstIdRaw = $this->getColumnsFromFlexibee(['id'],
1392
            ['limit' => 1, 'order' => 'id'], 'id');
1393
        if (count($firstIdRaw)) {
1394
            $firstID = (int) current($firstIdRaw)['id'];
1395
        }
1396
        return $firstID;
1397
    }
1398
1399
    /**
1400
     * Vrací hodnotu daného externího ID
1401
     *
1402
     * @param string $want Which ? If empty,you obtain the first one.
1403
     * @return string
1404
     */
1405
    public function getExternalID($want = null)
1406
    {
1407
        $extid = null;
1408
        $ids   = $this->getDataValue('external-ids');
1409
        if (is_null($want)) {
1410
            if (count($ids)) {
1411
                $extid = current($ids);
1412
            }
1413
        } else {
1414
            if (!is_null($ids) && is_array($ids)) {
1415
                foreach ($ids as $id) {
1416
                    if (strstr($id, 'ext:'.$want)) {
1417
                        $extid = str_replace('ext:'.$want.':', '', $id);
1418
                    }
1419
                }
1420
            }
1421
        }
1422
        return $extid;
1423
    }
1424
1425
    /**
1426
     * Obtain actual GlobalVersion
1427
     * Vrací aktuální globální verzi změn
1428
     *
1429
     * @link https://www.flexibee.eu/api/dokumentace/ref/changes-api#globalVersion Globální Verze
1430
     * @return type
1431
     */
1432
    public function getGlobalVersion()
1433
    {
1434
        $globalVersion = null;
1435
        if (!count($this->lastResult) || !isset($this->lastResult['@globalVersion'])) {
1436
            $this->getFlexiData(null,
1437
                ['add-global-version' => 'true', 'limit' => 1]);
1438
        }
1439
1440
        if (isset($this->lastResult['@globalVersion'])) {
1441
            $globalVersion = intval($this->lastResult['@globalVersion']);
1442
        }
1443
1444
        return $globalVersion;
1445
    }
1446
1447
    /**
1448
     * Obtain content type of last response
1449
     *
1450
     * @return string
1451
     */
1452
    public function getResponseFormat()
1453
    {
1454
        if (isset($this->curlInfo['content_type'])) {
1455
            $responseFormat = $this->curlInfo['content_type'];
1456
        } else {
1457
            $responseFormat = null;
1458
        }
1459
        return $responseFormat;
1460
    }
1461
1462
    /**
1463
     * Return the same response format for one and multiplete results
1464
     *
1465
     * @param array $responseBody
1466
     * @return array
1467
     */
1468
    public function unifyResponseFormat($responseBody)
1469
    {
1470
        if (!is_array($responseBody) || array_key_exists('message',
1471
                $responseBody)) { //Unifi response format
1472
            $response = $responseBody;
1473
        } else {
1474
            $evidence = $this->getResponseEvidence();
1475
            if (array_key_exists($evidence, $responseBody)) {
1476
                $response        = [];
1477
                $evidenceContent = $responseBody[$evidence];
1478
                if (array_key_exists(0, $evidenceContent)) {
1479
                    $response[$evidence] = $evidenceContent; //Multiplete Results
1480
                } else {
1481
                    $response[$evidence][0] = $evidenceContent; //One result
1482
                }
1483
            } else {
1484
                if (isset($responseBody['priloha'])) {
1485
                    $response = $responseBody['priloha'];
1486
                } else {
1487
                    $response = $responseBody;
1488
                }
1489
            }
1490
        }
1491
        return $response;
1492
    }
1493
1494
    /**
1495
     * Obtain structure for current (or given) evidence
1496
     *
1497
     * @param string $evidence
1498
     * @return array Evidence structure
1499
     */
1500 View Code Duplication
    public function getColumnsInfo($evidence = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1501
    {
1502
        $columnsInfo = null;
1503
        if (is_null($evidence)) {
1504
            $evidence = $this->getEvidence();
1505
        }
1506
        $propsName = lcfirst(FlexiBeeRO::evidenceToClassName($evidence));
1507
        if (isset(\FlexiPeeHP\Properties::$$propsName)) {
1508
            $columnsInfo = Properties::$$propsName;
1509
        }
1510
        return $columnsInfo;
1511
    }
1512
1513
    /**
1514
     * Obtain actions for current (or given) evidence
1515
     *
1516
     * @param string $evidence
1517
     * @return array Evidence structure
1518
     */
1519 View Code Duplication
    public function getActionsInfo($evidence = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1520
    {
1521
        $actionsInfo = null;
1522
        if (is_null($evidence)) {
1523
            $evidence = $this->getEvidence();
1524
        }
1525
        $propsName = lcfirst(FlexiBeeRO::evidenceToClassName($evidence));
1526
        if (isset(\FlexiPeeHP\Actions::$$propsName)) {
1527
            $actionsInfo = Actions::$$propsName;
1528
        }
1529
        return $actionsInfo;
1530
    }
1531
1532
    /**
1533
     * Obtain relations for current (or given) evidence
1534
     *
1535
     * @param string $evidence
1536
     * @return array Evidence structure
1537
     */
1538
    public function getRelationsInfo($evidence = null)
1539
    {
1540
        $relationsInfo = null;
1541
        if (is_null($evidence)) {
1542
            $evidence = $this->getEvidence();
1543
        }
1544
        $propsName = lcfirst(FlexiBeeRO::evidenceToClassName($evidence));
1545
        if (isset(\FlexiPeeHP\Relations::$$propsName)) {
1546
            $relationsInfo = Relations::$$propsName;
1547
        }
1548
        return $relationsInfo;
1549
    }
1550
1551
    /**
1552
     * Obtain info for current (or given) evidence
1553
     *
1554
     * @param string $evidence
1555
     * @return array Evidence info
1556
     */
1557 View Code Duplication
    public function getEvidenceInfo($evidence = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1558
    {
1559
        $evidencesInfo = null;
1560
        if (is_null($evidence)) {
1561
            $evidence = $this->getEvidence();
1562
        }
1563
        if (isset(EvidenceList::$evidences[$evidence])) {
1564
            $evidencesInfo = EvidenceList::$evidences[$evidence];
1565
        }
1566
        return $evidencesInfo;
1567
    }
1568
1569
    /**
1570
     * Obtain name for current (or given) evidence path
1571
     *
1572
     * @param string $evidence Evidence Path
1573
     * @return array Evidence info
1574
     */
1575 View Code Duplication
    public function getEvidenceName($evidence = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1576
    {
1577
        $evidenceName = null;
1578
        if (is_null($evidence)) {
1579
            $evidence = $this->getEvidence();
1580
        }
1581
        if (isset(EvidenceList::$name[$evidence])) {
1582
            $evidenceName = EvidenceList::$name[$evidence];
1583
        }
1584
        return $evidenceName;
1585
    }
1586
1587
    /**
1588
     * Perform given action (if availble) on current evidence/record
1589
     * @url https://demo.flexibee.eu/devdoc/actions
1590
     *
1591
     * @param string $action one of evidence actions
1592
     * @param string $method ext|int External method call operation in URL.
1593
     *                               Internal add the @action element to request body
1594
     */
1595
    public function performAction($action, $method = 'ext')
1596
    {
1597
        $actionsAvailble = $this->getActionsInfo();
1598
1599
        if (is_array($actionsAvailble) && array_key_exists($action,
1600
                $actionsAvailble)) {
1601
            switch ($actionsAvailble[$action]['actionMakesSense']) {
1602
                case 'ONLY_WITH_INSTANCE_AND_NOT_IN_EDIT':
1603
                case 'ONLY_WITH_INSTANCE': //Add instance
1604
                    $urlSuffix = '/'.$this->__toString().'/'.$action.'.'.$this->format;
1605
                    break;
1606
1607
                default:
1608
                    $urlSuffix = '/'.$action;
1609
                    break;
1610
            }
1611
1612
            switch ($method) {
1613
                case 'int':
1614
                    $this->setAction($action);
1615
                    $this->setPostFields($this->jsonizeData($this->getData()));
1616
                    $result = $this->performRequest(null, 'POST');
1617
                    break;
1618
1619
                default:
1620
                    $result = $this->performRequest($urlSuffix, 'GET');
1621
                    break;
1622
            }
1623
        } else {
1624
            throw new \Exception(sprintf(_('Unsupported action %s for evidence %s'),
1625
                $action, $this->getEvidence()));
1626
        }
1627
1628
        return $result;
1629
    }
1630
1631
    /**
1632
     * Save current object to file
1633
     *
1634
     * @param string $destfile path to file
1635
     */
1636
    public function saveResponseToFile($destfile)
1637
    {
1638
        if (strlen($this->lastCurlResponse)) {
1639
            $this->doCurlRequest($this->apiURL, 'GET', $this->format);
1640
        }
1641
        file_put_contents($destfile, $this->lastCurlResponse);
1642
    }
1643
1644
    /**
1645
     * Obtain established relations listing
1646
     *
1647
     * @return array Null or Relations
1648
     */
1649
    public function getVazby($id = null)
1650
    {
1651
        if (is_null($id)) {
1652
            $id = $this->getRecordID();
1653
        }
1654
        if (!empty($id)) {
1655
            $vazbyRaw = $this->getColumnsFromFlexibee(['vazby'],
1656
                ['relations' => 'vazby', 'id' => $id]);
1657
            $vazby    = $vazbyRaw[0]['vazby'];
1658
        } else {
1659
            throw new \Exception(_('ID requied to get record relations '));
1660
        }
1661
        return $vazby;
1662
    }
1663
1664
    /**
1665
     * Gives You URL for Current Record in FlexiBee web interface
1666
     *
1667
     * @return string url
1668
     */
1669
    public function getFlexiBeeURL()
1670
    {
1671
        $parsed_url = parse_url(str_replace('.'.$this->format, '', $this->apiURL));
1672
        $scheme     = isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://'
1673
                : '';
1674
        $host       = isset($parsed_url['host']) ? $parsed_url['host'] : '';
1675
        $port       = isset($parsed_url['port']) ? ':'.$parsed_url['port'] : '';
1676
        $user       = isset($parsed_url['user']) ? $parsed_url['user'] : '';
1677
        $pass       = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : '';
1678
        $pass       = ($user || $pass) ? "$pass@" : '';
1679
        $path       = isset($parsed_url['path']) ? $parsed_url['path'] : '';
1680
        return $scheme.$user.$pass.$host.$port.$path;
1681
    }
1682
1683
    /**
1684
     * Set Record Key
1685
     *
1686
     * @param int|string $myKeyValue
1687
     * @return boolean
1688
     */
1689
    public function setMyKey($myKeyValue)
1690
    {
1691
        $res = parent::setMyKey($myKeyValue);
1692
        $this->updateApiURL();
1693
        return $res;
1694
    }
1695
1696
    /**
1697
     * Set or get ignore not found pages flag
1698
     *
1699
     * @param boolean $ignore set flag to
1700
     *
1701
     * @return boolean get flag state
1702
     */
1703
    public function ignore404($ignore = null)
1704
    {
1705
        if (!is_null($ignore)) {
1706
            $this->ignoreNotFound = $ignore;
1707
        }
1708
        return $this->ignoreNotFound;
1709
    }
1710
1711
    /**
1712
     * Send Document by mail
1713
     *
1714
     * @url https://www.flexibee.eu/api/dokumentace/ref/odesilani-mailem/
1715
     *
1716
     * @param string $to
1717
     * @param string $subject
1718
     * @param string $body Email Text
1719
     *
1720
     * @return int http response code
1721
     */
1722
    public function sendByMail($to, $subject, $body, $cc = null)
1723
    {
1724
        $this->setPostFields($body);
1725
        $result = $this->doCurlRequest($this->getEvidenceURL().'/'.
1726
            urlencode($this->getRecordID()).'/odeslani-dokladu?to='.$to.'&subject='.urlencode($subject).'&cc='.$cc
1727
            , 'PUT', 'xml');
1728
        return $result == 200;
1729
    }
1730
1731
    /**
1732
     * Send all unsent Invoices by mail
1733
     *
1734
     * @url https://www.flexibee.eu/api/dokumentace/ref/odesilani-mailem/
1735
     * @return int http response code
1736
     */
1737
    public function sendUnsent()
1738
    {
1739
        return $this->doCurlRequest($this->getEvidenceURL().'/automaticky-odeslat-neodeslane',
1740
                'PUT', 'xml');
1741
    }
1742
1743
    /**
1744
     * FlexiBee date to PHP DateTime
1745
     *
1746
     * @param string $flexidate
1747
     *
1748
     * @return \DateTime | false
1749
     */
1750
    public static function flexiDateToDateTime($flexidate)
1751
    {
1752
        return \DateTime::createFromFormat('Y-m-jO', $flexidate);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...('Y-m-jO', $flexidate); of type DateTime|false adds false to the return on line 1752 which is incompatible with the return type documented by FlexiPeeHP\FlexiBeeRO::flexiDateToDateTime of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
1753
    }
1754
1755
    /**
1756
     * Získá dokument v daném formátu
1757
     * Obtain document in given format
1758
     *
1759
     * @param string $format  pdf/csv/xml/json/ ...
1760
     *
1761
     * @return string|null filename downloaded or none
1762
     */
1763
    public function getInFormat($format)
1764
    {
1765
        $response = null;
1766
        if ($this->setFormat($format)) {
1767
            if (($this->doCurlRequest(($format == 'html') ? $this->apiURL.'?inDesktopApp=true'
1768
                            : $this->apiURL, 'GET') == 200)) {
1769
                $response = $this->lastCurlResponse;
1770
            }
1771
        }
1772
        return $response;
1773
    }
1774
1775
    /**
1776
     * Uloží dokument v daném formátu do složky v systému souborů
1777
     * Save document in given format to directory in filesystem
1778
     *
1779
     * @param string $format  pdf/csv/xml/json/ ...
1780
     * @param string $destDir where to put file (prefix)
1781
     *
1782
     * @return string|null filename downloaded or none
1783
     */
1784
    public function downloadInFormat($format, $destDir = './')
1785
    {
1786
        $fileOnDisk = null;
1787
        if ($this->setFormat($format)) {
1788
            $downloadTo = $destDir.$this->getEvidence().'_'.$this->getMyKey().'.'.$format;
1789
            if (($this->doCurlRequest($this->apiURL, 'GET') == 200) && (file_put_contents($downloadTo,
1790
                    $this->lastCurlResponse) !== false)) {
1791
                $fileOnDisk = $downloadTo;
1792
            }
1793
        }
1794
        return $fileOnDisk;
1795
    }
1796
1797
    /**
1798
     * Compile and send Report about Error500 to FlexiBee developers
1799
     * If FlexiBee is running on localost try also include java backtrace
1800
     *
1801
     * @param array $errorResponse result of parseError();
1802
     */
1803
    public function error500Reporter($errorResponse)
1804
    {
1805
        $ur = str_replace('/c/'.$this->company, '',
1806
            str_replace($this->url, '', $this->curlInfo['url']));
1807
        if (!array_key_exists($ur, $this->reports)) {
1808
            $tmpdir   = sys_get_temp_dir();
1809
            $myTime   = $this->curlInfo['when'];
1810
            $curlname = $tmpdir.'/curl-'.$this->evidence.'-'.$myTime.'.json';
1811
            file_put_contents($curlname,
1812
                json_encode($this->curlInfo, JSON_PRETTY_PRINT));
1813
1814
            $report = new \Ease\Mailer($this->reportRecipient,
1815
                'Error report 500 - '.$ur);
1816
1817
            $d     = dir($tmpdir);
1818
            while (false !== ($entry = $d->read())) {
1819
                if (strstr($entry, $myTime)) {
1820
                    $ext  = pathinfo($tmpdir.'/'.$entry, PATHINFO_EXTENSION);
1821
                    $mime = Formats::suffixToContentType($ext);
1822
                    $report->addFile($tmpdir.'/'.$entry,
1823
                        empty($mime) ? 'text/plain' : $mime);
1824
                }
1825
            }
1826
            $d->close();
1827
1828
            if ((strstr($this->url, '://localhost') || strstr($this->url,
1829
                    '://127.')) && file_exists('/var/log/flexibee.log')) {
1830
1831
                $fl = fopen("/var/log/flexibee.log", "r");
1832
                if ($fl) {
1833
                    $tracelog = [];
1834
                    for ($x_pos = 0, $ln = 0, $output = array(); fseek($fl,
1835
                            $x_pos, SEEK_END) !== -1; $x_pos--) {
1836
                        $char = fgetc($fl);
1837
                        if ($char === "\n") {
1838
                            $tracelog[] = $output[$ln];
1839
                            if (strstr($output[$ln], $errorResponse['message'])) {
1840
                                break;
1841
                            }
1842
                            $ln++;
1843
                            continue;
1844
                        }
1845
                        $output[$ln] = $char.((array_key_exists($ln, $output)) ? $output[$ln]
1846
                                : '');
1847
                    }
1848
1849
                    $trace     = implode("\n", array_reverse($tracelog));
1850
                    $tracefile = $tmpdir.'/trace-'.$this->evidence.'-'.$myTime.'.log';
1851
                    file_put_contents($tracefile, $trace);
1852
                    $report->addItem("\n\n".$trace);
1853
                    fclose($fl);
1854
                }
1855
            } else {
1856
                $report->addItem($errorResponse['message']);
1857
            }
1858
1859
            $licenseInfo = $this->performRequest($this->url.'/default-license.json');
1860
1861
            $report->addItem("\n\n".json_encode($licenseInfo['license'],
1862
                    JSON_PRETTY_PRINT));
1863
1864
            if ($report->send()) {
1865
                $this->reports[$ur] = $myTime;
1866
            }
1867
        }
1868
    }
1869
}
1870