Stitek::getAvailbleLabels()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 4
1
<?php
2
/**
3
 * FlexiPeeHP - Objekt štítku.
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
 * Štítek
13
 *
14
 * @link https://demo.flexibee.eu/c/demo/stitek/properties Vlastnosti evidence
15
 */
16
class Stitek extends FlexiBeeRW
17
{
18
    /**
19
     * Evidence Path for vsb supported by label
20
     *
21
     * @var array
22
     */
23
    public static $vsbToEvidencePath = [
24
        'vsbAdr' => 'adresar', // Adresář
25
        'vsbBan' => 'banka', // Banka
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
//      'vsbCis' => 'ciselnik', // Číselníky
27
        'vsbFap' => 'faktura-prijata', // Přijaté faktury
28
        'vsbFav' => 'faktura-vydana', // Vydané faktury
29
//      'vsbInt' => '' // Interní doklady
30
        'vsbKatalog' => 'cenik', // Adresář
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
31
//      'vsbMaj' => '', // Majetek
32
//      'vsbMzd' => 'mzda', // Mzdy
33
        'vsbNap' => 'nabidka-prijata', // Nabídky přijaté
34
        'vsbNav' => 'nabidka-vydana', // Nabídky vydané
35
        'vsbObp' => 'objednavka-prijata', // Objednávky přijaté
36
        'vsbObv' => 'objednavka-vydana', // Objednávky vydané
37
        'vsbPhl' => 'pohledavka', // Pohledávky
38
        'vsbPok' => 'pokladna', // Pokladna
39
        'vsbPpp' => 'poptavka-prijata', // Poptávky přijaté
40
        'vsbPpv' => 'poptavka-vydana', // Poptávky vydané
41
        'vsbSkl' => 'sklad', // Sklad
42
        'vsbZav' => 'zavazek', // Závazky
43
    ];
44
45
    /**
46
     * Evidence užitá objektem.
47
     *
48
     * @var string
49
     */
50
    public $evidence = 'stitek';
51
52
    /**
53
     * Obtain labels for current record
54
     *
55
     * @deprecated since version 1.21
56
     * 
57
     * @param FlexiBeeRO $object data source
58
     * @return array labels
59
     */
60
    public static function getLabels($object)
61
    {
62
        $labels    = null;
63
        $labelsRaw = $object->getDataValue('stitky');
64
65
        if (strlen($labelsRaw)) {
66
            $labels = is_array($labelsRaw) ? $labelsRaw : self::listToArray($labelsRaw);
67
        }
68
        return $labels;
69
    }
70
71
    /**
72
     * Convert coma-separated list to array
73
     *
74
     * @param string $listRaw
75
     * 
76
     * @return array
77
     */
78
    public static function listToArray($listRaw)
79
    {
80
        if (is_array($listRaw)) {
0 ignored issues
show
introduced by
The condition is_array($listRaw) is always false.
Loading history...
81
            $list = array_combine(array_values($listRaw), array_values($listRaw));
82
        } else {
83
            if (strstr($listRaw, ',')) {
84
                $list = array_map('trim', explode(',', $listRaw));
85
            } else {
86
                $list = [$listRaw];
87
            }
88
            $list = array_combine($list, $list);
89
        }
90
        return empty($listRaw) ? [] : $list;
91
    }
92 1
93
    /**
94 1
     * Obtain list of availble labels for given object
95 1
     *
96 1
     * @param FlexiBeeRO $object
97 1
     * 
98
     * @return array
99 1
     */
100 1
    public static function getAvailbleLabels($object)
101 1
    {
102 1
        $labels         = [];
103 1
        $evidenceBackup = $object->getEvidence();
104 1
        $object->setEvidence('stitek');
105 1
        $pathToVsb      = array_flip(self::$vsbToEvidencePath);
106 1
107 1
        if (array_key_exists($evidenceBackup, $pathToVsb)) {
108
            $labelsRaw = $object->getColumnsFromFlexiBee(['kod', 'nazev'],
109 1
                [$pathToVsb[$evidenceBackup] => true], 'nazev');
110 1
            if (count($labelsRaw)) {
111
                foreach ($labelsRaw as $labelInfo) {
112
                    $labels[$labelInfo['kod']] = $labelInfo['nazev'];
113
                }
114
            }
115
        }
116
117
        $object->setEvidence($evidenceBackup);
118
        return $labels;
119
    }
120
121
    /**
122
     * Set Label for Current Object record
123
     *
124
     * @deprecated since version 1.21
125
     * 
126
     * @param string     $label
127
     * @param FlexiBeeRW $object
128
     *
129
     * @return boolean   success result ?
130
     */
131
    public static function setLabel($label, $object)
132
    {
133
        return $object->insertToFlexiBee(['id' => $object->getMyKey(), 'stitky' => $label]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $object->insertTo...), 'stitky' => $label)) returns the type array which is incompatible with the documented return type boolean.
Loading history...
134
    }
135
136
    /**
137
     * UnSet Label for Current Object record
138
     *
139
     * @deprecated since version 1.21
140
     * 
141
     * @param string     $label
142
     * @param FlexiBeeRW $object
143
     *
144
     * @return boolean   success result ?
145
     */
146
    public static function unsetLabel($label, $object)
147
    {
148
        $result = true;
149
        $labels = self::getLabels($object);
0 ignored issues
show
Deprecated Code introduced by
The function FlexiPeeHP\Stitek::getLabels() has been deprecated: since version 1.21 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

149
        $labels = /** @scrutinizer ignore-deprecated */ self::getLabels($object);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
150
        if (array_key_exists($label, $labels)) {
151
            unset($labels[$label]);
152
            $object->insertToFlexiBee(['id' => $object->getMyKey(), 'stitky@removeAll' => 'true',
153
                'stitky' => $labels]);
154
            $result = ($object->lastResponseCode == 201);
155
        }
156
        return $result;
157
    }
158
159
    /**
160
     * Create New Label for given evidencies
161
     * 
162
     * @param string $name       Label Name
163
     * @param array  $evidences  Evidence code list ex: ['faktura-vydana','faktura-prijata']
164
     * @param array  $options    Additional Label properties ex: ['kod'=>'EXAMPLE','skupVybKlic'=>'SKUPINA_STITKU'] 
165
     * 
166
     * @return boolean success
167
     */
168
    public function createNew($name, $evidences, $options = [])
169
    {
170
        $this->setData($options, true);
171
        $evidence2code = array_flip(self::$vsbToEvidencePath);
172
        foreach ($evidences as $evidence) {
173
            if (array_key_exists($evidence, $evidence2code)) {
174
                $this->setDataValue($evidence2code[$evidence], true);
175
            }
176
        }
177
178
        if (!array_key_exists('kod', $options)) {
179
            $this->setDataValue('kod', self::code($name));
180
        }
181
        $this->setDataValue('nazev', $name);
182
        return $this->sync();
183
    }
184
}
185