Test Failed
Push — master ( ee2b4b...4f178a )
by Vítězslav
02:47
created

Stitek::setLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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
    static public $vsbToEvidencePath = [
24
        'vsbAdr' => 'adresar', // Adresář
25
        'vsbSkl' => 'sklad', // Sklad
26
        'vsbPhl' => 'pohledavka', // Pohledávky
27
        'vsbZav' => 'zavazek', // Závazky
28
        'vsbObp' => 'objednavka-prijata', // Objednávky přijaté
29
        'vsbNav' => 'objednavka-vydana', // Nabídky vydané
30
        'vsbPpp' => 'poptavka-prijata', // Poptávky přijaté
31
        'vsbObv' => 'objednavka-vydana', // Objednávky vydané
32
        'vsbNap' => 'nabidka-prijata', // Nabídky přijaté
33
        'vsbPpv' => 'poptavka-vydana', // Poptávky vydané
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...
34
//        'vsbMzd' => 'mzda', // Mzdy
35
//        'vsbCis' => 'ciselnik', // Číselníky
36
    ];
37
38
    /**
39
     * Evidence užitá objektem.
40
     *
41
     * @var string
42
     */
43
    public $evidence = 'stitek';
44
45
    /**
46
     * Obtain labels for current record
47
     *
48
     * @param FlexiBeeRO $object data source
49
     * @return array labels
50
     */
51
    public static function getLabels($object)
52
    {
53
        $labels    = null;
54
        $labelsRaw = $object->getDataValue('stitky');
55
56
        if (strlen($labelsRaw)) {
57
            $labels = is_array($labelsRaw) ? $labelsRaw : self::listToArray($labelsRaw);
58
        }
59
        return $labels;
60
    }
61
62
    /**
63
     * Convert coma-separated list to array
64
     *
65
     * @param string $listRaw
66
     * @return array
67
     */
68
    public static function listToArray($listRaw)
69
    {
70
        if (strstr($listRaw, ',')) {
71
            $list = array_map('trim', explode(',', $listRaw));
72
            $list = array_combine($list, $list);
73
        } else {
74
            $list = [$listRaw];
75
        }
76
        return $list;
77
    }
78
79
    /**
80
     * Obtain list of availble labels for given object
81
     *
82
     * @param FlexiBeeRO $object
83
     * @return array
84
     */
85
    public static function getAvailbleLabels($object)
86
    {
87
        $labels         = [];
88
        $evidenceBackup = $object->getEvidence();
89
        $object->setEvidence('stitek');
90
        $pathToVsb      = array_flip(self::$vsbToEvidencePath);
91
92
        if (array_key_exists($evidenceBackup, $pathToVsb)) {
93
            $labelsRaw = $object->getColumnsFromFlexiBee(['kod', 'nazev'],
94
                [$pathToVsb[$evidenceBackup] => true], 'nazev');
95
            if (count($labelsRaw)) {
96
                foreach ($labelsRaw as $labelInfo) {
97
                    $labels[$labelInfo['kod']] = $labelInfo['nazev'];
98
                }
99
            }
100
        }
101
102
        $object->setEvidence($evidenceBackup);
103
        return $labels;
104
    }
105
106
    /**
107
     * Set Label for Current Object record
108
     *
109
     * @param string     $label
110
     * @param FlexiBeeRW $object
111
     *
112
     * @return boolean   success result ?
113
     */
114
    static public function setLabel($label, $object)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
115
    {
116
        return $object->insertToFlexiBee(['id' => $object->getMyKey(), 'stitky' => $label]);
117
    }
118
119
    /**
120
     * UnSet Label for Current Object record
121
     *
122
     * @param string     $label
123
     * @param FlexiBeeRW $object
124
     *
125
     * @return boolean   success result ?
126
     */
127
    static public function unsetLabel($label, $object)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
128
    {
129
        $result = true;
130
        $labels = self::getLabels($object);
131
        if (array_key_exists($label, $labels)) {
132
            unset($labels[$label]);
133
            if ($object->insertToFlexiBee(['id' => $object->getMyKey(), 'stitky@removeAll' => 'true',
134
                    'stitky' => $labels])) {
135
                $result = true;
136
            }
137
        }
138
        return $result;
139
    }
140
}
141