Completed
Push — master ( 9b3c4b...a03cda )
by Vítězslav
08:45
created

FlexiBeeRW::getLastInsertedId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 'DISALLOWED':
55
                throw new Exception(sprintf('Inserting data to r/o evidence %s',
56
                    $this->getEvidence()));
57
            case 'NOT_DOCUMENTED':
58
                if ($this->debug) {
59
                    $this->addStatusMessage(sprintf('Inserting data to undocumneted evidence %s',
60
                            $this->getEvidence()));
61
                }
62
63
                break;
64
            case 'SUPPORTED':
65
            default:
66
                break;
67
        }
68
69
        if (is_null($data)) {
70
            $data = $this->getData();
71
        }
72
        $this->postFields = $this->jsonizeData($data);
73
        return $this->performRequest($this->evidence.'.'.$this->format, 'PUT');
74
    }
75
76
    /**
77
     * Give you last inserted record ID.
78
     * 
79
     * @return int
80
     */
81
    public function getLastInsertedId()
82
    {
83
        return $this->lastInsertedID;
84
    }
85
86
    /**
87
     * Smaže záznam
88
     *
89
     * @param int|string $id identifikátor záznamu
90
     * @return boolean Response code is 200 ?
91
     */
92
    public function deleteFromFlexiBee($id = null)
93
    {
94
        if (is_null($id)) {
95
            $id = $this->getMyKey();
96
        }
97
        $this->performRequest($this->evidence.'/'.$id.'.'.$this->format,
98
            'DELETE');
99
        return $this->lastResponseCode == 200;
100
    }
101
102
    /**
103
     * Control for existing column names in evidence and take data
104
     *
105
     * @param array $data Data to keep
106
     * @return int number of records taken
107
     * @throws \Exception try to load data to unexistent column
108
     */
109
    public function takeData($data)
110
    {
111
        $fbColumns = $this->getColumnsInfo();
112
        foreach ($data as $key => $value) {
113
            if (!array_key_exists($key, $fbColumns)) {
114
                throw new \Exception(sprintf('unknown column %s for evidence %s',
115
                    $key, $this->getEvidence()));
116
            }
117
        }
118
119
        return parent::takeData($data);
120
    }
121
122
    /**
123
     * Control data for mandatory columns presence.
124
     *
125
     * @param array $data
126
     * @return array List of missing columns. Empty if all is ok
127
     */
128
    public function controlMandatoryColumns($data = null)
129
    {
130
        if (is_null($data)) {
131
            $data->getData();
0 ignored issues
show
Bug introduced by
The method getData cannot be called on $data (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
132
        }
133
134
        $missingMandatoryColumns = [];
135
136
        $fbColumns = $this->getColumnsInfo();
137
        foreach ($fbColumns as $columnName => $columnInfo) {
138
            $mandatory = ($columnInfo['mandatory'] == 'true');
139
            if ($mandatory && !array_key_exists($columnName, $data)) {
140
                $missingMandatoryColumns[$columnName] = $columnInfo['name'];
141
            }
142
        }
143
144
        return $missingMandatoryColumns;
145
    }
146
147
    /**
148
     * Convert Timestamp to FlexiBee Date format.
149
     *
150
     * @param int $timpestamp
151
     *
152
     * @return string FlexiBee Date or NULL
153
     */
154 View Code Duplication
    public static function timestampToFlexiDate($timpestamp = 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...
155
    {
156
        $flexiDate = null;
157
        if (!is_null($timpestamp)) {
158
            $date      = new \DateTime();
159
            $date->setTimestamp($timpestamp);
160
            $flexiDate = $date->format('Y-m-d');
161
        }
162
        return $flexiDate;
163
    }
164
165
    /**
166
     * Convert Timestamp to Flexi DateTime format.
167
     *
168
     * @param int $timpestamp
169
     *
170
     * @return string FlexiBee DateTime or NULL
171
     */
172 View Code Duplication
    public static function timestampToFlexiDateTime($timpestamp = 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...
173
    {
174
        $flexiDateTime = null;
175
        if (!is_null($timpestamp)) {
176
            $date          = new \DateTime();
177
            $date->setTimestamp($timpestamp);
178
            $flexiDateTime = $date->format('Y-m-dTH:i:s');
179
        }
180
        return $flexiDateTime;
181
    }
182
183
}
184