Completed
Push — master ( a03cda...e4da97 )
by Vítězslav
08:44
created

FlexiBeeRW::timestampToFlexiDateTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 10
loc 10
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
/**
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) {
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
        $this->postFields = $this->jsonizeData($data);
78
        return $this->performRequest($this->evidence.'.'.$this->format, 'PUT');
79
    }
80
81
    /**
82
     * Give you last inserted record ID.
83
     * 
84
     * @return int
85
     */
86
    public function getLastInsertedId()
87
    {
88
        return $this->lastInsertedID;
89
    }
90
91
    /**
92
     * Smaže záznam
93
     *
94
     * @param int|string $id identifikátor záznamu
95
     * @return boolean Response code is 200 ?
96
     */
97
    public function deleteFromFlexiBee($id = null)
98
    {
99
        if (is_null($id)) {
100
            $id = $this->getMyKey();
101
        }
102
        $this->performRequest($this->evidence.'/'.$id.'.'.$this->format,
103
            'DELETE');
104
        return $this->lastResponseCode == 200;
105
    }
106
107
    /**
108
     * Control for existing column names in evidence and take data
109
     *
110
     * @param array $data Data to keep
111
     * @return int number of records taken
112
     * @throws \Exception try to load data to unexistent column
113
     */
114
    public function takeData($data)
115
    {
116
        $fbColumns = $this->getColumnsInfo();
117
        if (count($fbColumns)) {
118
            foreach ($data as $key => $value) {
119
                if (!array_key_exists($key, $fbColumns)) {
120
                    throw new \Exception(sprintf('unknown column %s for evidence %s',
121
                        $key, $this->getEvidence()));
122
                }
123
            }
124
        }
125
        return parent::takeData($data);
126
    }
127
128
    /**
129
     * Control data for mandatory columns presence.
130
     *
131
     * @param array $data
132
     * @return array List of missing columns. Empty if all is ok
133
     */
134 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...
135
    {
136
        if (is_null($data)) {
137
            $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...
138
        }
139
140
        $missingMandatoryColumns = [];
141
142
        $fbColumns = $this->getColumnsInfo();
143
        foreach ($fbColumns as $columnName => $columnInfo) {
144
            $mandatory = ($columnInfo['mandatory'] == 'true');
145
            if ($mandatory && !array_key_exists($columnName, $data)) {
146
                $missingMandatoryColumns[$columnName] = $columnInfo['name'];
147
            }
148
        }
149
150
        return $missingMandatoryColumns;
151
    }
152
153
    /**
154
     * Control data for readonly columns presence.
155
     *
156
     * @param array $data
157
     * @return array List of ReadOnly columns. Empty if all is ok
158
     */
159 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...
160
    {
161
        if (is_null($data)) {
162
            $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...
163
        }
164
165
        $readonlyColumns = [];
166
167
        $fbColumns = $this->getColumnsInfo();
168
        foreach ($fbColumns as $columnName => $columnInfo) {
169
            $writable = ($columnInfo['isWritable'] == 'true');
170
            if (!$writable && !array_key_exists($columnName, $data)) {
171
                $readonlyColumns[$columnName] = $columnInfo['name'];
172
            }
173
        }
174
175
        return $readonlyColumns;
176
    }
177
178
    /**
179
     * Convert Timestamp to FlexiBee Date format.
180
     *
181
     * @param int $timpestamp
182
     *
183
     * @return string FlexiBee Date or NULL
184
     */
185 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...
186
    {
187
        $flexiDate = null;
188
        if (!is_null($timpestamp)) {
189
            $date      = new \DateTime();
190
            $date->setTimestamp($timpestamp);
191
            $flexiDate = $date->format('Y-m-d');
192
        }
193
        return $flexiDate;
194
    }
195
196
    /**
197
     * Convert Timestamp to Flexi DateTime format.
198
     *
199
     * @param int $timpestamp
200
     *
201
     * @return string FlexiBee DateTime or NULL
202
     */
203 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...
204
    {
205
        $flexiDateTime = null;
206
        if (!is_null($timpestamp)) {
207
            $date          = new \DateTime();
208
            $date->setTimestamp($timpestamp);
209
            $flexiDateTime = $date->format('Y-m-dTH:i:s');
210
        }
211
        return $flexiDateTime;
212
    }
213
}
214