Completed
Push — master ( 0a83bf...cdd0e8 )
by Vítězslav
09:35
created

FlexiBeeRW::insertToFlexiBee()   C

Complexity

Conditions 8
Paths 25

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 24
nc 25
nop 1
dl 0
loc 33
rs 5.3846
c 2
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
/**
12
 * Základní třída pro zápis do FlexiBee
13
 *
14
 * @url https://demo.flexibee.eu/devdoc/http-operations
15
 */
16
class FlexiBeeRW extends FlexiBeeRO
17
{
18
    /**
19
     * Sloupeček obsahující datum vložení záznamu do shopu.
20
     *
21
     * @var string
22
     */
23
    public $myCreateColumn = 'false';
24
25
    /**
26
     * Slopecek obsahujici datum poslení modifikace záznamu do shopu.
27
     *
28
     * @var string
29
     */
30
    public $myLastModifiedColumn = 'lastUpdate';
31
32
    /**
33
     * Last Inserted ID.
34
     *
35
     * @var int
36
     */
37
    public $lastInsertedID = null;
38
39
    /**
40
     * Array of fields for next curl POST operation
41
     * 
42
     * @var string
43
     */
44
    public $postFields = null;
45
46
    /**
47
     * Save record (if evidence allow to).
48
     * Uloží záznam (pokud to evidence dovoluje)
49
     *
50
     * @param array $data Data to save
51
     * @throws Exception Evidence does not support Import
52
     *
53
     * @return array odpověď
54
     */
55
    public function insertToFlexiBee($data = null)
56
    {
57
        $info = $this->getEvidenceInfo();
58
        switch ($info['importStatus']) {
59
            case 'DISALLOWED':
60
                throw new \Exception(sprintf('Inserting data to r/o evidence %s',
61
                    $this->getEvidence()));
62
            case 'NOT_DOCUMENTED':
63
                if ($this->debug === true) {
64
                    $this->addStatusMessage(sprintf('Inserting data to undocumneted evidence %s',
65
                            $this->getEvidence()));
66
                }
67
68
                break;
69
            case 'SUPPORTED':
70
            default:
71
                break;
72
        }
73
74
        if (is_null($data)) {
75
            $data = $this->getData();
76
        }
77
        if ($this->debug === true) {
78
            $missingColumns = $this->controlMandatoryColumns();
79
            if (count($missingColumns)) {
80
                $this->addStatusMessage(sprintf('Given data does not contain requied Columns: %s',
81
                        implode(',', $missingColumns))
82
                    , 'warning');
83
            }
84
        }
85
        $this->postFields = $this->jsonizeData($data);
86
        return $this->performRequest($this->evidence.'.'.$this->format, 'PUT');
87
    }
88
89
    /**
90
     * Give you last inserted record ID.
91
     * 
92
     * @return int
93
     */
94
    public function getLastInsertedId()
95
    {
96
        return $this->lastInsertedID;
97
    }
98
99
    /**
100
     * Smaže záznam
101
     * Delete record in FlexiBee
102
     *
103
     * @param int|string $id identifikátor záznamu
104
     * @return boolean Response code is 200 ?
105
     */
106
    public function deleteFromFlexiBee($id = null)
107
    {
108
        if (is_null($id)) {
109
            $id = $this->getMyKey();
110
        }
111
        $this->performRequest($this->evidence.'/'.$id.'.'.$this->format,
112
            'DELETE');
113
        return $this->lastResponseCode == 200;
114
    }
115
116
    /**
117
     * Control for existing column names in evidence and take data
118
     *
119
     * @param array $data Data to keep
120
     * @return int number of records taken
121
     */
122
    public function takeData($data)
123
    {
124
        if ($this->debug === true) {
125
            $fbColumns = $this->getColumnsInfo();
126
            if (count($fbColumns)) {
127
                foreach ($data as $key => $value) {
128
                    if (!array_key_exists($key, $fbColumns)) {
129
                        $this->addStatusMessage(sprintf('unknown column %s for evidence %s',
130
                                $key, $this->getEvidence()), 'warning');
131
                    }
132
                }
133
            }
134
        }
135
        return parent::takeData($data);
136
    }
137
138
    /**
139
     * Control data for mandatory columns presence.
140
     *
141
     * @param array $data
142
     * @return array List of missing columns. Empty if all is ok
143
     */
144 View Code Duplication
    public function controlMandatoryColumns($data = 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...
145
    {
146
        if (is_null($data)) {
147
            $data = $this->getData();
148
        }
149
150
        $missingMandatoryColumns = [];
151
152
        $fbColumns = $this->getColumnsInfo();
153
        foreach ($fbColumns as $columnName => $columnInfo) {
154
            $mandatory = ($columnInfo['mandatory'] == 'true');
155
            if ($mandatory && !array_key_exists($columnName, $data)) {
156
                $missingMandatoryColumns[$columnName] = $columnInfo['name'];
157
            }
158
        }
159
160
        return $missingMandatoryColumns;
161
    }
162
163
    /**
164
     * Control data for readonly columns presence.
165
     *
166
     * @param array $data
167
     * @return array List of ReadOnly columns. Empty if all is ok
168
     */
169 View Code Duplication
    public function controlReadOnlyColumns($data = 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...
170
    {
171
        if (is_null($data)) {
172
            $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...
173
        }
174
175
        $readonlyColumns = [];
176
177
        $fbColumns = $this->getColumnsInfo();
178
        foreach ($fbColumns as $columnName => $columnInfo) {
179
            $writable = ($columnInfo['isWritable'] == 'true');
180
            if (!$writable && !array_key_exists($columnName, $data)) {
181
                $readonlyColumns[$columnName] = $columnInfo['name'];
182
            }
183
        }
184
185
        return $readonlyColumns;
186
    }
187
188
    /**
189
     * Convert Timestamp to FlexiBee Date format.
190
     *
191
     * @param int $timpestamp
192
     *
193
     * @return string FlexiBee Date or NULL
194
     */
195 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...
196
    {
197
        $flexiDate = null;
198
        if (!is_null($timpestamp)) {
199
            $date      = new \DateTime();
200
            $date->setTimestamp($timpestamp);
201
            $flexiDate = $date->format('Y-m-d');
202
        }
203
        return $flexiDate;
204
    }
205
206
    /**
207
     * Convert Timestamp to Flexi DateTime format.
208
     *
209
     * @param int $timpestamp
210
     *
211
     * @return string FlexiBee DateTime or NULL
212
     */
213 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...
214
    {
215
        $flexiDateTime = null;
216
        if (!is_null($timpestamp)) {
217
            $date          = new \DateTime();
218
            $date->setTimestamp($timpestamp);
219
            $flexiDateTime = $date->format('Y-m-dTH:i:s');
220
        }
221
        return $flexiDateTime;
222
    }
223
224
}
225