Passed
Push — master ( 40d774...35f4a7 )
by Vítězslav
23:37 queued 10s
created

RW::deleteFromAbraFlexi()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * FlexiPeeHP - Třída pro zápis do FlexiBee.
5
 *
6
 * @author     Vítězslav Dvořák <[email protected]>
7
 * @copyright  (C) 2015-2017 Spoje.Net
8
 */
9
10
namespace FlexiPeeHP;
11
12
/**
13
 * Základní třída pro zápis do FlexiBee
14
 *
15
 * @url https://demo.flexibee.eu/devdoc/http-operations
16
 */
17
class RW extends RO {
18
19
    /**
20
     * Sloupeček obsahující datum vložení záznamu do shopu.
21
     *
22
     * @var string
23
     */
24
    public $myCreateColumn = 'false';
25
26
    /**
27
     * Slopecek obsahujici datum poslení modifikace záznamu do shopu.
28
     *
29
     * @var string
30
     */
31
    public $myLastModifiedColumn = 'lastUpdate';
32
33
    /**
34
     * Last Inserted ID.
35
     *
36
     * @var int
37
     */
38
    public $lastInsertedID = null;
39
40
    /**
41
     * Array of fields for next curl POST operation
42
     * 
43
     * @var string
44
     */
45
    public $postFields = null;
46
47
    /**
48
     * Transaction processing mode
49
     * 
50
     * @link https://www.flexibee.eu/api/dokumentace/ref/tx/ Transakční zpracování
51
     * @var boolean
52
     */
53
    public $atomic = null;
54
55
    /**
56
     * Record Copy helper
57
     * 
58
     * @var int 
59
     */
60
    private $sourceId = null;
61
62
    /**
63
     * SetUp Object to be ready for work
64
     *
65
     * @param array $options Object Options (authSessionId,user,password,
66
     *                                       url,company,evidence,companyUrl
67
     *                                       prefix,defaultUrlParams,debug,ver
68
     *                                       detail,offline,atomic,filter,ignore404
69
     */
70
    public function setUp($options = array()) {
71
        if (array_key_exists('atomic', $options)) {
72
            $this->atomic = (boolean) $options['atomic'];
73
        }
74
        return parent::setUp($options);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::setUp($options) targeting FlexiPeeHP\RO::setUp() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
75
    }
76
77
    /**
78
     * Save record (if evidence allow to).
79
     * 
80
     * @deprecated since version 2.0
81
     *
82
     * @param array $data Data to save
83
     * @throws Exception Evidence does not support Import
84
     *
85
     * @return array odpověď
86
     */
87
    public function insertToFlexiBee($data = null) {
88
        return $this->insertToAbraFlexi($data);
89
    }
90
91
    /**
92
     * Save record (if evidence allow to).
93
     *
94
     * @param array $data Data to save
95
     * 
96
     * @throws Exception Evidence does not support Import
97
     *
98
     * @return array odpověď
99
     */
100
    public function insertToAbraFlexi($data = null) {
101
        if (is_null($data)) {
102
            $data = $this->getData();
103
        }
104
        $this->postFields = $this->getJsonizedData($data,
105
                $this->debug ? JSON_PRETTY_PRINT : 0);
106
        return $this->performRequest(null, 'PUT');
107
    }
108
109
    /**
110
     * Parse Response array
111
     *
112
     * @param array $responseDecoded
113
     * @param int $responseCode Request Response Code
114
     *
115
     * @return array main data part of response
116
     */
117
    public function parseResponse($responseDecoded, $responseCode) {
118
        $parsedData = parent::parseResponse($responseDecoded, $responseCode);
119
        switch ($responseCode) {
120
            case 201: //Success Write
121
                if (is_array($responseDecoded)) {
0 ignored issues
show
introduced by
The condition is_array($responseDecoded) is always true.
Loading history...
122
                    $this->responseStats = array_key_exists('stats',
123
                                    $responseDecoded) ? (isset($responseDecoded['stats'][0]) ? array_map('intval', $responseDecoded['stats'][0]) : array_map('intval', $responseDecoded['stats'])) : null;
124
                    if (isset($responseDecoded[$this->resultField][0]['id'])) {
125
                        $this->lastInsertedID = $responseDecoded[$this->resultField][0]['id'];
126
                        $this->setMyKey($this->lastInsertedID);
127
                    } else {
128
                        $this->lastInsertedID = null;
129
                    }
130
                    if (count($this->chained)) {
131
                        $this->assignResultIDs($this->extractResultIDs($responseDecoded[$this->resultField]));
132
                    }
133
                }
134
                $parsedData = $responseDecoded['results'];
135
        }
136
        return $parsedData;
137
    }
138
139
    /**
140
     * Parse error message response
141
     *
142
     * @param array $responseDecoded
143
     * 
144
     * @return int number of errors processed
145
     */
146
    public function parseError(array $responseDecoded) {
147
        if (array_key_exists('results', $responseDecoded)) {
148
149
            if (array_key_exists(0, $responseDecoded['results'])) {
150
                foreach ($responseDecoded['results'] as $result) {
151
152
                    if (array_key_exists('request-id', $result)) {
153
                        unset($result['request-id']);
154
                    }
155
156
                    if (array_key_exists('errors', $result)) {
157
                        foreach ($result as $error) {
158
                            $this->errors[] = current($error);
159
                        }
160
                    }
161
                }
162
            } else {
163
                foreach ($responseDecoded['results'][0]['result'] as $result) {
164
                    if (array_key_exists('errors', $result)) {
165
                        foreach ($result as $error) {
166
                            $this->errors[] = current($error);
167
                        }
168
                    }
169
                }
170
            }
171
172
            foreach ($this->errors as $errorInfo) {
173
                $this->addStatusMessage(array_key_exists('error', $errorInfo) ? $errorInfo['error'] : $errorInfo['message'], 'error');
174
                if ($this->debug && array_key_exists('for', $errorInfo)) {
175
                    unset($errorInfo['message']);
176
                    $this->addStatusMessage(json_encode($errorInfo), 'debug');
177
                }
178
            }
179
        } else {
180
            parent::parseError($responseDecoded);
181
        }
182
        return count($this->errors);
183
    }
184
185
    /**
186
     * Assign result IDs to its source objects
187
     * 
188
     * @param array $candidates FlexiBee insert IDs  prepared by extractResultIDs()
189
     */
190
    public function assignResultIDs($candidates) {
191
        foreach ($this->chained as $chid => $chained) {
192
            $chainedEvidence = $chained->getEvidence();
193
            $chainedExtid = $chained->getRecordID();
194
            if (is_array($chainedExtid)) { //if there are more IDs
195
                foreach ($chainedExtid as $extId) { //find external ID in format ext:.....
196
                    if (stripos($extId, 'ext:') === 0) {
197
                        $chainedExtid = $extId;
198
                        break;
199
                    }
200
                }
201
            }
202
            $chained->getData();
203
            if (isset($candidates[$chainedEvidence][$chainedExtid])) {
204
                $chained->setMyKey($candidates[$chainedEvidence][$chainedExtid]);
205
                $chained->setDataValue('external-ids', [$chainedExtid]);
206
            }
207
            if (count($this->chained[$chid]->chained)) {
208
                $this->chained[$chid]->assignResultIDs($candidates);
209
            }
210
        }
211
    }
212
213
    /**
214
     * Extract IDs from FlexiBee response Array
215
     * 
216
     * @param array $resultInfo FlexiBee response
217
     * 
218
     * @return array List of [ 'evidence1'=>[ 'original-id'=>numericID,'original-id2'=>numericID2 ], 'evidence2'=> ... ]
219
     */
220
    public function extractResultIDs($resultInfo) {
221
        $candidates = [];
222
        foreach ($resultInfo as $insertResult) {
223
            $newID = $insertResult['id'];
224
            if (array_key_exists('request-id', $insertResult)) {
225
                $extid = $insertResult['request-id'];
226
            } else {
227
                $extid = null;
228
            }
229
            $evidence = explode('/', $insertResult['ref'])[3];
230
            $candidates[$evidence][$extid] = $newID;
231
        }
232
        return $candidates;
233
    }
234
235
    /**
236
     * Give you last inserted record ID.
237
     * 
238
     * @return int
239
     */
240
    public function getLastInsertedId() {
241
        return $this->lastInsertedID;
242
    }
243
244
    /**
245
     * Delete record in FlexiBee
246
     * 
247
     * @deprecated since version 2.0
248
     *
249
     * @param int|string $id identifikátor záznamu
250
     * 
251
     * @return boolean Response code is 200 ?
252
     */
253
    public function deleteFromFlexiBee($id = null) {
254
        return $this->deleteFromAbraFlexi($id);
255
    }
256
257
    /**
258
     * Delete record in FlexiBee
259
     *
260
     * @param int|string $id identifikátor záznamu
261
     * 
262
     * @return boolean Response code is 200 ?
263
     */
264
    public function deleteFromAbraFlexi($id = null) {
265
        if (is_null($id)) {
266
            $id = $this->getMyKey();
267
        }
268
269
        $this->performRequest($this->getEvidenceUrl() . '/' . self::urlizeId($id),
270
                'DELETE');
271
        return $this->lastResponseCode == 200;
272
    }
273
274
    /**
275
     * Control for existing column names in evidence and take data
276
     *
277
     * @param array $data Data to keep
278
     * 
279
     * @return int number of records taken
280
     */
281
    public function takeData($data) {
282
        if ($this->debug === true) {
283
            $fbRelations = [];
284
            $fbColumns = $this->getColumnsInfo();
285
            foreach ($this->getRelationsInfo() as $relation) {
286
                if (is_array($relation) && isset($relation['url'])) {
287
                    $fbRelations[$relation['url']] = $relation['url'];
288
                }
289
            }
290
            if (count($fbColumns)) {
291
                foreach ($data as $key => $value) {
292
                    if (!array_key_exists($key, $fbColumns)) {
293
294
                        if (!array_key_exists($key, $fbRelations)) {
295
                            $this->addStatusMessage(sprintf('unknown column %s for evidence %s',
296
                                            $key, $this->getEvidence()), 'warning');
297
                        } else {
298
                            if (!is_array($value)) {
299
                                $this->addStatusMessage(sprintf('subevidence %s in evidence %s must bee an array',
300
                                                $key, $this->getEvidence()), 'warning');
301
                            }
302
                        }
303
                    }
304
                }
305
            }
306
        }
307
        return parent::takeData($data);
308
    }
309
310
    /**
311
     * Control data for mandatory columns presence.
312
     *
313
     * @deprecated since version 1.8.7
314
     * 
315
     * @param array $data
316
     * 
317
     * @return array List of missing columns. Empty if all is ok
318
     */
319
    public function controlMandatoryColumns($data = null) {
320
        if (is_null($data)) {
321
            $data = $this->getData();
322
        }
323
        $missingMandatoryColumns = [];
324
        if (!empty($data) && count($data)) {
325
            $fbColumns = $this->getColumnsInfo();
326
            if (count($fbColumns)) {
327
                foreach ($fbColumns as $columnName => $columnInfo) {
328
                    $mandatory = ($columnInfo['mandatory'] == 'true');
329
                    if ($mandatory && !array_key_exists($columnName, $data)) {
330
                        $missingMandatoryColumns[$columnName] = $columnInfo['name'];
331
                    }
332
                }
333
            }
334
        }
335
        return $missingMandatoryColumns;
336
    }
337
338
    /**
339
     * Control data for readonly columns presence.
340
     *
341
     * @param array $data
342
     * 
343
     * @return array List of ReadOnly columns. Empty if all is ok
344
     */
345
    public function controlReadOnlyColumns($data = null) {
346
        if (is_null($data)) {
347
            $data = $this->getData();
348
        }
349
350
        $readonlyColumns = [];
351
352
        $fbColumns = $this->getColumnsInfo();
353
        if (!empty($fbColumns) && count($fbColumns)) {
354
            foreach ($fbColumns as $columnName => $columnInfo) {
355
                $writable = ($columnInfo['isWritable'] == 'true');
356
                if (!$writable && !array_key_exists($columnName, $data)) {
357
                    $readonlyColumns[$columnName] = $columnInfo['name'];
358
                }
359
            }
360
        }
361
        return $readonlyColumns;
362
    }
363
364
    /**
365
     * Convert Timestamp to FlexiBee Date format.
366
     *
367
     * @param int $timpestamp
368
     *
369
     * @return string FlexiBee Date or NULL
370
     */
371
    public static function timestampToFlexiDate($timpestamp = null) {
372
        $flexiDate = null;
373
        if (!is_null($timpestamp)) {
374
            $date = new \DateTime();
375
            $date->setTimestamp($timpestamp);
376
            $flexiDate = $date->format('Y-m-d');
377
        }
378
        return $flexiDate;
379
    }
380
381
    /**
382
     * Convert Timestamp to Flexi DateTime format.
383
     *
384
     * @param int $timpestamp
385
     *
386
     * @return string FlexiBee DateTime or NULL
387
     */
388
    public static function timestampToFlexiDateTime($timpestamp = null) {
389
        $flexiDateTime = null;
390
        if (!is_null($timpestamp)) {
391
            $date = new \DateTime();
392
            $date->setTimestamp($timpestamp);
393
            $flexiDateTime = $date->format('Y-m-dTH:i:s');
394
        }
395
        return $flexiDateTime;
396
    }
397
398
    /**
399
     * Add Data to evidence Branch
400
     * Přidá data do větve
401
     *
402
     * @thanksto Karel Běl
403
     *
404
     * @see Relations
405
     *
406
     * @param array   $data pole dat
407
     * @param string  $relationPath path evidence (relation) pro vkládaná data
408
     * @param boolean $removeAll
409
     *
410
     * @return boolean Operation success
411
     */
412
    public function addArrayToBranch($data, $relationPath = 'polozkyDokladu',
413
            $removeAll = false) {
414
        $currentBranchData = $this->getDataValue($relationPath);
415
        $branchData = $currentBranchData;
416
        $branchData[] = $data;
417
        if (is_array($this->getEvidence()) && array_key_exists('bezPolozek',
0 ignored issues
show
introduced by
The condition is_array($this->getEvidence()) is always false.
Loading history...
418
                        $this->getColumnsInfo())) {
419
            $this->setDataValue('bezPolozek', false);
420
        }
421
        if ($removeAll === true) {
422
            $this->setDataValue($relationPath . '@removeAll', true);
423
        }
424
        return $this->setDataValue($relationPath, $branchData);
425
    }
426
427
    /**
428
     * Vloží do větve data z objektu
429
     *
430
     * @param RO $object    objekt evidence
431
     * @param boolean    $removeAll flush older items 
432
     */
433
    public function addObjectToBranch($object, $removeAll = false) {
434
        $this->addArrayToBranch([$object->getEvidence() => $object->getData()],
435
                'polozkyDokladu', $removeAll);
436
    }
437
438
    /**
439
     * Přidá uživatelskou vazbu
440
     *
441
     * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/
442
     * @param string $vazba
443
     */
444
    public function vazbaAdd($vazba) {
445
        $this->addArrayToBranch(['uzivatelska-vazba' => $vazba],
446
                'uzivatelske-vazby');
447
    }
448
449
    /**
450
     * Smaže uživatelskou vazbu
451
     *
452
     * @see https://www.flexibee.eu/api/dokumentace/ref/uzivatelske-vazby/
453
     * @param string $vazba
454
     */
455
    public function vazbaDel($vazba) {
456
        $this->setDataValue('uzivatelska-vazba@action', 'delete');
457
        $this->addArrayToBranch(['uzivatelska-vazba' => $vazba],
458
                'uzivatelske-vazby');
459
    }
460
461
    /**
462
     * Převede data do Json formátu pro FlexiBee.
463
     * Pokud jsou štítky pole, jsou převedeny na seznam oddělený čárkou.
464
     * Convert data to FlexiBee like Json format.
465
     * Array of Labels is converted to coma separated list
466
     *
467
     * @param array $data
468
     * @param int   $options json_encode options like JSON_PRETTY_PRINT etc 
469
     *
470
     * @return string
471
     */
472
    public function getJsonizedData($data = null, $options = 0) {
473
        if (is_null($data)) {
474
            $data = $this->getData();
475
        }
476
477
        if (array_key_exists('stitky', $data)) {
478
            if (is_array($data['stitky'])) {
479
                $data['stitky'] = implode(',', $data['stitky']);
480
            }
481
        }
482
        $dataToJsonize = parent::getJsonizedData($data, $options);
483
        return $dataToJsonize;
484
    }
485
486
    /**
487
     * Get Data Fragment specific for current object
488
     * 
489
     * @param array $data
490
     * 
491
     * @return array
492
     */
493
    public function getDataForJSON($data = null) {
494
        if (is_null($data)) {
495
            $data = $this->getData();
496
        }
497
        $dataForJSON = parent::getDataForJSON($data);
498
        if (!is_null($this->atomic)) {
0 ignored issues
show
introduced by
The condition is_null($this->atomic) is always false.
Loading history...
499
            $dataForJSON['@atomic'] = $this->atomic;
500
        }
501
        if (isset($this->sourceId)) {
502
            $dataForJSON['@sourceId'] = $this->sourceId;
503
            $this->sourceId = null;
504
        }
505
        return $dataForJSON;
506
    }
507
508
    /**
509
     * Insert current data into FlexiBee and load actual record data back
510
     *
511
     * @param array $data Initial data to save
512
     * 
513
     * @return boolean Operation success
514
     */
515
    public function sync($data = null) {
516
        $this->insertToAbraFlexi($data);
517
        $insertResult = $this->lastResponseCode;
518
        if ($insertResult == 201) {
519
            $this->reload();
520
        }
521
        $loadResult = $this->lastResponseCode;
522
        return ($insertResult + $loadResult) == 401;
523
    }
524
525
    /**
526
     * Make Copy of given record with optional modifiactions
527
     * 
528
     * !!!Experimental Feature!!!
529
     * 
530
     * @param int  $source
531
     * @param array $overrides
532
     * 
533
     * @return RW|null copied record object or null in case of failure
534
     */
535
    public function copy($source, $overrides = []) {
536
        $this->sourceId = $source;
537
        return $this->sync($overrides) ? $this : null;
538
    }
539
540
    /**
541
     * Perform given action (if availble) on current evidence/record
542
     * @url https://demo.flexibee.eu/devdoc/actions
543
     *
544
     * @param string $action one of evidence actions
545
     * @param string $method ext|int External method call operation in URL.
546
     *                               Internal add the @action element to request body
547
     *
548
     * @return boolean operation success
549
     */
550
    public function performAction($action, $method = 'int') {
551
        $actionsAvailble = $this->getActionsInfo();
552
553
        if (is_array($actionsAvailble) && array_key_exists($action,
554
                        $actionsAvailble)) {
555
            switch ($actionsAvailble[$action]['actionMakesSense']) {
556
                case 'ONLY_WITH_INSTANCE_AND_NOT_IN_EDIT':
557
                case 'ONLY_WITH_INSTANCE': //Add instance
558
                    $urlSuffix = '/' . $this->__toString() . '/' . $action;
559
                    break;
560
561
                default:
562
                    $urlSuffix = '/' . $action;
563
                    break;
564
            }
565
566
            switch ($method) {
567
                case 'int':
568
                    $this->setAction($action);
569
                    $this->setPostFields($this->getJsonizedData(['id' => $this]));
570
                    $this->performRequest(null, 'POST');
571
                    $result = $this->lastResponseCode == 201;
572
                    break;
573
574
                default:
575
                    $result = $this->performRequest($this->evidenceUrlWithSuffix($urlSuffix),
576
                            'GET');
577
                    break;
578
            }
579
        } else {
580
            throw new \Exception(sprintf(_('Unsupported action %s for evidence %s'),
581
                                    $action, $this->getEvidence()));
582
        }
583
584
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result also could return the type array which is incompatible with the documented return type boolean.
Loading history...
585
    }
586
587
    /**
588
     * Add External ID to Current Record
589
     * 
590
     * @param string $extId ext:whatever:123 or simplay whatever:123
591
     * 
592
     * @return array Insert result
593
     */
594
    public function addExternalID($extId) {
595
        return $this->insertToAbraFlexi(['id' => [$this->getRecordID(), 'ext:' . preg_replace('/^ext:/',
596
                                '', $extId)]]);
597
    }
598
599
    /**
600
     * Change Value of external id identified by selector. Add new if not exists
601
     * 
602
     * @param string     $selector ext:$selector:$newValue
603
     * @param string|int $newValue string or number
604
     * @param string|int $forID    Other than current record id
605
     * 
606
     * @return array operation result
607
     */
608
    public function changeExternalID($selector, $newValue, $forID = null) {
609
        $change['@removeExternalIds'] = 'ext:' . $selector . ':';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$change was never initialized. Although not strictly required by PHP, it is generally a good practice to add $change = array(); before regardless.
Loading history...
610
        $change['id'] = [is_null($forID) ? $this->getRecordID() : $forID,
611
            'ext:' . $selector . ':' . $newValue];
612
        return $this->insertToAbraFlexi($change);
613
    }
614
615
    /**
616
     * Send all unsent Documents by eMail
617
     *
618
     * @url https://www.flexibee.eu/api/dokumentace/ref/odesilani-mailem/
619
     * 
620
     * @return int http response code
621
     */
622
    public function sendUnsent() {
623
        $this->performRequest('automaticky-odeslat-neodeslane', 'PUT', 'xml');
624
        return $this->lastResponseCode == 202;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->lastResponseCode == 202 returns the type boolean which is incompatible with the documented return type integer.
Loading history...
625
    }
626
627
}
628