Completed
Push — master ( 0aee47...def38b )
by Marcus
02:08
created

Itemdata::writeItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 3
eloc 13
nc 3
nop 0
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);
76
        }
77
    }
78
79
    public function addDataWhere($field, $needle, $newfield, $content)
80
    {
81
        foreach ($this->items as $key => $item) {
82 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...
83
                $this->items[$key]['itm_data']->$newfield = $content;
84
                $this->itemschanged[$key] = true;
85
            }
86
        }
87
    }
88
89
    // if $needls is set to false, existence of the field is enough to trigger removal
90
    public function removeDataWhere($field, $needle, $fieldtoremove)
91
    {
92
        foreach ($this->items as $key => $item) {
93
            if (!empty($item['itm_data']->$field)) {
94
                if ($needle) {
95 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...
96
                        unset($this->items[$key]['itm_data']->$fieldtoremove);
97
                        $this->itemschanged[$key] = true;
98
                    }
99
                } else {
100
                    if (isset($item['itm_data']->$fieldtoremove)) {
101
                        unset($this->items[$key]['itm_data']->$fieldtoremove);
102
                        $this->itemschanged[$key] = true;
103
                    }
104
                }
105
            }
106
        }
107
    }
108
109
    public function writeItems()
110
    {
111
        $this->encodeItemData();
112
113
        foreach ($this->items as $key => $item) {
114
            if (!isset($this->itemschanged[$key])) {
115
                continue;
116
            }
117
            $querybuilder = $this->dbal->createQueryBuilder();
118
119
            $querybuilder
120
                ->update('item_base')
121
                ->set('itm_data', '?')
122
                ->setParameter(0, $item['itm_data'])
123
                ->where('itm_id = '.$item['itm_id'])
124
            ;
125
            echo 'updating item with itm_id: '.$item['itm_id'].PHP_EOL;
126
            $querybuilder->execute();
127
        }
128
    }
129
}