Passed
Push — master ( 38b53e...3a86a3 )
by Vítězslav
02:01
created

Company::getApiURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * FlexiPeeHP - Objekt Společnosti.
4
 *
5
 * @author     Vítězslav Dvořák <[email protected]>
6
 * @copyright  (C) 2015-2017 Spoje.Net
7
 */
8
9
namespace FlexiPeeHP;
10
11
/**
12
 * Firmy/účetní jednotky
13
 *
14
 * @note Tato položka nemá dostupné položky evidence
15
 */
16
class Company extends FlexiBeeRW
17
{
18
    /**
19
     * Základní namespace pro komunikaci s FlexiBEE.
20
     *
21
     * @var string Jmený prostor datového bloku odpovědi
22
     */
23
    public $nameSpace = 'companies';
24
25
    /**
26
     * Default Line Prefix.
27
     *
28
     * @var string
29
     */
30
    public $prefix = '/c';
31
32
    /**
33
     * Company.
34
     *
35
     * @var string
36
     */
37
    public $evidence = '';
38
39
    /**
40
     * Tato třída nepracuje sezvolenou firmou.
41
     *
42
     * @var string
43
     */
44
    public $company = '';
45
46
    /**
47
     * Key Column for this evidence
48
     * @var string
49
     */
50
    public $keyColumn = 'dbNazev';
51
52
    /**
53
     * FlexiBee Company Class .
54
     *
55
     * @param string|array $init    company dbNazev or initial data
56
     * @param array        $options Connection settings override
57
     */
58
    public function __construct($init = null, $options = [])
59
    {
60
        if (is_string($init)) {
61
            $init = ['dbNazev' => $init];
62
        }
63
        parent::__construct($init, $options);
64 1
    }
65
66 1
    /**
67 1
     * Zinicializuje objekt dle daných dat. Možné hodnoty:
68 1
     *
69 1
     *  * ['dbNazev'=>'company']           - load company form FlexiBee
70 1
     *  * 234                              - interní číslo záznamu k načtení
71 1
     *  * code:LOPATA                      - kód záznamu
72 1
     *  * BAGR                             - kód záznamu k načtení
73 1
     *  * ['id'=>24,'nazev'=>'hoblík']     - pole hodnot k předvyplnění
74
     *  * 743.json?relations=adresa,vazby  - část url s parametry k načtení
75
     *
76
     * @param mixed $init číslo/"(code:)kód"/(část)URI záznamu k načtení | pole hodnot k předvyplnění
77
     */
78
    public function processInit($init)
79
    {
80
81 2
        parent::processInit($init);
82
        if (is_array($init) && array_key_exists('dbNazev', $init)) {
83 2
            $companyInfo = $this->getFlexiData('/c/'.$init['dbNazev']);
84 2
            if(!array_key_exists('message', $companyInfo)){
85 2
                $this->takeData(current($companyInfo));
86
            }
87 2
        }
88 2
    }
89
90
    /**
91 2
     * Vrací základní URL pro užitou evidenci
92
     *
93
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
94
     * @param string $urlSuffix
95
     */
96
    public function getEvidenceURL($urlSuffix = null)
97
    {
98
        if (is_null($urlSuffix)) {
99 1
            $urlSuffix = $this->evidence;
100
        }
101 1
102
        $url = $this->url.$this->prefix.'/'.$this->company;
103
        if (!empty($urlSuffix)) {
104
            $url .= (($urlSuffix[0] == '.') ? '' : '/').$urlSuffix;
105
        }
106
        return $url;
107
    }
108
109
    /**
110
     * Gives you current ApiURL with given format suffix
111
     * 
112
     * @param string $format json|html|xml|...
113
     * 
114
     * @return string API URL for current record or object/evidence
115
     */
116
    public function getApiURL($format = null)
117
    {
118
        return dirname(parent::getApiURL($format));
119
    }
120
121
    /**
122
     * Vrací název evidence použité v odpovědích z FlexiBee
123
     *
124
     * @return string
125
     */
126
    public function getResponseEvidence()
127
    {
128
        return 'company';
129
    }
130
131
    /**
132
     * Parse Raw FlexiBee response in several formats
133
     *
134
     * @param string $responseRaw raw response body
135
     * @param string $format      Raw Response format json|xml|etc
136
     *
137
     * @return array
138
     */
139
    public function rawResponseToArray($responseRaw, $format)
140
    {
141
        if (strstr($responseRaw, 'winstrom')) {
142
            $nsbackup        = $this->nameSpace;
143
            $this->nameSpace = 'winstrom';
144
            $response        = parent::rawResponseToArray($responseRaw, $format);
145
            $this->nameSpace = $nsbackup;
146
        } else {
147
            $response = parent::rawResponseToArray($responseRaw, $format);
148
        }
149
        return $response;
150
    }
151
152
    /**
153
     * Save company backup to file
154
     *
155
     * @param string $filename
156
     *
157
     * @return boolean was backup saved to file ?
158
     */
159
    public function saveBackupTo($filename)
160
    {
161
        $result                                   = false;
162
        $headersBackup                            = $this->defaultHttpHeaders;
163
        $this->defaultHttpHeaders['Accept']       = '*/*';
164
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
165
        $this->performRequest('backup', 'GET');
166
        $this->defaultHttpHeaders                 = $headersBackup;
167
168
        if ($this->lastResponseCode == 200) {
169
            if (file_put_contents($filename, $this->lastCurlResponse)) {
170
                $result = true;
171
            }
172
        }
173
        $this->defaultHttpHeaders = $headersBackup;
174
        return $result;
175
    }
176
177
    /**
178
     * Restore company from given file
179
     *
180
     * @param string $filename
181
     *
182
     * @return boolean result
183
     */
184
    public function restoreBackupFrom($filename)
185 3
    {
186
        $headersBackup                            = $this->defaultHttpHeaders;
0 ignored issues
show
Unused Code introduced by
The assignment to $headersBackup is dead and can be removed.
Loading history...
187 3
        $this->defaultHttpHeaders['Accept']       = '*/*';
188
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
189
        $this->setPostFields(file_get_contents($filename));
190
        $this->performRequest('restore', 'PUT');
191
        return $this->lastResponseCode == 200;
192
    }
193
194
    /**
195
     * Create new company
196
     *
197
     * @param string $name
198
     *
199
     * @return boolean
200
     */
201
    public function createNew($name)
202
    {
203
        $this->performRequest('/admin/zalozeni-firmy?name='.urlencode($name),
204
            'PUT');
205
        return $this->lastResponseCode == 201;
206
    }
207
208
    /**
209
     * Obtain company identifier
210
     *
211
     * @return string company database name
212
     */
213
    public function getRecordID()
214
    {
215
        return $this->getDataValue('dbNazev');
216
    }
217
218
    /**
219
     * Company has no relations
220
     *
221
     * @return null
222
     */
223
    public function getVazby($id = null)
224
    {
225
        throw new \Exception(_('Company has no relations'));
226
    }
227
228
    /**
229
     * Smaže firmu ve FlexiBee
230
     * Delete  company in FlexiBee
231
     *
232
     * @param string $company identifikátor záznamu
233
     * 
234
     * @return boolean Response code is 200 ?
235
     */
236
    public function deleteFromFlexiBee($company = null)
237
    {
238
        if (is_null($company)) {
239
            $company = $this->getDataValue('dbNazev');
240
        }
241
        $this->performRequest('/c/'.$company.'.'.$this->format, 'DELETE');
242
        return $this->lastResponseCode == 200;
243
    }
244
}
245