Test Failed
Push — master ( fd39e5...584f1e )
by Vítězslav
02:42
created

FlexiBeeRO::setUp()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.1158

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 16
nop 1
dl 0
loc 22
ccs 10
cts 12
cp 0.8333
crap 5.1158
rs 8.6737
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
     * Where to get JSON files with evidence stricture etc.
20
     * @var string
21
     */
22
    public static $infoDir = __DIR__.'/../../static';
23
24
    /**
25
     * Version of FlexiPeeHP library
26
     *
27
     * @var string
28
     */
29
    public static $libVersion = '1.4.2.3';
30
31
    /**
32
     * Základní namespace pro komunikaci s FlexiBee.
33
     * Basic namespace for communication with FlexiBee
34
     *
35
     * @var string Jmený prostor datového bloku odpovědi
36
     */
37
    public $nameSpace = 'winstrom';
38
39
    /**
40
     * URL of object data in FlexiBee
41
     * @var string url
42
     */
43
    public $apiURL = null;
44
45
    /**
46
     * Datový blok v poli odpovědi.
47
     * Data block in response field.
48
     *
49
     * @var string
50
     */
51
    public $resultField = 'results';
52
53
    /**
54
     * Verze protokolu použitého pro komunikaci.
55
     * Communication protocol version used.
56
     *
57
     * @var string Verze použitého API
58
     */
59
    public $protoVersion = '1.0';
60
61
    /**
62
     * Evidence užitá objektem.
63
     * Evidence used by object
64
     *
65
     * @link https://demo.flexibee.eu/c/demo/evidence-list Přehled evidencí
66
     * @var string
67
     */
68
    public $evidence = null;
69
70
    /**
71
     * Výchozí formát pro komunikaci.
72
     * Default communication format.
73
     *
74
     * @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů
75
     *
76
     * @var string json|xml|...
77
     */
78
    public $format = 'json';
79
80
    /**
81
     * formát příchozí odpovědi
82
     * response format
83
     *
84
     * @link https://www.flexibee.eu/api/dokumentace/ref/format-types Přehled možných formátů
85
     *
86
     * @var string json|xml|...
87
     */
88
    public $responseFormat = 'json';
89
90
    /**
91
     * Curl Handle.
92
     *
93
     * @var resource
94
     */
95
    public $curl = null;
96
97
    /**
98
     * @link https://demo.flexibee.eu/devdoc/company-identifier Identifikátor firmy
99
     * @var string
100
     */
101
    public $company = null;
102
103
    /**
104
     * Server[:port]
105
     * @var string
106
     */
107
    public $url = null;
108
109
    /**
110
     * REST API Username
111
     * @var string
112
     */
113
    public $user = null;
114
115
    /**
116
     * REST API Password
117
     * @var string
118
     */
119
    public $password = null;
120
121
    /**
122
     * @var array Pole HTTP hlaviček odesílaných s každým požadavkem
123
     */
124
    public $defaultHttpHeaders = ['User-Agent' => 'FlexiPeeHP'];
125
126
    /**
127
     * Default additional request url parameters after question mark
128
     *
129
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls   Common params
130
     * @link https://www.flexibee.eu/api/dokumentace/ref/paging Paging params
131
     * @var array
132
     */
133
    public $defaultUrlParams = ['limit' => 0];
134
135
    /**
136
     * Identifikační řetězec.
137
     *
138
     * @var string
139
     */
140
    public $init = null;
141
142
    /**
143
     * Sloupeček s názvem.
144
     *
145
     * @var string
146
     */
147
    public $nameColumn = 'nazev';
148
149
    /**
150
     * Sloupeček obsahující datum vložení záznamu do shopu.
151
     *
152
     * @var string
153
     */
154
    public $myCreateColumn = 'false';
155
156
    /**
157
     * Slopecek obsahujici datum poslení modifikace záznamu do shopu.
158
     *
159
     * @var string
160
     */
161
    public $myLastModifiedColumn = 'lastUpdate';
162
163
    /**
164
     * Klíčový idendifikátor záznamu.
165
     *
166
     * @var string
167
     */
168
    public $fbKeyColumn = 'id';
169
170
    /**
171
     * Informace o posledním HTTP requestu.
172
     *
173
     * @var *
174
     */
175
    public $curlInfo;
176
177
    /**
178
     * Informace o poslední HTTP chybě.
179
     *
180
     * @var string
181
     */
182
    public $lastCurlError = null;
183
184
    /**
185
     * Used codes storage.
186
     *
187
     * @var array
188
     */
189
    public $codes = null;
190
191
    /**
192
     * Last Inserted ID.
193
     *
194
     * @var int
195
     */
196
    public $lastInsertedID = null;
197
198
    /**
199
     * Default Line Prefix.
200
     *
201
     * @var string
202
     */
203
    public $prefix = '/c/';
204
205
    /**
206
     * Raw Content of last curl response
207
     *
208
     * @var string
209
     */
210
    public $lastCurlResponse;
211
212
    /**
213
     * HTTP Response code of last request
214
     *
215
     * @var int
216
     */
217
    public $lastResponseCode = null;
218
219
    /**
220
     * Body data  for next curl POST operation
221
     *
222
     * @var string
223
     */
224
    protected $postFields = null;
225
226
    /**
227
     * Last operation result data or message(s)
228
     *
229
     * @var array
230
     */
231
    public $lastResult = null;
232
233
    /**
234
     * Number from  @rowCount in response
235
     * @var int
236
     */
237
    public $rowCount = null;
238
239
    /**
240
     * Number from  @globalVersion
241
     * @var int
242
     */
243
    public $globalVersion = null;
244
245
    /**
246
     * @link https://www.flexibee.eu/api/dokumentace/ref/zamykani-odemykani/
247
     * @var string filter query
248
     */
249
    public $filter;
250
251
    /**
252
     * @link https://demo.flexibee.eu/devdoc/actions Provádění akcí
253
     * @var string
254
     */
255
    protected $action;
256
257
    /**
258
     * Pole akcí které podporuje ta která evidence
259
     * @link https://demo.flexibee.eu/c/demo/faktura-vydana/actions.json Např. Akce faktury
260
     * @var array
261
     */
262
    public $actionsAvailable = null;
263
264
    /**
265
     * Parmetry pro URL
266
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Všechny podporované parametry
267
     * @var array
268
     */
269
    public $urlParams = [
270
        'idUcetniObdobi',
271
        'dry-run',
272
        'fail-on-warning',
273
        'report-name',
274
        'report-lang',
275
        'report-sign',
276
        'detail', //See: https://www.flexibee.eu/api/dokumentace/ref/detail-levels
277
        'mode',
278
        'limit',
279
        'start',
280
        'order',
281
        'sort',
282
        'add-row-count',
283
        'relations',
284
        'includes',
285
        'use-ext-id',
286
        'use-internal-id',
287
        'stitky-as-ids',
288
        'only-ext-ids',
289
        'no-ext-ids',
290
        'no-ids',
291
        'code-as-id',
292
        'no-http-errors',
293
        'export-settings',
294
        'as-gui',
295
        'code-in-response',
296
        'add-global-version',
297
        'encoding',
298
        'delimeter',
299
        'format',
300
        'auth',
301
        'skupina-stitku',
302
        'dir',
303
        'relations',
304
        'relations',
305
        'xpath', // See: https://www.flexibee.eu/api/dokumentace/ref/xpath/
306
        'dry-run', // See: https://www.flexibee.eu/api/dokumentace/ref/dry-run/
307
        'inDesktopApp' // Note: Undocumented function (html only)
308
    ];
309
310
    /**
311
     * Save 404 results to log ?
312
     * @var boolean
313
     */
314
    protected $ignoreNotFound = false;
315
316
    /**
317
     * Array of errors caused by last request
318
     * @var array
319
     */
320
    private $errors = [];
321
322
    /**
323
     * List of Error500 reports sent
324
     * @var array
325
     */
326
    private $reports = [];
327
328
    /**
329
     * Send Error500 Report to
330
     * @var string email address
331
     */
332
    public $reportRecipient = '[email protected]';
333
334
    /**
335
     * Formating string for \DateTime::format() for datetime columns
336
     * @var string 
337
     */
338
    static public $DateTimeFormat = 'Y-m-d\TH:i:s.u+P';
339
340 70
    /**
341
     * Formating string for \DateTime::format() for date columns
342 70
     * @var string 
343
     */
344 70
    static public $DateFormat = 'Y-m-d';
345 70
346 70
    /**
347 70
     * Chained Objects
348 22
     * @var array
349 22
     */
350 70
    public $chained = [];
351
352
    /**
353
     * We Connect to server by default
354
     * @var boolean
355
     */
356
    public $offline = false;
357
358 71
    /**
359
     * Class for read only interaction with FlexiBee.
360 71
     *
361 71
     * @param mixed $init default record id or initial data
362 71
     * @param array $options Connection settings override
363 71
     */
364 71
    public function __construct($init = null, $options = [])
365 23
    {
366 23
        $this->init = $init;
367 71
368 71
        parent::__construct();
369 23
        $this->setUp($options);
370 23
        $this->curlInit();
371 71
        if (!empty($init)) {
372
            $this->processInit($init);
373
        }
374 71
    }
375 71
376 71
    /**
377
     * SetUp Object to be ready for connect
378
     *
379
     * @param array $options Object Options (company,url,user,password,evidence,
380
     *                                       prefix,defaultUrlParams,debug)
381
     */
382
    public function setUp($options = [])
383
    {
384
        $this->setupProperty($options, 'company', 'FLEXIBEE_COMPANY');
385 48
        $this->setupProperty($options, 'url', 'FLEXIBEE_URL');
386
        $this->setupProperty($options, 'user', 'FLEXIBEE_LOGIN');
387 48
        $this->setupProperty($options, 'password', 'FLEXIBEE_PASSWORD');
388
        if (isset($options['evidence'])) {
389
            $this->setEvidence($options['evidence']);
390 48
        }
391 48
        $this->setupProperty($options, 'defaultUrlParams');
392 48
        if (isset($options['prefix'])) {
393
            $this->setPrefix($options['prefix']);
394 48
        }
395
        if (array_key_exists('detail', $options)) {
396
            $this->defaultUrlParams['detail'] = $options['detail'];
397
        }
398
        if (array_key_exists('offline', $options)) {
399 94
            $this->offline = (boolean) $options['offline'];
400
        }
401 94
        $this->setupProperty($options, 'debug');
402 94
        $this->updateApiURL();
403 94
    }
404 94
405 94
    /**
406 94
     * Set up one of properties
407 94
     *
408 94
     * @param array  $options  array of given properties
409 94
     * @param string $name     name of property to process
410 94
     * @param string $constant load default property value from constant
411
     */
412
    public function setupProperty($options, $name, $constant = null)
413
    {
414
        if (isset($options[$name])) {
415
            $this->$name = $options[$name];
416
        } else {
417
            if (property_exists($this, $name) && !empty($constant) && defined($constant)) {
418
                $this->$name = constant($constant);
419
            }
420
        }
421
    }
422
423 13
    /**
424
     * Inicializace CURL
425 13
     * 
426 10
     * @return boolean Online Status
427 13
     */
428 13
    public function curlInit()
429 13
    {
430 10
        if ($this->offline === false) {
431 10
            $this->curl = \curl_init(); // create curl resource
432 10
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); // return content as a string from curl_exec
433 8
            curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, true); // follow redirects (compatibility for future changes in FlexiBee)
434
            curl_setopt($this->curl, CURLOPT_HTTPAUTH, true);       // HTTP authentication
435 13
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); // FlexiBee by default uses Self-Signed certificates
436
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false);
437
            curl_setopt($this->curl, CURLOPT_VERBOSE, ($this->debug === true)); // For debugging
438
            curl_setopt($this->curl, CURLOPT_USERPWD,
439
                $this->user.':'.$this->password); // set username and password
440
        }
441
        return !$this->offline;
442 23
    }
443
444
    /**
445 23
     * Zinicializuje objekt dle daných dat. Možné hodnoty:
446 23
     *
447 23
     *  * 234                              - interní číslo záznamu k načtení
448 23
     *  * code:LOPATA                      - kód záznamu
449 23
     *  * BAGR                             - kód záznamu k načtení
450 23
     *  * ['id'=>24,'nazev'=>'hoblík']     - pole hodnot k předvyplnění
451 23
     *  * 743.json?relations=adresa,vazby  - část url s parametry k načtení
452 23
     *
453 23
     * @param mixed $init číslo/"(code:)kód"/(část)URI záznamu k načtení | pole hodnot k předvyplnění
454 23
     */
455 23
    public function processInit($init)
456 23
    {
457 23
        if (is_integer($init)) {
458 23
            $this->loadFromFlexiBee($init);
459 23
        } elseif (is_array($init)) {
460 23
            $this->takeData($init);
461 23
        } elseif (preg_match('/\.(json|xml|csv)/', $init)) {
462 23
            $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...
463
                            : $init)));
464
        } else {
465
            $this->loadFromFlexiBee($init);
466
        }
467
    }
468
469
    /**
470
     * Set Data Field value
471 23
     *
472
     * @param string $columnName field name
473 23
     * @param mixed  $value      field data value
474 23
     *
475
     * @return bool Success
476
     */
477
    public function setDataValue($columnName, $value)
478
    {
479
        if (is_object($value)) {
480 23
            switch (get_class($value)) {
481 23
                case 'DateTime':
482 23
                    $columnInfo = $this->getColumnInfo($columnName);
483 23
                    switch ($columnInfo['type']) {
484 23
                        case 'date':
485
                            $value = $value->format(self::$DateFormat);
486
                            break;
487
                        case 'datetime':
488
                            $value = $value->format(self::$DateTimeFormat);
489
                            break;
490
                    }
491
                    break;
492
            }
493
        }
494 23
        return parent::setDataValue($columnName, $value);
495
    }
496 23
497 23
    /**
498 23
     * Set URL prefix
499 23
     *
500
     * @param string $prefix
501
     */
502
    public function setPrefix($prefix)
503 23
    {
504 23
        switch ($prefix) {
505
            case 'a': //Access
506
            case 'c': //Company
507 20
            case 'u': //User
508 20
            case 'g': //License Groups
509
            case 'admin':
510 20
            case 'status':
511 3
            case 'login-logout':
512 3
                $this->prefix = '/'.$prefix.'/';
513 3
                break;
514 3
            case null:
515 23
            case '':
516 23
            case '/':
517 23
                $this->prefix = '';
518
                break;
519
            default:
520
                throw new \Exception(sprintf('Unknown prefix %s', $prefix));
521
        }
522
    }
523
524
    /**
525
     * Set communication format.
526 69
     * One of html|xml|json|csv|dbf|xls|isdoc|isdocx|edi|pdf|pdf|vcf|ical
527
     *
528 69
     * @param string $format
529
     * @return boolen format is availble
530
     */
531
    public function setFormat($format)
532
    {
533
        $result = true;
534
        if (($this->debug === true) && !empty($this->evidence) && isset(Formats::$$this->evidence)) {
535
            if (array_key_exists($format, array_flip(Formats::$$this->evidence))
536
                === false) {
537 23
                $result = false;
538
            }
539 23
        }
540 23
        if ($result === true) {
541
            $this->format = $format;
542
            $this->updateApiURL();
543
        }
544
        return $result;
545
    }
546
547
    /**
548 23
     * Nastaví Evidenci pro Komunikaci.
549
     * Set evidence for communication
550 23
     *
551
     * @param string $evidence evidence pathName to use
552
     * @return boolean evidence switching status
553
     */
554
    public function setEvidence($evidence)
555
    {
556
        switch ($this->prefix) {
557
            case '/c/':
558 25
                if ($this->debug === true) {
559
                    if (array_key_exists($evidence, EvidenceList::$name)) {
560 25
                        $this->evidence = $evidence;
561 25
                        $result         = true;
562
                    } else {
563
                        throw new \Exception(sprintf('Try to set unsupported evidence %s',
564 25
                                $evidence));
565 1
                    }
566 1
                } else {
567 24
                    $this->evidence = $evidence;
568 24
                    $result         = true;
569 24
                }
570 25
                break;
571 25
            default:
572
                $this->evidence = $evidence;
573
                $result         = true;
574
                break;
575
        }
576
        $this->updateApiURL();
577
        return $result;
578
    }
579
580
    /**
581 23
     * Vrací právě používanou evidenci pro komunikaci
582
     * Obtain current used evidence
583 23
     *
584 23
     * @return string
585 23
     */
586 23
    public function getEvidence()
587 23
    {
588 23
        return $this->evidence;
589 23
    }
590 23
591 23
    /**
592 23
     * Set used company.
593 23
     * Nastaví Firmu.
594 23
     *
595 23
     * @param string $company
596
     */
597
    public function setCompany($company)
598
    {
599 23
        $this->company = $company;
600
    }
601
602
    /**
603
     * Obtain company now used
604
     * Vrací právě používanou firmu
605
     *
606
     * @return string
607
     */
608
    public function getCompany()
609 23
    {
610
        return $this->company;
611 23
    }
612 23
613 22
    /**
614 22
     * Vrací název evidence použité v odpovědích z FlexiBee
615 23
     *
616 17
     * @return string
617 17
     */
618 17
    public function getResponseEvidence()
619 17
    {
620 23
        switch ($this->evidence) {
621
            case 'c':
622
                $evidence = 'company';
623
                break;
624 23
            case 'evidence-list':
625
                $evidence = 'evidence';
626
                break;
627
            default:
628
                $evidence = $this->getEvidence();
629
                break;
630
        }
631
        return $evidence;
632
    }
633
634 68
    /**
635
     * Převede rekurzivně Objekt na pole.
636 68
     *
637 68
     * @param object|array $object
638 68
     *
639 62
     * @return array
640 62
     */
641 68
    public static function object2array($object)
642
    {
643
        $result = null;
644
        if (is_object($object)) {
645
            $objectData = get_object_vars($object);
646
            if (is_array($objectData) && count($objectData)) {
647
                $result = array_map('self::object2array', $objectData);
648
            }
649
        } else {
650
            if (is_array($object)) {
651 23
                foreach ($object as $item => $value) {
652
                    $result[$item] = self::object2array($value);
653 23
                }
654 23
            } else {
655 23
                $result = $object;
656 23
            }
657 23
        }
658 23
659 23
        return $result;
660 23
    }
661 23
662
    /**
663
     * Převede rekurzivně v poli všechny objekty na jejich identifikátory.
664
     *
665
     * @param object|array $object
666
     *
667 48
     * @return array
668
     */
669 48
    public static function objectToID($object)
670 48
    {
671 48
        $resultID = null;
672
        if (is_object($object)) {
673
            $resultID = $object->__toString();
674 48
        } else {
675 48
            if (is_array($object)) {
676
                foreach ($object as $item => $value) {
677
                    $resultID[$item] = self::objectToID($value);
678
                }
679
            } else { //String
680
                $resultID = $object;
681
            }
682
        }
683
684
        return $resultID;
685
    }
686 23
687
    /**
688 23
     * Return basic URL for used Evidence
689 23
     *
690 23
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
691 23
     *
692 23
     * @return string Evidence URL
693 23
     */
694 23
    public function getEvidenceURL()
695 23
    {
696 23
        $evidenceUrl = $this->url.$this->prefix.$this->company;
697 23
        $evidence    = $this->getEvidence();
698 23
        if (!empty($evidence)) {
699 23
            $evidenceUrl .= '/'.$evidence;
700 23
        }
701
        return $evidenceUrl;
702
    }
703 23
704 23
    /**
705 23
     * Add suffix to Evidence URL
706
     *
707
     * @param string $urlSuffix
708 23
     *
709
     * @return string
710
     */
711
    public function evidenceUrlWithSuffix($urlSuffix)
712
    {
713
        $evidenceUrl = $this->getEvidenceUrl();
714
        if (!empty($urlSuffix)) {
715
            if (($urlSuffix[0] != '/') && ($urlSuffix[0] != ';') && ($urlSuffix[0]
716
                != '?')) {
717
                $evidenceUrl .= '/';
718 23
            }
719
            $evidenceUrl .= $urlSuffix;
720 23
        }
721
        return $evidenceUrl;
722
    }
723
724
    /**
725
     * Update $this->apiURL
726
     */
727
    public function updateApiURL()
728
    {
729
        $this->apiURL = $this->getEvidenceURL();
730
        $id           = $this->__toString();
731 25
        if (!empty($id)) {
732
            $this->apiURL .= '/'.urlencode($id);
733
        }
734 25
        $this->apiURL .= '.'.$this->format;
735
    }
736 25
737
    /**
738 25
     * Add params to url
739 4
     *
740 4
     * @param string  $url      originall url
741 22
     * @param array   $params   value to add
742
     * @param boolean $override replace already existing values ?
743
     *
744 25
     * @return string url with parameters added
745
     */
746 25
    public function addUrlParams($url, $params, $override = false)
747 25
    {
748
        $urlParts = parse_url($url);
749
        $urlFinal = '';
750
        if (array_key_exists('scheme', $urlParts)) {
751
            $urlFinal .= $urlParts['scheme'].'://'.$urlParts['host'];
752
        }
753
        if (array_key_exists('port', $urlParts)) {
754
            $urlFinal .= ':'.$urlParts['port'];
755
        }
756
        if (array_key_exists('path', $urlParts)) {
757
            $urlFinal .= $urlParts['path'];
758 3
        }
759
        if (array_key_exists('query', $urlParts)) {
760 3
            parse_str($urlParts['query'], $queryUrlParams);
761
            $urlParams = $override ? array_merge($params, $queryUrlParams) : array_merge($queryUrlParams,
762 3
                    $params);
763 3
        } else {
764 3
            $urlParams = $params;
765
        }
766
767
        if (!empty($urlParams)) {
768
            $urlFinal .= '?';
769
            if (is_array($urlParams)) {
770
                $urlFinal .= http_build_query($urlParams);
771
            } else {
772
                $urlFinal .= $urlParams;
773 3
            }
774
        }
775 3
        return $urlFinal;
776
    }
777
778
    /**
779
     * Add Default Url params to given url if not overrided
780
     *
781
     * @param string $urlRaw
782
     *
783
     * @return string url with default params added
784
     */
785 3
    public function addDefaultUrlParams($urlRaw)
786
    {
787 3
        return $this->addUrlParams($urlRaw, $this->defaultUrlParams, false);
788 3
    }
789 3
790 3
    /**
791
     * Funkce, která provede I/O operaci a vyhodnotí výsledek.
792
     *
793 3
     * @param string $urlSuffix část URL za identifikátorem firmy.
794
     * @param string $method    HTTP/REST metoda
795
     * @param string $format    Requested format
796
     * @return array|boolean Výsledek operace
797 3
     */
798
    public function performRequest($urlSuffix = null, $method = 'GET',
799
                                   $format = null)
800
    {
801
        $this->rowCount = null;
802
803
        if (preg_match('/^http/', $urlSuffix)) {
804
            $url = $urlSuffix;
805
        } elseif (strlen($urlSuffix) && ($urlSuffix[0] == '/')) {
806
            $url = $this->url.$urlSuffix;
807
        } else {
808
            $url = $this->evidenceUrlWithSuffix($urlSuffix);
809
        }
810
811
        $responseCode = $this->doCurlRequest($url, $method, $format);
812
813
        return $this->parseResponse($this->rawResponseToArray($this->lastCurlResponse,
0 ignored issues
show
Bug introduced by
It seems like $this->rawResponseToArra... $this->responseFormat) targeting FlexiPeeHP\FlexiBeeRO::rawResponseToArray() can also be of type string; however, FlexiPeeHP\FlexiBeeRO::parseResponse() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
814
                    $this->responseFormat), $responseCode);
815
    }
816
817
    /**
818
     * Parse Raw FlexiBee response in several formats
819
     *
820 3
     * @param string $responseRaw raw response body
821
     * @param string $format      Raw Response format json|xml|etc
822 3
     *
823
     * @return array
824 3
     */
825
    public function rawResponseToArray($responseRaw, $format)
826
    {
827
        $responseDecoded = [];
828
        if (!empty(trim($responseRaw))) {
829
            switch ($format) {
830
                case 'json':
831
                    $responseDecoded = $this->rawJsonToArray($responseRaw);
832 3
                    break;
833 3
                case 'xml':
834 3
                    $responseDecoded = $this->rawXmlToArray($this->lastCurlResponse);
835
                    break;
836
                case 'txt':
837 3
                default:
838
                    $responseDecoded = $this->lastCurlResponse;
839
                    break;
840 3
            }
841
        }
842
        return $responseDecoded;
843
    }
844
845
    /**
846
     * Convert FlexiBee Response JSON to Array
847
     *
848
     * @param string $rawJson
849
     * 
850
     * @return array
851
     */
852
    public function rawJsonToArray($rawJson)
853
    {
854
        $responseDecoded = json_decode($rawJson, true, 10);
855
        $decodeError     = json_last_error_msg();
856
        if ($decodeError == 'No error') {
857
            if (array_key_exists($this->nameSpace, $responseDecoded)) {
858
                $responseDecoded = $responseDecoded[$this->nameSpace];
859
            }
860 3
        } else {
861
            $this->addStatusMessage('JSON Decoder: '.$decodeError, 'error');
862
            $this->addStatusMessage($rawJson, 'debug');
863
        }
864
        return $responseDecoded;
865
    }
866
867
    /**
868
     * Convert FlexiBee Response XML to Array
869
     *
870
     * @param string $rawXML
871
     *
872
     * @return array
873
     */
874
    public function rawXmlToArray($rawXML)
875
    {
876
        return self::xml2array($rawXML);
877
    }
878
879
    /**
880
     * Parse Response array
881
     *
882
     * @param array $responseDecoded
883
     * @param int $responseCode Request Response Code
884
     *
885
     * @return array main data part of response
886
     */
887
    public function parseResponse($responseDecoded, $responseCode)
888
    {
889
        $response = null;
890 3
        switch ($responseCode) {
891
            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...
892 3
                if (isset($responseDecoded[$this->resultField][0]['id'])) {
893 3
                    $this->lastInsertedID = $responseDecoded[$this->resultField][0]['id'];
894 3
                    $this->setMyKey($this->lastInsertedID);
895 3
                    $this->apiURL         = $this->getEvidenceURL().'/'.$this->lastInsertedID;
896
                } else {
897 3
                    $this->lastInsertedID = null;
898
                }
899 3
            case 200: //Success Read
900
                $response         = $this->lastResult = $this->unifyResponseFormat($responseDecoded);
901 3
                if (isset($responseDecoded['@rowCount'])) {
902
                    $this->rowCount = (int) $responseDecoded['@rowCount'];
903 3
                }
904
                if (isset($responseDecoded['@globalVersion'])) {
905 3
                    $this->globalVersion = (int) $responseDecoded['@globalVersion'];
906 3
                }
907 3
                break;
908 3
909 3
            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...
910 3
                if ($this->debug === true) {
911 3
                    $this->error500Reporter($responseDecoded);
912 3
                }
913 3
            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...
914 3
                if ($this->ignoreNotFound === true) {
915 3
                    break;
916 3
                }
917 3
            case 400: //Bad Request parameters
918
            default: //Something goes wrong
919 3
                $this->addStatusMessage($this->lastResponseCode.': '.$this->curlInfo['url'],
920
                    'warning');
921
                if (is_array($responseDecoded)) {
922 3
                    $this->parseError($responseDecoded);
923 3
                }
924 3
                $this->logResult($responseDecoded, $this->curlInfo['url']);
925 3
                break;
926 3
        }
927 3
        return $response;
928 3
    }
929 3
930 3
    /**
931
     * Parse error message response
932
     *
933
     * @param array $responseDecoded
934
     * @return int number of errors processed
935 3
     */
936
    public function parseError(array $responseDecoded)
937
    {
938
        if (array_key_exists('results', $responseDecoded)) {
939 3
            $this->errors = $responseDecoded['results'][0]['errors'];
940
        } else {
941
            if (array_key_exists('message', $responseDecoded)) {
942
                $this->errors = [['message' => $responseDecoded['message']]];
943
            }
944
        }
945
        return count($this->errors);
946
    }
947
948
    /**
949 23
     * Vykonej HTTP požadavek
950
     *
951 23
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
952 23
     * @param string $url    URL požadavku
953 23
     * @param string $method HTTP Method GET|POST|PUT|OPTIONS|DELETE
954 23
     * @param string $format požadovaný formát komunikace
955 15
     * @return int HTTP Response CODE
956 15
     */
957 15
    public function doCurlRequest($url, $method, $format = null)
958 23
    {
959
        if (is_null($format)) {
960
            $format = $this->format;
961
        }
962
        curl_setopt($this->curl, CURLOPT_URL, $url);
963
// Nastavení samotné operace
964
        curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, strtoupper($method));
965
//Vždy nastavíme byť i prázná postdata jako ochranu před chybou 411
966
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postFields);
967
968 23
        $httpHeaders = $this->defaultHttpHeaders;
969
970 23
        $formats = Formats::bySuffix();
971 23
972 23
        if (!isset($httpHeaders['Accept'])) {
973 23
            $httpHeaders['Accept'] = $formats[$format]['content-type'];
974 23
        }
975
        if (!isset($httpHeaders['Content-Type'])) {
976 23
            $httpHeaders['Content-Type'] = $formats[$format]['content-type'];
977 23
        }
978 23
        $httpHeadersFinal = [];
979 23
        foreach ($httpHeaders as $key => $value) {
980 23
            if (($key == 'User-Agent') && ($value == 'FlexiPeeHP')) {
981
                $value .= ' v'.self::$libVersion;
982 23
            }
983 23
            $httpHeadersFinal[] = $key.': '.$value;
984 23
        }
985
986
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $httpHeadersFinal);
987
988
// Proveď samotnou operaci
989
        $this->lastCurlResponse            = curl_exec($this->curl);
990 1
        $this->curlInfo                    = curl_getinfo($this->curl);
991
        $this->curlInfo['when']            = microtime();
992 1
        $this->curlInfo['request_headers'] = $httpHeadersFinal;
993 1
        $this->responseFormat              = isset($this->curlInfo['content_type'])
994 1
                ? Formats::contentTypeToSuffix($this->curlInfo['content_type']) : 'txt';
995 1
        $this->lastResponseCode            = $this->curlInfo['http_code'];
996 1
        $this->lastCurlError               = curl_error($this->curl);
997
        if (strlen($this->lastCurlError)) {
998
            $this->addStatusMessage(sprintf('Curl Error (HTTP %d): %s',
999
                    $this->lastResponseCode, $this->lastCurlError), 'error');
1000
        }
1001 1
1002
        if ($this->debug === true) {
1003 1
            $this->saveDebugFiles();
1004 1
        }
1005
1006
        return $this->lastResponseCode;
1007
    }
1008
1009
    /**
1010
     * Nastaví druh prováděné akce.
1011
     *
1012
     * @link https://demo.flexibee.eu/devdoc/actions Provádění akcí
1013 23
     * @param string $action
1014
     * @return boolean
1015 23
     */
1016 23
    public function setAction($action)
1017 22
    {
1018
        $result           = false;
1019
        $actionsAvailable = $this->getActionsInfo();
1020
        if (is_array($actionsAvailable) && array_key_exists($action,
1021 22
                $actionsAvailable)) {
1022
            $this->action = $action;
1023
            $result       = true;
1024
        }
1025
        return $result;
1026
    }
1027
1028
    /**
1029
     * Convert XML to array.
1030
     *
1031
     * @param string $xml
1032
     *
1033
     * @return array
1034
     */
1035
    public static function xml2array($xml)
1036
    {
1037
        $arr = [];
1038
        if (!empty($xml)) {
1039
            if (is_string($xml)) {
1040
                $xml = simplexml_load_string($xml);
1041
            }
1042
1043
            foreach ($xml->children() as $r) {
1044
                if (count($r->children()) == 0) {
1045
                    $arr[$r->getName()] = strval($r);
1046
                } else {
1047
                    $arr[$r->getName()][] = self::xml2array($r);
1048
                }
1049
            }
1050
        }
1051
        return $arr;
1052
    }
1053
1054
    /**
1055
     * Odpojení od FlexiBee.
1056
     */
1057
    public function disconnect()
1058
    {
1059 15
        if (is_resource($this->curl)) {
1060
            curl_close($this->curl);
1061 15
        }
1062 15
        $this->curl = null;
1063
    }
1064 15
1065 8
    /**
1066 7
     * Disconnect CURL befere pass away
1067 7
     */
1068 7
    public function __destruct()
1069
    {
1070 8
        $this->disconnect();
1071 1
    }
1072 1
1073 8
    /**
1074
     * Načte řádek dat z FlexiBee.
1075 15
     *
1076 4
     * @param int $recordID id požadovaného záznamu
1077 4
     *
1078 4
     * @return array
1079
     */
1080
    public function getFlexiRow($recordID)
1081
    {
1082
        $record   = null;
1083 4
        $response = $this->performRequest($this->evidence.'/'.$recordID.'.json');
1084
        if (isset($response[$this->evidence])) {
1085 15
            $record = $response[$this->evidence][0];
1086
        }
1087 15
1088 15
        return $record;
1089
    }
1090
1091 15
    /**
1092
     * Oddělí z pole podmínek ty jenž patří za ? v URL požadavku
1093 15
     *
1094 15
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
1095 15
     * @param array $conditions pole podmínek   - rendrují se do ()
1096
     * @param array $urlParams  pole parametrů  - rendrují za ?
1097 15
     */
1098
    public function extractUrlParams(&$conditions, &$urlParams)
1099 15
    {
1100 15
        foreach ($this->urlParams as $urlParam) {
1101 15
            if (isset($conditions[$urlParam])) {
1102 9
                \Ease\Sand::divDataArray($conditions, $urlParams, $urlParam);
1103 9
            }
1104 6
        }
1105 6
    }
1106 9
1107 6
    /**
1108
     * convert unicode to entities
1109
     *
1110 15
     * @param string $urlRaw
1111
     * @return string
1112
     */
1113
    public static function urlEncode($urlRaw)
1114
    {
1115
        return str_replace(['%27'], ["'"], rawurlencode($urlRaw));
1116
    }
1117
1118
    /**
1119
     * Načte data z FlexiBee.
1120
     *
1121 23
     * @param string $suffix     dotaz
1122
     * @param string|array $conditions Volitelný filtrovací výraz
1123 23
     *
1124 23
     * @return array Data obtained
1125 23
     */
1126 23
    public function getFlexiData($suffix = null, $conditions = null)
1127 23
    {
1128
        $finalUrl  = '';
1129
        $urlParams = $this->defaultUrlParams;
1130
1131 23
        if (!empty($conditions)) {
1132
            if (is_array($conditions)) {
1133
                $this->extractUrlParams($conditions, $urlParams);
1134
                $conditions = $this->flexiUrl($conditions);
1135 23
            }
1136 22
1137 22
            if (strlen($conditions) && ($conditions[0] != '/')) {
1138 9
                $conditions = '('.self::urlEncode($conditions).')';
1139 9
            }
1140 22
        }
1141
1142
        if (strlen($suffix)) {
1143
            if (preg_match('/^http/', $suffix) || ($suffix[0] == '/') || is_numeric($suffix)) {
1144
                $finalUrl = $suffix;
1145
            } else {
1146
                if (preg_match('/^(code|ext):(.*)/', $suffix, $matches)) {
1147
                    $finalUrl = $matches[1].':'.rawurlencode($matches[2]);
1148
                }
1149
            }
1150
        }
1151 6
1152
        $finalUrl .= $conditions;
1153
1154 6
        if (count($urlParams)) {
1155 6
            if (strstr($finalUrl, '?')) {
1156 6
                $finalUrl .= '&';
1157 6
            } else {
1158 6
                $finalUrl .= '?';
1159
            }
1160 6
            $finalUrl .= http_build_query($urlParams, null, '&',
1161
                PHP_QUERY_RFC3986);
1162
        }
1163
1164
        $transactions = $this->performRequest($finalUrl, 'GET');
1165 6
1166
        $responseEvidence = $this->getResponseEvidence();
1167
        if (is_array($transactions) && array_key_exists($responseEvidence,
1168
                $transactions)) {
1169 6
            $result = $transactions[$responseEvidence];
1170
            if ((count($result) == 1) && (count(current($result)) == 0 )) {
1171
                $result = null; // Response is empty Array
1172
            }
1173
        } else {
1174
            $result = $transactions;
1175
        }
1176
1177 16
        return $result;
1178
    }
1179 16
1180 9
    /**
1181 9
     * Načte záznam z FlexiBee a uloží v sobě jeho data
1182 16
     * Read FlexiBee record and store it inside od object
1183 16
     *
1184 16
     * @param int $id ID or conditions
1185
     *
1186 16
     * @return int počet načtených položek
1187 16
     */
1188 16
    public function loadFromFlexiBee($id = null)
1189 15
    {
1190 15
        $data = [];
1191
        if (is_null($id)) {
1192
            $id = $this->getMyKey();
1193
        }
1194
        if (is_array($id)) {
1195
            $id = rawurlencode('('.self::flexiUrl($id).')');
1196
        }
1197
1198
        if (preg_match('/^code/', $id)) {
1199 15
            $id = self::code(rawurlencode(self::uncode($id)));
1200
        }
1201
1202 15
        $flexidata    = $this->getFlexiData($this->getEvidenceUrl().'/'.$id);
1203 10
        $this->apiURL = $this->curlInfo['url'];
1204 10
        if (is_array($flexidata) && (count($flexidata) == 1)) {
1205 15
            $data = current($flexidata);
1206 15
        }
1207 15
        return $this->takeData($data);
1208 15
    }
1209
1210 14
    /**
1211 14
     * Převede data do Json formátu pro FlexiBee.
1212 14
     * Convert data to FlexiBee like Json format
1213 14
     * 
1214 10
     * @url https://www.flexibee.eu/api/dokumentace/ref/actions/
1215
     * @url https://www.flexibee.eu/api/dokumentace/ref/zamykani-odemykani/
1216 14
     *
1217 14
     * @param array $data    object data 
1218
     * @param int   $options json_encode options like JSON_PRETTY_PRINT etc 
1219
     *
1220
     * @return string
1221
     */
1222
    public function getJsonizedData($data = null, $options = 0)
1223
    {
1224
        if (is_null($data)) {
1225
            $data = $this->getData();
1226
        }
1227
1228
        $dataToJsonize = array_merge(['@version' => $this->protoVersion],
1229
            $this->getDataForJSON($data));
1230
        $jsonRaw       = json_encode([$this->nameSpace => $dataToJsonize],
1231
            $options);
1232
1233
        return $jsonRaw;
1234
    }
1235
1236
    /**
1237
     * Get Data Fragment specific for current object
1238
     * 
1239
     * @param array $data
1240
     * 
1241
     * @return array
1242
     */
1243
    public function getDataForJSON($data = null)
1244
    {
1245
        if (is_null($data)) {
1246
            $data = $this->getData();
1247
        }
1248
1249
        $dataForJson = [$this->getEvidence() => $data];
1250
1251
        if (!is_null($this->action)) {
1252 15
            $dataForJson[$this->evidence.'@action'] = $this->action;
1253
            $this->action                           = null;
1254
        }
1255 15
1256 15
        if (!is_null($this->filter)) {
1257 15
            $dataForJson[$this->evidence.'@filter'] = $this->filter;
1258
        }
1259 15
1260 15
1261 15
        foreach ($this->chained as $chained) {
1262 15
            $chainedData = $chained->getDataForJSON();
1263 15
            foreach ($chainedData as $chainedItemEvidence => $chainedItemData) {
1264 15
                if (array_key_exists($chainedItemEvidence, $dataForJson)) {
1265 15
                    if (is_string(key($dataForJson[$chainedItemEvidence]))) {
1266 15
                        $dataBackup                          = $dataForJson[$chainedItemEvidence];
1267
                        $dataForJson[$chainedItemEvidence]   = [];
1268 15
                        $dataForJson[$chainedItemEvidence][] = $dataBackup;
1269
                    }
1270
                    if (array_key_exists(0, $chainedItemData)) {
1271 15
                        foreach ($chainedItemData as $chainedItem) {
1272
                            $dataForJson[$chainedItemEvidence][] = $chainedItem;
1273
                        }
1274 15
                    } else {
1275 15
                        $dataForJson[$chainedItemEvidence][] = $chainedItemData;
1276 15
                    }
1277 15
                } else {
1278 15
                    $dataForJson[$chainedItemEvidence] = $chainedItemData;
1279
                }
1280 15
            }
1281
        }
1282 15
1283
1284 15
        return $dataForJson;
1285 9
    }
1286 9
1287
    /**
1288 15
     * Join another FlexiPeeHP Object
1289
     * 
1290
     * @param FlexiBeeRO $object
1291
     * 
1292
     * @return boolean adding to stack success
1293
     */
1294
    public function join($object)
1295
    {
1296
        $result = true;
1297
        if (method_exists($object, 'getDataForJSON')) {
1298
            $this->chained[] = $object;
1299 23
        } else {
1300
            throw new \Ease\Exception('$object->getDataForJSON() does not exist');
1301 23
        }
1302
1303 23
        return $result;
1304 23
    }
1305 23
1306
    /**
1307 23
     * Test if given record ID exists in FlexiBee.
1308 23
     *
1309 23
     * @param mixed $identifer presence state
1310
     * 
1311 23
     * @return boolean
1312 23
     */
1313 23
    public function idExists($identifer = null)
1314 23
    {
1315 23
        if (is_null($identifer)) {
1316 23
            $identifer = $this->getMyKey();
1317 23
        }
1318 23
        $ignorestate = $this->ignore404();
1319 23
        $this->ignore404(true);
1320 23
        $this->getFlexiData(null,
1321
            [
1322
                'detail' => 'custom:'.$this->getmyKeyColumn(),
1323
                $this->getmyKeyColumn() => $identifer
1324 23
        ]);
1325 23
        $this->ignore404($ignorestate);
1326 23
        return $this->lastResponseCode == 200;
1327
    }
1328 23
1329 23
    /**
1330 23
     * Test if given record exists in FlexiBee.
1331 23
     *
1332
     * @param array $data
1333
     * @return boolean Record presence status
1334 23
     */
1335 23
    public function recordExists($data = [])
1336 23
    {
1337 23
1338 23
        if (empty($data)) {
1339 23
            $data = $this->getData();
1340 23
        }
1341 23
        $ignorestate = $this->ignore404();
1342 23
        $this->ignore404(true);
1343 23
        $res         = $this->getColumnsFromFlexibee([$this->getKeyColumn()],
0 ignored issues
show
Bug introduced by
The method getKeyColumn() does not exist on FlexiPeeHP\FlexiBeeRO. Did you maybe mean getmyKeyColumn()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
1344 23
            [self::flexiUrl($data)]);
1345 23
1346
        if (!count($res) || (isset($res['success']) && ($res['success'] == 'false'))
1347 23
            || !count($res[0])) {
1348 23
            $found = false;
1349
        } else {
1350 23
            $found = true;
1351
        }
1352
        $this->ignore404($ignorestate);
1353
        return $found;
1354
    }
1355
1356
    /**
1357
     * Vrací z FlexiBee sloupečky podle podmínek.
1358
     *
1359
     * @param array|int|string $conditions pole podmínek nebo ID záznamu
1360 23
     * @param string           $indexBy    klice vysledku naplnit hodnotou ze
1361
     *                                     sloupečku
1362 23
     * @return array
1363 23
     */
1364 23
    public function getAllFromFlexibee($conditions = null, $indexBy = null)
1365
    {
1366
        if (is_int($conditions)) {
1367 23
            $conditions = [$this->getmyKeyColumn() => $conditions];
1368 23
        }
1369 23
1370 23
        $flexiData = $this->getFlexiData('', $conditions);
1371 23
1372
        if (!is_null($indexBy)) {
1373
            $flexiData = $this->reindexArrayBy($flexiData);
1374 23
        }
1375 23
1376 23
        return $flexiData;
1377
    }
1378 23
1379 23
    /**
1380 23
     * Vrací z FlexiBee sloupečky podle podmínek.
1381 23
     *
1382 23
     * @param string[] $columnsList seznam položek
1383
     * @param array    $conditions  pole podmínek nebo ID záznamu
1384 23
     * @param string   $indexBy     Sloupeček podle kterého indexovat záznamy
1385 23
     *
1386 23
     * @return array
1387 23
     */
1388 23
    public function getColumnsFromFlexibee($columnsList, $conditions = [],
1389
                                           $indexBy = null)
1390 23
    {
1391 23
        $detail = 'full';
1392 23
        switch (gettype($columnsList)) {
1393 23
            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...
1394
                $conditions = [$this->getmyKeyColumn() => $conditions];
1395
            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...
1396 23
                if (!is_null($indexBy) && !array_key_exists($indexBy,
1397
                        $columnsList)) {
1398
                    $columnsList[] = $indexBy;
1399 23
                }
1400
                $columns = implode(',', array_unique($columnsList));
1401
                $detail  = 'custom:'.$columns;
1402 23
            default:
1403 23
                switch ($columnsList) {
1404 23
                    case 'id':
1405 23
                        $detail = 'id';
1406 23
                        break;
1407 23
                    case 'summary':
1408
                        $detail = 'summary';
1409
                        break;
1410
                    default:
1411
                        break;
1412
                }
1413
                break;
1414
        }
1415
1416
        $conditions['detail'] = $detail;
1417
1418
        $flexiData = $this->getFlexiData(null, $conditions);
1419
1420
        if (!is_null($indexBy) && count($flexiData) && count(current($flexiData))) {
1421
            $flexiData = $this->reindexArrayBy($flexiData, $indexBy);
1422
        }
1423
1424
        return $flexiData;
1425
    }
1426
1427
    /**
1428
     * Vrací kód záznamu.
1429
     * Obtain record CODE
1430
     *
1431
     * @param mixed $data
1432
     *
1433
     * @return string
1434
     */
1435
    public function getKod($data = null, $unique = true)
1436
    {
1437
        $kod = null;
1438
1439
        if (is_null($data)) {
1440
            $data = $this->getData();
1441
        }
1442
1443
        if (is_string($data)) {
1444 23
            $data = [$this->nameColumn => $data];
1445
        }
1446 23
1447
        if (isset($data['kod'])) {
1448 23
            $kod = $data['kod'];
1449 23
        } else {
1450 23
            if (isset($data[$this->nameColumn])) {
1451 23
                $kod = preg_replace('/[^a-zA-Z0-9]/', '',
1452 23
                    \Ease\Sand::rip($data[$this->nameColumn]));
1453 23
            } else {
1454 23
                if (isset($data[$this->keyColumn])) {
0 ignored issues
show
Bug introduced by
The property keyColumn does not seem to exist. Did you mean fbKeyColumn?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1455 23
                    $kod = \Ease\Sand::rip($data[$this->keyColumn]);
0 ignored issues
show
Bug introduced by
The property keyColumn does not seem to exist. Did you mean fbKeyColumn?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1456 23
                }
1457
            }
1458 23
        }
1459 23
1460 23
        if (!strlen($kod)) {
1461 23
            $kod = 'NOTSET';
1462 23
        }
1463
1464
        if (strlen($kod) > 18) {
1465 23
            $kodfinal = strtoupper(substr($kod, 0, 18));
1466 23
        } else {
1467
            $kodfinal = strtoupper($kod);
1468
        }
1469 23
1470
        if ($unique) {
1471 23
            $counter = 0;
1472 23
            if (count($this->codes)) {
1473
                foreach ($this->codes as $codesearch => $keystring) {
1474 23
                    if (strstr($codesearch, $kodfinal)) {
1475
                        ++$counter;
1476
                    }
1477 23
                }
1478 23
            }
1479
            if ($counter) {
1480
                $kodfinal = $kodfinal.$counter;
1481
            }
1482
1483
            $this->codes[$kodfinal] = $kod;
1484
        }
1485
1486
        return self::code($kodfinal);
1487
    }
1488
1489 65
    /**
1490
     * Write Operation Result.
1491 65
     *
1492 65
     * @param array  $resultData
1493
     * @param string $url        URL
1494
     * @return boolean Log save success
1495 65
     */
1496 65
    public function logResult($resultData = null, $url = null)
1497
    {
1498
        $logResult = false;
1499
        if (isset($resultData['success']) && ($resultData['success'] == 'false')) {
1500
            if (isset($resultData['message'])) {
1501 65
                $this->addStatusMessage($resultData['message'], 'warning');
1502
            }
1503
            $this->addStatusMessage('Error '.$this->lastResponseCode.': '.urldecode($url),
1504
                'warning');
1505
            unset($url);
1506
        }
1507
        if (is_null($resultData)) {
1508
            $resultData = $this->lastResult;
1509
        }
1510
        if (isset($url)) {
1511 71
            $this->logger->addStatusMessage($this->lastResponseCode.':'.urldecode($url));
1512
        }
1513 71
1514
        if (isset($resultData['results'])) {
1515
            if ($resultData['success'] == 'false') {
1516
                $status = 'error';
1517
            } else {
1518
                $status = 'success';
1519
            }
1520
            foreach ($resultData['results'] as $result) {
1521
                if (isset($result['request-id'])) {
1522 23
                    $rid = $result['request-id'];
1523
                } else {
1524 23
                    $rid = '';
1525
                }
1526
                if (isset($result['errors'])) {
1527
                    foreach ($result['errors'] as $error) {
1528
                        $message = $error['message'];
1529
                        if (isset($error['for'])) {
1530
                            $message .= ' for: '.$error['for'];
1531
                        }
1532 15
                        if (isset($error['value'])) {
1533
                            $message .= ' value:'.$error['value'];
1534 15
                        }
1535 15
                        if (isset($error['code'])) {
1536 15
                            $message .= ' code:'.$error['code'];
1537 15
                        }
1538 15
                        $this->addStatusMessage($rid.': '.$message, $status);
1539 9
                    }
1540 9
                }
1541 15
            }
1542
        }
1543
        return $logResult;
1544
    }
1545
1546
    /**
1547
     * Save RAW Curl Request & Response to files in Temp directory
1548
     */
1549
    public function saveDebugFiles()
1550 23
    {
1551
        $tmpdir   = sys_get_temp_dir();
1552 23
        $fname    = $this->evidence.'-'.$this->curlInfo['when'].'.'.$this->format;
1553 23
        $reqname  = $tmpdir.'/request-'.$fname;
1554 23
        $respname = $tmpdir.'/response-'.$fname;
1555 23
        file_put_contents($reqname, $this->postFields);
1556 23
        file_put_contents($respname, $this->lastCurlResponse);
1557 23
    }
1558 23
1559 23
    /**
1560 23
     * Připraví data pro odeslání do FlexiBee
1561 23
     *
1562 23
     * @param string $data
1563 23
     */
1564 23
    public function setPostFields($data)
1565 23
    {
1566
        $this->postFields = $data;
1567 23
    }
1568
1569
    /**
1570
     * Generuje fragment url pro filtrování.
1571
     *
1572
     * @see https://www.flexibee.eu/api/dokumentace/ref/filters
1573
     *
1574
     * @param array  $data
1575
     * @param string $joiner default and/or
1576
     * @param string $defop  default operator
1577 22
     *
1578
     * @return string
1579 22
     */
1580
    public static function flexiUrl(array $data, $joiner = 'and', $defop = 'eq')
1581 21
    {
1582
        $parts = [];
1583
1584
        foreach ($data as $column => $value) {
1585
            if (!is_numeric($column)) {
1586
                if (is_integer($data[$column]) || is_float($data[$column])) {
1587
                    $parts[$column] = $column.' eq \''.$data[$column].'\'';
1588
                } elseif (is_bool($data[$column])) {
1589 22
                    $parts[$column] = $data[$column] ? $column.' eq true' : $column.' eq false';
1590
                } elseif (is_null($data[$column])) {
1591 22
                    $parts[$column] = $column." is null";
1592 22
                } else {
1593 22
                    switch ($value) {
1594
                        case '!null':
1595
                            $parts[$column] = $column." is not null";
1596 22
                            break;
1597
                        case 'is empty':
1598
                        case 'is not empty':
1599
                            $parts[$column] = $column.' '.$value;
1600
                            break;
1601
                        default:
1602
                            if ($column == 'stitky') {
1603
                                $parts[$column] = $column."='".self::code($data[$column])."'";
1604
                            } else {
1605 22
                                $parts[$column] = $column." $defop '".$data[$column]."'";
1606
                            }
1607 22
                            break;
1608 22
                    }
1609 22
                }
1610 22
            } else {
1611 22
                $parts[] = $value;
1612 22
            }
1613 22
        }
1614 22
        return implode(' '.$joiner.' ', $parts);
1615 22
    }
1616 22
1617 22
    /**
1618 22
     * Obtain record/object numeric identificator id:
1619
     * Vrací číselný identifikátor objektu id:
1620 22
     *
1621
     * @link https://demo.flexibee.eu/devdoc/identifiers Identifikátory záznamů
1622
     *
1623
     * @return null|int indentifikátor záznamu reprezentovaného objektem
1624
     */
1625
    public function getRecordID()
1626
    {
1627
        $id = $this->getDataValue('id');
1628
        return is_null($id) ? null : intval($id);
1629
    }
1630
1631
    /**
1632 22
     * Obtain record/object identificator code:
1633
     * Vrací identifikátor objektu code:
1634
     *
1635
     * @link https://demo.flexibee.eu/devdoc/identifiers Identifikátory záznamů
1636
     *
1637
     * @return string record code identifier
1638
     */
1639
    public function getRecordCode()
1640
    {
1641 23
        return empty($this->getDataValue('kod')) ? null : self::code($this->getDataValue('kod'));
1642
    }
1643 23
1644 23
    /**
1645 23
     * Obtain record/object identificator extId: code: or id:
1646 23
     * Vrací identifikátor objektu extId: code: nebo id:
1647 16
     *
1648 16
     * @link https://demo.flexibee.eu/devdoc/identifiers Identifikátory záznamů
1649 23
     *
1650
     * @return string|int|null record code identifier
1651
     */
1652
    public function getRecordIdent()
1653
    {
1654
        $ident = $this->getExternalID();
1655
        if (empty($ident)) {
1656
            $ident = $this->getRecordCode();
1657
        }
1658 23
        if (empty($ident)) {
1659
            $ident = $this->getRecordID();
1660 23
        }
1661 23
        return $ident;
1662 23
    }
1663 23
1664 23
    /**
1665 23
     * Obtain record/object identificator code: or id:
1666 23
     * Vrací identifikátor objektu code: nebo id:
1667 23
     *
1668 23
     * @link https://demo.flexibee.eu/devdoc/identifiers Identifikátory záznamů
1669
     * @return string indentifikátor záznamu reprezentovaného objektem
1670
     */
1671
    public function __toString()
1672
    {
1673
        return strval($this->getRecordIdent());
1674
    }
1675
1676
    /**
1677 23
     * Gives you FlexiPeeHP class name for Given Evidence
1678
     *
1679 23
     * @param string $evidence
1680 23
     * @return string Class name
1681 23
     */
1682 23
    public static function evidenceToClassName($evidence)
1683 23
    {
1684 23
        return str_replace(' ', '', ucwords(str_replace('-', ' ', $evidence)));
1685 13
    }
1686 13
1687 23
    /**
1688
     * Obtain ID of first record in evidence
1689
     *
1690
     * @return string|null id or null if no records
1691
     */
1692
    public function getFirstRecordID()
1693
    {
1694
        $firstID    = null;
1695
        $keyColumn  = $this->getmyKeyColumn();
1696 23
        $firstIdRaw = $this->getColumnsFromFlexibee([$keyColumn],
1697
            ['limit' => 1, 'order' => $keyColumn], $keyColumn);
1698 23
        if (count($firstIdRaw)) {
1699 23
            $firstID = current($firstIdRaw)[$keyColumn];
1700 23
        }
1701 23
        return is_numeric($firstID) ? intval($firstID) : $firstID;
1702 23
    }
1703 16
1704 16
    /**
1705 23
     * Vrací hodnotu daného externího ID
1706
     *
1707
     * @param string $want Which ? If empty,you obtain the first one.
1708
     * @return string
1709
     */
1710
    public function getExternalID($want = null)
1711
    {
1712
        $extid = null;
1713
        $ids   = $this->getDataValue('external-ids');
1714 23
        if (is_null($want)) {
1715
            if (count($ids)) {
1716 23
                $extid = current($ids);
1717 23
            }
1718 23
        } else {
1719 23
            if (!is_null($ids) && is_array($ids)) {
1720 23
                foreach ($ids as $id) {
1721 16
                    if (strstr($id, 'ext:'.$want)) {
1722 16
                        $extid = str_replace('ext:'.$want.':', '', $id);
1723 23
                    }
1724
                }
1725
            }
1726
        }
1727
        return $extid;
1728
    }
1729
1730
    /**
1731 23
     * Obtain actual GlobalVersion
1732
     * Vrací aktuální globální verzi změn
1733 23
     *
1734 1
     * @link https://www.flexibee.eu/api/dokumentace/ref/changes-api#globalVersion Globální Verze
1735 1
     * @return type
1736 23
     */
1737 23
    public function getGlobalVersion()
1738
    {
1739
        $this->getFlexiData(null, ['add-global-version' => 'true', 'limit' => 1]);
1740
1741
        return $this->globalVersion;
1742
    }
1743
1744 20
    /**
1745
     * Obtain content type of last response
1746 20
     *
1747 20
     * @return string
1748 20
     */
1749 20
    public function getResponseFormat()
1750
    {
1751
        if (isset($this->curlInfo['content_type'])) {
1752
            $responseFormat = $this->curlInfo['content_type'];
1753
        } else {
1754
            $responseFormat = null;
1755 20
        }
1756
        return $responseFormat;
1757
    }
1758
1759
    /**
1760
     * Return the same response format for one and multiplete results
1761
     *
1762
     * @param array $responseBody
1763
     * @return array
1764
     */
1765
    public function unifyResponseFormat($responseBody)
1766
    {
1767
        if (!is_array($responseBody) || array_key_exists('message',
1768
                $responseBody)) { //Unifi response format
1769
            $response = $responseBody;
1770
        } else {
1771
            $evidence = $this->getResponseEvidence();
1772
            if (array_key_exists($evidence, $responseBody)) {
1773
                $response        = [];
1774
                $evidenceContent = $responseBody[$evidence];
1775
                if (array_key_exists(0, $evidenceContent)) {
1776
                    $response[$evidence] = $evidenceContent; //Multiplete Results
1777
                } else {
1778
                    $response[$evidence][0] = $evidenceContent; //One result
1779
                }
1780
            } else {
1781
                if (isset($responseBody['priloha'])) {
1782
                    $response = $responseBody['priloha'];
1783
                } else {
1784
                    if (array_key_exists('results', $responseBody)) {
1785
                        $response = $responseBody['results'];
1786
                    } else {
1787
                        $response = $responseBody;
1788
                    }
1789
                }
1790
            }
1791
        }
1792
        return $response;
1793
    }
1794
1795
    /**
1796
     * Obtain structure for current (or given) evidence
1797
     *
1798
     * @param string $evidence
1799
     * @return array Evidence structure
1800
     */
1801
    public function getColumnsInfo($evidence = null)
1802
    {
1803
        $columnsInfo = null;
1804
        $infoSource  = self::$infoDir.'/Properties.'.(empty($evidence) ? $this->getEvidence()
1805
                : $evidence).'.json';
1806
        if (file_exists($infoSource)) {
1807
            $columnsInfo = json_decode(file_get_contents($infoSource), true);
1808
        }
1809
        return $columnsInfo;
1810
    }
1811
1812
    /**
1813
     * Gives you properties for (current) evidence column
1814
     * 
1815
     * @param string $column    name of column
1816
     * @param string $evidence  evidence name if different
1817
     * 
1818
     * @return array column properties or null if column not exits
1819
     */
1820
    public function getColumnInfo($column, $evidence = null)
1821
    {
1822
        $columnsInfo = $this->getColumnsInfo(empty($evidence) ? $this->getEvidence()
1823
                : $evidence);
1824
        return array_key_exists($column, $columnsInfo) ? $columnsInfo[$column] : null;
1825
    }
1826
1827
    /**
1828
     * Obtain actions for current (or given) evidence
1829
     *
1830
     * @param string $evidence
1831
     * @return array Evidence structure
1832
     */
1833
    public function getActionsInfo($evidence = null)
1834
    {
1835
        $actionsInfo = null;
1836
        if (is_null($evidence)) {
1837
            $evidence = $this->getEvidence();
1838
        }
1839
        $propsName = lcfirst(FlexiBeeRO::evidenceToClassName($evidence));
1840
        if (isset(\FlexiPeeHP\Actions::$$propsName)) {
1841
            $actionsInfo = Actions::$$propsName;
1842
        }
1843
        return $actionsInfo;
1844
    }
1845 23
1846
    /**
1847 23
     * Obtain relations for current (or given) evidence
1848
     *
1849
     * @param string $evidence
1850
     * @return array Evidence structure
1851
     */
1852
    public function getRelationsInfo($evidence = null)
1853
    {
1854
        $relationsInfo = null;
1855
        if (is_null($evidence)) {
1856
            $evidence = $this->getEvidence();
1857 23
        }
1858
        $propsName = lcfirst(FlexiBeeRO::evidenceToClassName($evidence));
1859 23
        if (isset(\FlexiPeeHP\Relations::$$propsName)) {
1860
            $relationsInfo = Relations::$$propsName;
1861
        }
1862
        return $relationsInfo;
1863
    }
1864
1865
    /**
1866
     * Obtain info for current (or given) evidence
1867
     *
1868
     * @param string $evidence
1869
     * @return array Evidence info
1870
     */
1871 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...
1872
    {
1873
        $evidencesInfo = null;
1874
        if (is_null($evidence)) {
1875
            $evidence = $this->getEvidence();
1876
        }
1877
        if (isset(EvidenceList::$evidences[$evidence])) {
1878
            $evidencesInfo = EvidenceList::$evidences[$evidence];
1879
        }
1880
        return $evidencesInfo;
1881
    }
1882
1883
    /**
1884
     * Obtain name for current (or given) evidence path
1885
     *
1886
     * @param string $evidence Evidence Path
1887
     * @return array Evidence info
1888
     */
1889 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...
1890
    {
1891
        $evidenceName = null;
1892
        if (is_null($evidence)) {
1893
            $evidence = $this->getEvidence();
1894
        }
1895
        if (isset(EvidenceList::$name[$evidence])) {
1896
            $evidenceName = EvidenceList::$name[$evidence];
1897
        }
1898
        return $evidenceName;
1899
    }
1900
1901
    /**
1902
     * Save current object to file
1903
     *
1904
     * @param string $destfile path to file
1905
     */
1906
    public function saveResponseToFile($destfile)
1907
    {
1908
        if (strlen($this->lastCurlResponse)) {
1909
            $this->doCurlRequest($this->apiURL, 'GET', $this->format);
1910
        }
1911
        file_put_contents($destfile, $this->lastCurlResponse);
1912
    }
1913
1914
    /**
1915
     * Obtain established relations listing
1916
     *
1917
     * @return array Null or Relations
1918
     */
1919
    public function getVazby($id = null)
1920
    {
1921
        if (is_null($id)) {
1922
            $id = $this->getRecordID();
1923
        }
1924
        if (!empty($id)) {
1925
            $vazbyRaw = $this->getColumnsFromFlexibee(['vazby'],
1926
                ['relations' => 'vazby', 'id' => $id]);
1927
            $vazby    = array_key_exists('vazby', $vazbyRaw[0]) ? $vazbyRaw[0]['vazby']
1928
                    : null;
1929
        } else {
1930
            throw new \Exception(_('ID requied to get record relations '));
1931
        }
1932
        return $vazby;
1933
    }
1934
1935
    /**
1936
     * Gives You URL for Current Record in FlexiBee web interface
1937
     *
1938
     * @return string url
1939
     */
1940
    public function getFlexiBeeURL()
1941
    {
1942
        $parsed_url = parse_url(str_replace('.'.$this->format, '', $this->apiURL));
1943
        $scheme     = isset($parsed_url['scheme']) ? $parsed_url['scheme'].'://'
1944
                : '';
1945
        $host       = isset($parsed_url['host']) ? $parsed_url['host'] : '';
1946
        $port       = isset($parsed_url['port']) ? ':'.$parsed_url['port'] : '';
1947
        $user       = isset($parsed_url['user']) ? $parsed_url['user'] : '';
1948
        $pass       = isset($parsed_url['pass']) ? ':'.$parsed_url['pass'] : '';
1949
        $pass       = ($user || $pass) ? "$pass@" : '';
1950
        $path       = isset($parsed_url['path']) ? $parsed_url['path'] : '';
1951
        return $scheme.$user.$pass.$host.$port.$path;
1952
    }
1953
1954
    /**
1955
     * Set Record Key
1956
     *
1957
     * @param int|string $myKeyValue
1958
     * @return boolean
1959
     */
1960
    public function setMyKey($myKeyValue)
1961
    {
1962
        if (substr($myKeyValue, 0, 4) == 'ext:') {
1963
            $extIds = $this->getDataValue('external-ids');
1964
            if (count($extIds)) {
1965
                $extIds = array_combine($extIds, $extIds);
1966
            }
1967
            $extIds[$myKeyValue] = $myKeyValue;
1968
            $res                 = $this->setDataValue('external-ids', $extIds);
1969
        } else {
1970
            $res = parent::setMyKey($myKeyValue);
1971
        }
1972
        $this->updateApiURL();
1973
        return $res;
1974
    }
1975
1976
    /**
1977
     * Set or get ignore not found pages flag
1978
     *
1979
     * @param boolean $ignore set flag to
1980
     *
1981
     * @return boolean get flag state
1982
     */
1983
    public function ignore404($ignore = null)
1984
    {
1985
        if (!is_null($ignore)) {
1986
            $this->ignoreNotFound = $ignore;
1987
        }
1988
        return $this->ignoreNotFound;
1989
    }
1990
1991
    /**
1992
     * Send Document by mail
1993
     *
1994
     * @url https://www.flexibee.eu/api/dokumentace/ref/odesilani-mailem/
1995
     *
1996
     * @param string $to         Email ecipient
1997
     * @param string $subject    Email Subject
1998
     * @param string $body       Email Text
1999
     *
2000
     * @return int http response code
2001
     */
2002
    public function sendByMail($to, $subject, $body, $cc = null)
2003
    {
2004
        $this->setPostFields($body);
2005
2006
        $this->performRequest(rawurlencode($this->getRecordID()).'/odeslani-dokladu?to='.$to.'&subject='.urlencode($subject).'&cc='.$cc
2007
            , 'PUT', 'xml');
2008
2009
        return $this->lastResponseCode == 200;
2010
    }
2011
2012
    /**
2013
     * Send all unsent Invoices by mail
2014
     *
2015
     * @url https://www.flexibee.eu/api/dokumentace/ref/odesilani-mailem/
2016
     * @return int http response code
2017
     */
2018
    public function sendUnsent()
2019
    {
2020
        return $this->doCurlRequest('automaticky-odeslat-neodeslane', 'PUT',
2021
                'xml');
2022
    }
2023
2024
    /**
2025
     * FlexiBee date to PHP DateTime conversion
2026
     *
2027
     * @param string $flexidate 2017-05-26+02:00
2028
     *
2029
     * @return \DateTime | false
2030
     */
2031
    public static function flexiDateToDateTime($flexidate)
2032
    {
2033
        return \DateTime::createFromFormat(self::$DateFormat.'O', $flexidate)->setTime(0,
2034
                0);
2035 23
    }
2036
2037 23
    /**
2038 23
     * FlexiBee dateTime to PHP DateTime conversion
2039 23
     *
2040 23
     * @param string $flexidatetime 2017-09-26T10:00:53.755+02:00 or older 2017-05-19T00:00:00+02:00
2041
     *
2042
     * @return \DateTime | false
2043
     */
2044
    public static function flexiDateTimeToDateTime($flexidatetime)
2045
    {
2046
        if (strchr($flexidatetime, '.')) { //NewFormat
2047
            $format = self::$DateTimeFormat;
2048
        } else { // Old format
2049
            $format = 'Y-m-d\TH:i:s+P';
2050
        }
2051
        return \DateTime::createFromFormat($format, $flexidatetime);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression \DateTime::createFromFor...ormat, $flexidatetime); of type DateTime|false adds false to the return on line 2051 which is incompatible with the return type documented by FlexiPeeHP\FlexiBeeRO::flexiDateTimeToDateTime of type DateTime. It seems like you forgot to handle an error condition.
Loading history...
2052
    }
2053
2054
    /**
2055
     * Získá dokument v daném formátu
2056
     * Obtain document in given format
2057
     *
2058
     * @param string $format  pdf/csv/xml/json/ ...
2059
     * @param string $reportName Template used to generate PDF
0 ignored issues
show
Bug introduced by
There is no parameter named $reportName. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
2060
     *
2061
     * @return string|null filename downloaded or none
2062
     */
2063
    public function getInFormat($format)
2064
    {
2065
        $response = null;
2066
        if ($this->setFormat($format)) {
2067
            $urlParams = [];
2068
            if (!empty($reportName)) {
0 ignored issues
show
Bug introduced by
The variable $reportName seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
2069
                $urlParams['report-name'] = $reportName;
2070
            }
2071
            if ($format == 'html') {
2072
                $urlParams['inDesktopApp'] = 'true';
2073
            }
2074
            if (($this->doCurlRequest($this->addUrlParams($this->apiURL,
2075
                        $urlParams), 'GET') == 200)) {
2076
                $response = $this->lastCurlResponse;
2077
            }
2078
        }
2079
        return $response;
2080
    }
2081
2082
    /**
2083
     * Uloží dokument v daném formátu do složky v systému souborů
2084
     * Save document in given format to directory in filesystem
2085
     *
2086
     * @param string $format  pdf/csv/xml/json/ ...
2087
     * @param string $destDir where to put file (prefix)
2088
     * @param string $reportName Template used to generate PDF
2089
     *
2090
     * @return string|null filename downloaded or none
2091
     */
2092
    public function downloadInFormat($format, $destDir = './',
2093
                                     $reportName = null)
2094
    {
2095
        $fileOnDisk   = null;
2096
        $formatBackup = $this->format;
2097
        if ($this->setFormat($format)) {
2098
            $downloadTo = $destDir.$this->getEvidence().'_'.$this->getMyKey().'.'.$format;
2099
            if (($this->doCurlRequest(empty($reportName) ? $this->apiURL : $this->addUrlParams($this->apiURL,
2100
                            ['report-name' => $reportName]), 'GET') == 200) && (file_put_contents($downloadTo,
2101
                    $this->lastCurlResponse) !== false)) {
2102
                $fileOnDisk = $downloadTo;
2103
            }
2104
            $this->setFormat($formatBackup);
2105
        }
2106
        return $fileOnDisk;
2107
    }
2108
2109
    /**
2110
     * Compile and send Report about Error500 to FlexiBee developers
2111
     * If FlexiBee is running on localost try also include java backtrace
2112
     *
2113
     * @param array $errorResponse result of parseError();
2114
     */
2115
    public function error500Reporter($errorResponse)
2116
    {
2117
        $ur = str_replace('/c/'.$this->company, '',
2118
            str_replace($this->url, '', $this->curlInfo['url']));
2119
        if (!array_key_exists($ur, $this->reports)) {
2120
            $tmpdir   = sys_get_temp_dir();
2121
            $myTime   = $this->curlInfo['when'];
2122
            $curlname = $tmpdir.'/curl-'.$this->evidence.'-'.$myTime.'.json';
2123
            file_put_contents($curlname,
2124
                json_encode($this->curlInfo, JSON_PRETTY_PRINT));
2125
2126
            $report = new \Ease\Mailer($this->reportRecipient,
2127
                'Error report 500 - '.$ur);
2128
2129
            $d     = dir($tmpdir);
2130
            while (false !== ($entry = $d->read())) {
2131
                if (strstr($entry, $myTime)) {
2132
                    $ext  = pathinfo($tmpdir.'/'.$entry, PATHINFO_EXTENSION);
2133
                    $mime = Formats::suffixToContentType($ext);
2134
                    $report->addFile($tmpdir.'/'.$entry,
2135
                        empty($mime) ? 'text/plain' : $mime);
2136
                }
2137
            }
2138
            $d->close();
2139
2140
            if ((strstr($this->url, '://localhost') || strstr($this->url,
2141
                    '://127.')) && file_exists('/var/log/flexibee.log')) {
2142
2143
                $fl = fopen("/var/log/flexibee.log", "r");
2144
                if ($fl) {
2145
                    $tracelog = [];
2146
                    for ($x_pos = 0, $ln = 0, $output = array(); fseek($fl,
2147
                            $x_pos, SEEK_END) !== -1; $x_pos--) {
2148
                        $char = fgetc($fl);
2149
                        if ($char === "\n") {
2150
                            $tracelog[] = $output[$ln];
2151
                            if (strstr($output[$ln], $errorResponse['message'])) {
2152
                                break;
2153
                            }
2154
                            $ln++;
2155
                            continue;
2156
                        }
2157
                        $output[$ln] = $char.((array_key_exists($ln, $output)) ? $output[$ln]
2158
                                : '');
2159
                    }
2160
2161
                    $trace     = implode("\n", array_reverse($tracelog));
2162
                    $tracefile = $tmpdir.'/trace-'.$this->evidence.'-'.$myTime.'.log';
2163
                    file_put_contents($tracefile, $trace);
2164
                    $report->addItem("\n\n".$trace);
2165
                    fclose($fl);
2166
                }
2167
            } else {
2168
                $report->addItem($errorResponse['message']);
2169
            }
2170
2171
            $licenseInfo = $this->performRequest($this->url.'/default-license.json');
2172
2173
            $report->addItem("\n\n".json_encode($licenseInfo['license'],
2174
                    JSON_PRETTY_PRINT));
2175
2176
            if ($report->send()) {
2177
                $this->reports[$ur] = $myTime;
2178
            }
2179
        }
2180
    }
2181
2182
    /**
2183
     * Returns code:CODE
2184
     *
2185
     * @param string $code
2186
     *
2187
     * @return string
2188
     */
2189
    public static function code($code)
2190
    {
2191
        return 'code:'.self::uncode($code);
2192
    }
2193
2194
    /**
2195
     * Returns CODE without code: prefix
2196
     *
2197
     * @param string $code
2198
     *
2199
     * @return string
2200
     */
2201
    public static function uncode($code)
2202
    {
2203
        return str_replace(['code:', 'code%3A'], '', $code);
2204
    }
2205
2206
    /**
2207
     * Remove all @ items from array
2208
     *
2209
     * @param array $data original data
2210
     *
2211
     * @return array data without @ columns
2212
     */
2213
    public static function arrayCleanUP($data)
2214
    {
2215
        return array_filter(
2216
            $data,
2217
            function ($key) {
2218
            return !strchr($key, '@');
2219
        }, ARRAY_FILTER_USE_KEY);
2220
    }
2221
2222
    /**
2223
     * Add Info about used user, server and libraries
2224
     *
2225
     * @param string $additions Additional note text
2226
     */
2227
    function logBanner($additions = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
2228
    {
2229
        $this->addStatusMessage('FlexiBee '.str_replace('://',
2230
                '://'.$this->user.'@', str_replace('.json', '', $this->apiURL)).' FlexiPeeHP v'.self::$libVersion.' (FlexiBee '.EvidenceList::$version.') EasePHP Framework v'.\Ease\Atom::$frameworkVersion.' '.$additions,
2231
            'debug');
2232
    }
2233
2234
    /**
2235
     * Reconnect After unserialization
2236
     */
2237
    public function __wakeup()
2238
    {
2239
        parent::__wakeup();
2240
        $this->curlInit();
2241
    }
2242
}
2243