Test Failed
Push — master ( 1555e6...7d3e18 )
by Vítězslav
10:27
created

Company   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 43.4%

Importance

Changes 0
Metric Value
dl 0
loc 160
ccs 23
cts 53
cp 0.434
rs 10
c 0
b 0
f 0
wmc 16
lcom 3
cbo 1

7 Methods

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