Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

BannersModuleManager::dropModuleFromComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace xbanners\src\Installers;
4
5
/**
6
 * @author Crayd
7
 */
8
final class BannersModuleManager
9
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
10
11
    /**
12
     * @var string
13
     */
14
    protected $sqlFile;
15
16
    /**
17
     * @var string
18
     */
19
    protected $sqlContent;
20
21
    /**
22
     * @var \CI_DB_active_record
23
     */
24
    protected $db;
25
26
    /**
27
     * @var name of module folder
28
     * used in self::setAutoload()
29
     */
30
    protected $moduleName = 'xbanners';
31
32
    /**
33
     * @var string
34
     */
35
    protected $bannerImagesFolder;
36
37
    /**
38
     * @var string
39
     */
40
    protected $currentTemplate;
41
42
    /**
43
     * Regular expression for selecting all CREATE queries from sql string
44
     */
45
    const DROP_PATTERN = '/DROP TABLE IF EXISTS \`[0-9a-z\_]+\`\;/i';
46
47
    /**
48
     * Regular expression for selecting all DROP queries from sql string
49
     */
50
    const CREATE_EXPRESSION = '/CREATE TABLE \`[0-9a-z\_]+\`[\s\S]+?\;/i';
51
52
    public function __construct() {
53
        $this->db = \CI::$APP->db;
54
        $this->sqlFile = realpath(__DIR__ . '/../../models/Shop.sql');
55
        $this->bannerImagesFolder = UPLOADSPATH . 'images/bimages';
56
        //        $this->bannerImagesFolder = UPLOADSPATH . 'banners/origins';
57
        $this->sqlContent = $this->getSqlFile();
58
    }
59
60
    /**
61
     * Create all tebles used in module
62
     *
63
     * @return boolean
64
     */
65
    public function install() {
66
        $this->deinstall();
67
        $this->setEnabled(TRUE);
68
        $this->makeBannersDirectory();
69
        return $this->executeQueries($this->getCreateQueries()) && $this->setAutoload(TRUE);
0 ignored issues
show
Security Bug introduced by
It seems like $this->getCreateQueries() targeting xbanners\src\Installers\...ger::getCreateQueries() can also be of type false; however, xbanners\src\Installers\...nager::executeQueries() does only seem to accept array, did you maybe forget to handle an error condition?
Loading history...
70
    }
71
72
    /**
73
     * Drop all tebles used in module
74
     *
75
     * @return boolean
76
     */
77
    public function deinstall() {
78
        $this->removeBannersDirectory();
79
        return $this->executeQueries($this->getDropQueries());
80
    }
81
82
    /**
83
     * @param boolean $bool
84
     */
85 View Code Duplication
    public function setAutoload($bool) {
86
        $state = $bool ? 1 : 0;
87
88
        return $this->db
89
            ->where(['name' => $this->moduleName])
90
            ->update('components', ['autoload' => $state]);
91
    }
92
93
    /**
94
     * @param boolean $bool
95
     */
96 View Code Duplication
    public function setEnabled($bool) {
97
        $state = $bool ? 1 : 0;
98
99
        return $this->db
100
            ->where(['name' => $this->moduleName])
101
            ->update('components', ['enabled' => $state]);
102
    }
103
104
    /**
105
     * Drop module row from components
106
     *
107
     * @return boolean
108
     */
109
    public function dropModuleFromComponents() {
110
        return $this->db->delete('components', ['name' => $this->moduleName]);
111
    }
112
113
    /**
114
     * Write places from "template_places.php" to db
115
     */
116
    public function installTemplatePlaces() {
117
        (new TemplatePlacesInstaller)->install();
118
    }
119
120
    public function updateTemplatePlaces() {
121
        (new TemplatePlacesInstaller)->update();
122
    }
123
124
    /**
125
     * @return boolean
126
     */
127
    protected function makeBannersDirectory() {
128
        mkdir(dirname($this->bannerImagesFolder), 0777, true);
129
        chmod(dirname($this->bannerImagesFolder), 0777);
130
131
        return mkdir($this->bannerImagesFolder, 0777, true) &&
132
                chmod($this->bannerImagesFolder, 0777);
133
    }
134
135
    protected function removeBannersDirectory() {
136
        return rmdir($this->bannerImagesFolder);
137
    }
138
139
    /**
140
     * Get content of sql file
141
     *
142
     * @return string
143
     */
144
    protected function getSqlFile() {
145
146
        return file_get_contents($this->sqlFile);
147
    }
148
149
    /**
150
     * Run self::getQueries() with DROP_PATTERN
151
     *
152
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
153
     */
154
    protected function getDropQueries() {
155
156
        return array_reverse($this->getQueries(self::DROP_PATTERN));
157
    }
158
159
    /**
160
     * Run self::getQueries() with CREATE_EXPRESSION
161
     *
162
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be string[]|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
163
     */
164
    protected function getCreateQueries() {
165
166
        return $this->getQueries(self::CREATE_EXPRESSION);
167
    }
168
169
    /**
170
     * Search all queries by passed pattern
171
     *
172
     * @param string $pattern
173
     * @return boolean|array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[]|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
174
     */
175
    protected function getQueries($pattern) {
176
        $matches = [];
177
        if (preg_match_all($pattern, $this->sqlContent, $matches)) {
178
179
            return $matches[0];
180
        }
181
182
        return FALSE;
183
    }
184
185
    /**
186
     * Execute array of queries (DROP OR CREATE)
187
     *
188
     * @param array $queries
189
     * @return bool Returns true only if all queries are successfull
190
     * @throws \Exception
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
191
     */
192
    protected function executeQueries(array $queries) {
193
        $allQueriesResult = true;
194
        foreach ($queries as $query) {
195
            $currentResult = $this->db->query($query);
196
            $allQueriesResult = $allQueriesResult && $currentResult;
197
            if (($error = $this->db->_error_message())) {
198
                throw new \Exception($error);
199
            }
200
        }
201
202
        return $allQueriesResult;
203
    }
204
205
}