Completed
Push — master ( 1ef83c...516478 )
by Vítězslav
13:51
created

FlexiBeeRW::takeData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * FlexiPeeHP - Třída pro zápis do FlexiBee.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2015,2016 Spoje.Net
7
 */
8
9
namespace FlexiPeeHP;
10
11
class FlexiBeeRW extends FlexiBeeRO
12
{
13
    /**
14
     * Sloupeček obsahující datum vložení záznamu do shopu.
15
     *
16
     * @var string
17
     */
18
    public $myCreateColumn = 'false';
19
20
    /**
21
     * Slopecek obsahujici datum poslení modifikace záznamu do shopu.
22
     *
23
     * @var string
24
     */
25
    public $myLastModifiedColumn = 'lastUpdate';
26
27
    /**
28
     * Last Inserted ID.
29
     *
30
     * @var int
31
     */
32
    public $lastInsertedID = null;
33
34
    /**
35
     * Array of fields for next curl POST operation
36
     * 
37
     * @var string
38
     */
39
    public $postFields = null;
40
41
    /**
42
     * Save record (if evidence allow to).
43
     * Uloží záznam (pokud to evidence dovoluje)
44
     *
45
     * @param array $data Data to save
46
     * @throws Exception Evidence does not support Import
47
     *
48
     * @return array odpověď
49
     */
50
    public function insertToFlexiBee($data = null)
51
    {
52
        $info = $this->getEvidenceInfo();
53
        switch ($info['importStatus']) {
54
            case 'NOT_DOCUMENTED':
55
                $this->addStatusMessage(sprintf('Inserting data to undocumneted evidence %s',
56
                        $this->getEvidence()));
57
58
                break;
59
            case 'DISALLOWED':
60
                throw new Exception(sprintf('Inserting data to r/o evidence %s',
61
                    $this->getEvidence()));
62
            case 'SUPPORTED':
63
            default:
64
                break;
65
        }
66
67
        if (is_null($data)) {
68
            $data = $this->getData();
69
        }
70
        $this->postFields = $this->jsonizeData($data);
71
        return $this->performRequest($this->evidence.'.'.$this->format, 'PUT');
72
    }
73
74
    /**
75
     * Give you last inserted record ID.
76
     * 
77
     * @return int
78
     */
79
    public function getLastInsertedId()
80
    {
81
        return $this->lastInsertedID;
82
    }
83
84
    /**
85
     * Smaže záznam
86
     *
87
     * @param int|string $id identifikátor záznamu
88
     * @return boolean Response code is 200 ?
89
     */
90
    public function deleteFromFlexiBee($id = null)
91
    {
92
        if (is_null($id)) {
93
            $id = $this->getMyKey();
94
        }
95
        $this->performRequest($this->evidence.'/'.$id.'.'.$this->format,
96
            'DELETE');
97
        return $this->lastResponseCode == 200;
98
    }
99
100
    /**
101
     * Control for existing column names in evidence and take data
102
     *
103
     * @param array $data Data to keep
104
     * @return int number of records taken
105
     * @throws \Exception try to load data to unexistent column
106
     */
107
    public function takeData($data)
108
    {
109
        $fbColumns = $this->getColumnsInfo();
110
        foreach ($data as $key => $value) {
111
            if (!array_key_exists($key, $fbColumns)) {
112
                throw new \Exception(sprintf('unknown column %s for evidence %s',
113
                    $key, $this->getEvidence()));
114
            }
115
        }
116
117
        return parent::takeData($data);
118
    }
119
120
}
121