Config::getNotificationTo()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php namespace Tequilarapido\Cli\Config;
2
3
use Hazbo\Json\Formatter;
4
use Herrera\Json\Exception\JsonException;
5
use Herrera\Json\Json;
6
7
/**
8
 * Manage configuration from the json configuration file
9
 */
10
class Config
11
{
12
13
    protected $file;
14
    protected $schemaFile;
15
    protected $raw;
16
17
    public function __construct($file, $schemaFile)
18
    {
19
        $this->file = $file;
20
        $this->schemaFile = $schemaFile;
21
    }
22
23
    public function load()
24
    {
25
        $linter = new Json();
26
        $json = $linter->decodeFile($this->file);
27
28
        // Validate json against schema
29
        try {
30
            $linter->validate($linter->decodeFile($this->schemaFile), $json);
31
        } catch (JsonException $e) {
32
            $this->echoJsonException($e);
33
            return false;
34
        }
35
36
        // Ok ?
37
        $this->raw = $json;
38
        return true;
39
    }
40
41
    public function getRaw()
42
    {
43
        return $this->raw;
44
    }
45
46
    public function getFile()
47
    {
48
        return $this->file;
49
    }
50
51
    public function getFormattedConfiguration()
52
    {
53
        $formatter = new Formatter();
54
        return $formatter->format(json_encode($this->raw));
55
    }
56
57
    public function getProject()
58
    {
59
        if (isset($this->raw->project)) {
60
            return $this->raw->project;
61
        }
62
63
        return null;
64
    }
65
66
67
    public function getReplacements()
68
    {
69
        if (isset($this->raw->replace->replacements)) {
70
            return $this->raw->replace->replacements;
71
        }
72
73
        return null;
74
    }
75
76
    public function isLockTables()
77
    {
78
        if (isset($this->raw->replace->lockTables)) {
79
            return (bool)json_decode($this->raw->replace->lockTables);
80
        }
81
82
        return null;
83
    }
84
85
    public function getExcludeTables()
86
    {
87
        if (isset($this->raw->replace->excludeTables) && is_array($this->raw->replace->excludeTables)) {
88
            return $this->raw->replace->excludeTables;
89
        }
90
91
        return array();
92
    }
93
94
95
    public function isSingleWebSite()
96
    {
97
        $replacements = $this->getReplacements();
98
        return is_array($replacements) && count($replacements) === 1;
99
    }
100
101
    public function getDatabase()
102
    {
103
        if (isset($this->raw->database)) {
104
            return array(
105
                'driver' => 'mysql',
106
                'host' => $this->raw->database->host,
107
                'database' => $this->raw->database->database,
108
                'username' => $this->raw->database->username,
109
                'password' => $this->raw->database->password,
110
                'charset' => 'utf8',
111
                'collation' => 'utf8_general_ci',
112
                'prefix' => '',
113
            );
114
        }
115
116
        return null;
117
    }
118
119 View Code Duplication
    public function getDatabaseName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
120
    {
121
        if (!empty($this->raw->database->database)) {
122
            return $this->raw->database->database;
123
        }
124
125
        throw new \LogicException('Database name is not specified. Maybe the configuration file is not valid');
126
    }
127
128 View Code Duplication
    public function getDatabasePrefix()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
129
    {
130
        if (!empty($this->raw->database->prefix)) {
131
            return $this->raw->database->prefix;
132
        }
133
134
        throw new \LogicException('Database prefix is not specified. Maybe the configuration file is not valid');
135
    }
136
137
    public function getCleanup()
138
    {
139
        if (isset($this->raw->cleanup)) {
140
            return $this->raw->cleanup;
141
        }
142
143
        return null;
144
    }
145
146
    public function getTruncateCleanup()
147
    {
148
        $conf = new \stdClass();
149
150
        if (isset($this->raw->cleanup->truncate->simple)) {
151
            $conf->simple = (array)$this->raw->cleanup->truncate->simple;
152
        }
153
154
        if (isset($this->raw->cleanup->truncate->multi)) {
155
            $conf->multi = (array)$this->raw->cleanup->truncate->multi;
156
        }
157
158
        if (!isset($conf->simple) && !isset($conf->multi)) {
159
            return null;
160
        }
161
162
        return $conf;
163
    }
164
165
    public function getDeleteCleanup()
166
    {
167
        if (empty($this->raw->cleanup->delete)) {
168
            return null;
169
        }
170
171
        return $this->raw->cleanup->delete;
172
    }
173
174
175
    /**
176
     * @param string $command
177
     * @return bool
178
     */
179
    public function isNotifyOnForCommand($command)
180
    {
181
        return !empty($this->raw->{$command}->notify)
182
            && $this->raw->{$command}->notify;
183
    }
184
185
    public function getNotificationConfig()
186
    {
187
        return !empty($this->raw->notify)
188
            && $this->raw->notify;
189
    }
190
191
    public function getNotificationTransport()
192
    {
193
        return empty($this->raw->notify->transport) ? null : $this->raw->notify->transport;
194
    }
195
196
    public function getNotificationFrom()
197
    {
198
        return empty($this->raw->notify->from) ? null : $this->raw->notify->from;
199
    }
200
201
    public function getNotificationTo()
202
    {
203
        return empty($this->raw->notify->to) ? null : $this->raw->notify->to;
204
    }
205
206
207
    /**
208
     * @param JsonException $e
209
     */
210
    private function echoJsonException($e)
211
    {
212
        echo 'Your configuration file may contain errors, ' . $e->getMessage() . PHP_EOL;
213
        foreach ($e->getErrors() as $k => $error) {
214
            echo ($k + 1) . ' : ' . $error . PHP_EOL;
215
        }
216
    }
217
}