DemodataBanners::prepareData()   F
last analyzed

Complexity

Conditions 20
Paths 1096

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
nc 1096
nop 1
dl 0
loc 46
rs 0
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace template_manager\installer;
4
5
/**
6
 * Image CMS
7
 * Module Template_manager
8
 * class DemodataBanners
9
 */
10
class DemodataBanners extends DemodataDirector
11
{
12
13
    /**
14
     * DemodataBanners SimpleXMLElement node
15
     * @var \SimpleXMLElement
16
     */
17
    public $node;
18
19
    private $bannerData = [];
20
21
    private $bannerI18nData = [];
22
23
    private $bannerGroupsData = [];
24
25
    private $existed_banners_groups = [];
26
27
    private $ci;
28
29
    public function __construct(\SimpleXMLElement $node) {
30
        $this->node = $node;
31
        $this->ci = & get_instance();
32
        $banners_groups = $this->ci->db->get('mod_banner_groups');
33
        if ($banners_groups) {
34
            $banners_groups = $banners_groups->result_array();
35
            foreach ($banners_groups as $group) {
36
                $this->existed_banners_groups[$group['name']] = $group['name'];
37
            }
38
        }
39
    }
40
41
    /**
42
     * Install baners into DB
43
     * @return boolean
44
     */
45
    public function install() {
46
        if (!SHOP_INSTALLED) {
47
            return TRUE;
48
        }
49
50
        $this->ci->db
51
            ->where('active', 1)
52
            ->set('active', 0)
53
            ->update('mod_banner');
54
55
        foreach ($this->node as $banner) {
56
            $this->prepareData($banner);
57
        }
58
59
        if ($this->bannerI18nData) {
60
            $this->ci->db->insert_batch('mod_banner_i18n', $this->bannerI18nData);
61
        }
62
63
        return TRUE;
64
    }
65
66
    /**
67
     * Prepare installed banners array
68
     */
69
    private function prepareData(\SimpleXMLElement $banner) {
70
        if ($banner->getName() != 'groups') {
71
            $attributes = $banner->attributes();
72
            $this->bannerData = [
73
                                 'group'      => (string) $attributes->group ? serialize(explode(',', trim((string) $attributes->group))) : '',
74
                                 'active'     => (string) $attributes->active ? (string) $attributes->active : 0,
75
                                 'active_to'  => (string) $attributes->active_to ? (string) $attributes->active_to : -1,
76
                                 'where_show' => (string) $attributes->where_show ? serialize([(string) $attributes->where_show . '_0']) : serialize(['default']),
77
                                 'position'   => (string) $attributes->position ? (string) $attributes->position : 0,
78
                                ];
79
80
            $this->ci->db->insert('mod_banner', $this->bannerData);
81
82
            if (isset($banner->banner_i18n) && $this->ci->db->insert_id()) {
83
                foreach ($banner->banner_i18n as $banner_i18n) {
84
                    $attributes = $banner_i18n->attributes();
85
                    $this->bannerI18nData[] = [
86
                                               'id'          => $this->ci->db->insert_id(),
87
                                               'name'        => (string) $attributes->name ? (string) $attributes->name : 'Banner',
88
                                               'description' => (string) $attributes->description ? (string) $attributes->description : '',
89
                                               'url'         => (string) $attributes->url ? (string) $attributes->url : '',
90
                                               'locale'      => (string) $attributes->locale ? (string) $attributes->locale : 'ru',
91
                                               'photo'       => (string) $attributes->photo ? (string) $attributes->photo : '',
92
                                              ];
93
                }
94
            } else {
95
                $this->messages[] = lang('Can not install banner.', 'template_manager');
0 ignored issues
show
Bug introduced by
The property messages cannot be accessed from this context as it is declared private in class template_manager\installer\DemodataDirector.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
96
                return FALSE;
97
            }
98
        } else {
99
            if (isset($banner->group)) {
100
                foreach ($banner->group as $group) {
101
                    $attributes = $group->attributes();
102
                    $this->bannerGroupsData = [
103
                                               'name' => (string) $attributes->name ? (string) $attributes->name : '',
104
                                              ];
105
106
                    if ($this->bannerGroupsData) {
107
                        if (!isset($this->existed_banners_groups[$this->bannerGroupsData['name']])) {
108
                            $this->ci->db->insert('mod_banner_groups', $this->bannerGroupsData);
109
                        }
110
                    }
111
                }
112
            }
113
        }
114
    }
115
116
}