Itemdata::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: mhaase
5
 * Date: 28.06.2017
6
 * Time: 10:26
7
 */
8
9
namespace HaaseIT\HCSF\Controller\CLI;
10
11
use Zend\ServiceManager\ServiceManager;
12
13
class Itemdata
14
{
15
    /**
16
     * @var ServiceManager
17
     */
18
    protected $serviceManager;
19
20
    /**
21
     * @var \Doctrine\DBAL\Connection
22
     */
23
    protected $dbal;
24
25
    /**
26
     * @var array
27
     */
28
    protected $items;
29
30
    /**
31
     * @var array
32
     */
33
    protected $itemschanged = [];
34
35
    public function __construct(ServiceManager $serviceManager)
36
    {
37
        $this->serviceManager = $serviceManager;
38
        $this->dbal = $serviceManager->get('dbal');
39
    }
40
41
    public function getItems()
42
    {
43
        return $this->items;
44
    }
45
46
    public function fetchItems()
47
    {
48
        $querybuilder = $this->dbal->createQueryBuilder();
49
50
        $querybuilder
51
            ->select('itm_id', 'itm_data')
52
            ->from('item_base')
53
        ;
54
55
        $stmt = $querybuilder->execute();
56
57
        $this->items = $stmt->fetchAll();
58
59
        $this->decodeItemData();
60
    }
61
62
    protected function decodeItemData()
63
    {
64
        foreach ($this->items as $key => $item) {
65
            if (empty($item['itm_data']) || ($itemdata = json_decode($item['itm_data'])) === null) {
66
                $itemdata = new \stdClass();
67
            }
68
            $this->items[$key]['itm_data'] = $itemdata;
69
        }
70
    }
71
72
    protected function encodeItemData()
73
    {
74
        foreach ($this->items as $key => $item) {
75
            $this->items[$key]['itm_data'] = json_encode($item['itm_data'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
76
        }
77
    }
78
79
    public function addDataWhere($field, $needle, $newfield, $content)
80
    {
81
        foreach ($this->items as $key => $item) {
82
            if ($needle !== false) {
83 View Code Duplication
                if (!empty($item['itm_data']->$field) && $item['itm_data']->$field == $needle) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                    $this->items[$key]['itm_data']->$newfield = $content;
85
                    $this->itemschanged[$key] = true;
86
                }
87
            } else {
88
                if (!empty($item['itm_data']->$field)) {
89
                    $this->items[$key]['itm_data']->$newfield = $content;
90
                    $this->itemschanged[$key] = true;
91
                }
92
            }
93
        }
94
    }
95
96
    // if $needls is set to false, existence of the field is enough to trigger removal
97
    public function removeDataWhere($field, $needle, $fieldtoremove)
98
    {
99
        foreach ($this->items as $key => $item) {
100
            if (!empty($item['itm_data']->$field)) {
101
                if ($needle) {
102 View Code Duplication
                    if ($item['itm_data']->$field == $needle && isset($item['itm_data']->$fieldtoremove)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
                        unset($this->items[$key]['itm_data']->$fieldtoremove);
104
                        $this->itemschanged[$key] = true;
105
                    }
106
                } else {
107
                    if (isset($item['itm_data']->$fieldtoremove)) {
108
                        unset($this->items[$key]['itm_data']->$fieldtoremove);
109
                        $this->itemschanged[$key] = true;
110
                    }
111
                }
112
            }
113
        }
114
    }
115
116
    public function writeItems()
117
    {
118
        $this->encodeItemData();
119
120
        foreach ($this->items as $key => $item) {
121
            if (!isset($this->itemschanged[$key])) {
122
                continue;
123
            }
124
            $querybuilder = $this->dbal->createQueryBuilder();
125
126
            $querybuilder
127
                ->update('item_base')
128
                ->set('itm_data', '?')
129
                ->setParameter(0, $item['itm_data'])
130
                ->where('itm_id = '.$item['itm_id'])
131
            ;
132
            echo 'updating item with itm_id: '.$item['itm_id'].PHP_EOL;
133
            $querybuilder->execute();
134
        }
135
    }
136
}