Test Failed
Push — master ( 2514b0...1bd162 )
by Vítězslav
02:52
created

Company::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
ccs 1
cts 1
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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 $myKeyColumn = 'dbNazev';
51
52
    /**
53
     * Class for read only interaction with FlexiBee.
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 (count($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;
103
        if (!empty($urlSuffix)) {
104
            $url .= (($urlSuffix[0] == '.') ? '' : '/').$urlSuffix;
105
        }
106
        return $url;
107
    }
108
109
    /**
110
     * Vrací název evidence použité v odpovědích z FlexiBee
111
     *
112
     * @return string
113
     */
114
    public function getResponseEvidence()
115
    {
116
        return 'company';
117
    }
118
119
    /**
120
     * Parse Raw FlexiBee response in several formats
121
     *
122
     * @param string $responseRaw raw response body
123
     * @param string $format      Raw Response format json|xml|etc
124
     *
125
     * @return array
126
     */
127
    public function rawResponseToArray($responseRaw, $format)
128
    {
129
        if (strstr($responseRaw, 'winstrom')) {
130
            $nsbackup        = $this->nameSpace;
131
            $this->nameSpace = 'winstrom';
132
            $response        = parent::rawResponseToArray($responseRaw, $format);
133
            $this->nameSpace = $nsbackup;
134
        } else {
135
            $response = parent::rawResponseToArray($responseRaw, $format);
136
        }
137
        return $response;
138
    }
139
140
    /**
141
     * Save company backup to file
142
     *
143
     * @param string $filename
144
     *
145
     * @return boolean was backup saved to file ?
146
     */
147
    public function saveBackupTo($filename)
148
    {
149
        $result                                   = false;
150
        $headersBackup                            = $this->defaultHttpHeaders;
151
        $this->defaultHttpHeaders['Accept']       = '*/*';
152
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
153
        $this->performRequest($this->getDataValue('dbNazev').'/backup', 'GET');
154
        $this->defaultHttpHeaders                 = $headersBackup;
155
156
        if ($this->lastResponseCode == 200) {
157
            if (file_put_contents($filename, $this->lastCurlResponse)) {
158
                $result = true;
159
            }
160
        }
161
        return $result;
162
    }
163
164
    /**
165
     * Restore company from given file
166
     *
167
     * @param string $filename
168
     *
169
     * @return boolean result
170
     */
171
    public function restoreBackupFrom($filename)
172
    {
173
        $result                                   = false;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
174
        $headersBackup                            = $this->defaultHttpHeaders;
0 ignored issues
show
Unused Code introduced by
$headersBackup is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
        $this->defaultHttpHeaders['Accept']       = '*/*';
176
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
177
        $this->setPostFields(file_get_contents($filename));
178
        $this->performRequest($this->getDataValue('dbNazev').'/restore', 'PUT');
179
        return $this->lastResponseCode == 200;
180
    }
181
182
    /**
183
     * Create new company
184
     *
185 3
     * @param string $name
186
     *
187 3
     * @return boolean
188
     */
189
    public function createNew($name)
190
    {
191
        $this->performRequest('/admin/zalozeni-firmy?name='.$name, 'PUT');
192
        return $this->lastResponseCode == 201;
193
    }
194
195
    /**
196
     * Obtain company identifier
197
     *
198
     * @return string company database name
199
     */
200
    public function getRecordID()
201
    {
202
        return $this->getDataValue('dbNazev');
203
    }
204
205
    /**
206
     * Company has no relations
207
     *
208
     * @return null
209
     */
210
    public function getVazby($id = null)
211
    {
212
        throw new \Exception(_('Company has no relations'));
213
    }
214
}
215