Test Failed
Push — master ( 99a915...bca16c )
by Vítězslav
07:03
created

Company::getRecordID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

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
ccs 2
cts 2
cp 1
crap 1
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
     * Zinicializuje objekt dle daných dat. Možné hodnoty:
54
     *
55
     *  * ['dbNazev'=>'company']           - load company form FlexiBee
56
     *  * 234                              - interní číslo záznamu k načtení
57
     *  * code:LOPATA                      - kód záznamu
58
     *  * BAGR                             - kód záznamu k načtení
59
     *  * ['id'=>24,'nazev'=>'hoblík']     - pole hodnot k předvyplnění
60
     *  * 743.json?relations=adresa,vazby  - část url s parametry k načtení
61
     *
62
     * @param mixed $init číslo/"(code:)kód"/(část)URI záznamu k načtení | pole hodnot k předvyplnění
63
     */
64 1
    public function processInit($init)
65
    {
66 1
        parent::processInit($init);
67 1
        if (is_array($init) && array_key_exists('dbNazev', $init)) {
68 1
            $companyInfo = $this->getFlexiData('/c/'.$init['dbNazev']);
69 1
            if (count($companyInfo)) {
70 1
                $this->takeData(current($companyInfo));
71 1
            }
72 1
        }
73 1
    }
74
75
    /**
76
     * Vrací základní URL pro užitou evidenci
77
     *
78
     * @link https://www.flexibee.eu/api/dokumentace/ref/urls/ Sestavování URL
79
     * @param string $urlSuffix
80
     */
81 2
    public function getEvidenceURL($urlSuffix = null)
82
    {
83 2
        if (is_null($urlSuffix)) {
84 2
            $urlSuffix = $this->evidence;
85 2
        }
86
87 2
        $url = $this->url.$this->prefix;
88 2
        if (!empty($urlSuffix)) {
89
            $url .= (($urlSuffix[0] == '.') ? '' : '/').$urlSuffix;
90
        }
91 2
        return $url;
92
    }
93
94
    /**
95
     * Vrací název evidence použité v odpovědích z FlexiBee
96
     *
97
     * @return string
98
     */
99 1
    public function getResponseEvidence()
100
    {
101 1
        return 'company';
102
    }
103
104
    /**
105
     * Parse Raw FlexiBee response in several formats
106
     *
107
     * @param string $responseRaw raw response body
108
     * @param string $format      Raw Response format json|xml|etc
109
     *
110
     * @return array
111
     */
112
    public function rawResponseToArray($responseRaw, $format)
113
    {
114
        if (strstr($responseRaw, 'winstrom')) {
115
            $nsbackup        = $this->nameSpace;
116
            $this->nameSpace = 'winstrom';
117
            $response        = parent::rawResponseToArray($responseRaw, $format);
118
            $this->nameSpace = $nsbackup;
119
        } else {
120
            $response = parent::rawResponseToArray($responseRaw, $format);
121
        }
122
        return $response;
123
    }
124
125
    /**
126
     * Save company backup to file
127
     *
128
     * @param string $filename
129
     *
130
     * @return boolean was backup saved to file ?
131
     */
132
    public function saveBackupTo($filename)
133
    {
134
        $result                                   = false;
135
        $headersBackup                            = $this->defaultHttpHeaders;
136
        $this->defaultHttpHeaders['Accept']       = '*/*';
137
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
138
        $this->performRequest($this->getDataValue('dbNazev').'/backup', 'GET');
139
        $this->defaultHttpHeaders                 = $headersBackup;
140
141
        if ($this->lastResponseCode == 200) {
142
            if (file_put_contents($filename, $this->lastCurlResponse)) {
143
                $result = true;
144
            }
145
        }
146
        return $result;
147
    }
148
149
    /**
150
     * Restore company from given file
151
     *
152
     * @param string $filename
153
     *
154
     * @return boolean result
155
     */
156
    public function restoreBackupFrom($filename)
157
    {
158
        $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...
159
        $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...
160
        $this->defaultHttpHeaders['Accept']       = '*/*';
161
        $this->defaultHttpHeaders['Content-Type'] = 'application/x-winstrom-backup';
162
        $this->setPostFields(file_get_contents($filename));
163
        $this->performRequest($this->getDataValue('dbNazev').'/restore', 'PUT');
164
        return $this->lastResponseCode == 200;
165
    }
166
167
    /**
168
     * Create new company
169
     *
170
     * @param string $name
171
     *
172
     * @return boolean
173
     */
174
    public function createNew($name)
175
    {
176
        $this->performRequest('/admin/zalozeni-firmy?name='.$name, 'PUT');
177
        return $this->lastResponseCode == 201;
178
    }
179
180
    /**
181
     * Obtain company identifier
182
     *
183
     * @return string company database name
184
     */
185 3
    public function getRecordID()
186
    {
187 3
        return $this->getDataValue('dbNazev');
188
    }
189
190
    /**
191
     * Company has no relations
192
     *
193
     * @return null
194
     */
195
    public function getVazby($id = null)
196
    {
197
        throw new \Exception(_('Company has no relations'));
198
    }
199
}
200