Issues (157)

src/Blocks/Config.php (1 issue)

Severity
1
<?php
2
3
namespace SoliDry\Blocks;
4
5
use SoliDry\Controllers\BaseCommand;
6
use SoliDry\Extension\JSONApiInterface;
7
use SoliDry\Helpers\Classes;
8
use SoliDry\Helpers\Console;
9
use SoliDry\Helpers\MigrationsHelper;
10
use SoliDry\Types\ConfigInterface;
11
use SoliDry\Types\CustomsInterface;
12
use SoliDry\Types\ModelsInterface;
13
use SoliDry\Types\ModulesInterface;
14
use SoliDry\Types\PhpInterface;
15
use SoliDry\Types\ApiInterface;
16
17
/**
18
 * Class Config
19
 *
20
 * @package SoliDry\Blocks
21
 */
22
class Config implements ConfigInterface
23
{
24
    use ContentManager, ConfigTrait;
0 ignored issues
show
The trait SoliDry\Blocks\ContentManager requires some properties which are not provided by SoliDry\Blocks\Config: $options, $version, $modulesDir
Loading history...
25
26
    protected $sourceCode = '';
27
    /** @var BaseCommand generator */
28
    protected $generator;
29
    protected $className;
30
31
    private $queryParams = [
32
        ModelsInterface::PARAM_LIMIT,
33
        ModelsInterface::PARAM_SORT,
34
        ModelsInterface::PARAM_PAGE,
35
        JSONApiInterface::PARAM_ACCESS_TOKEN,
36
    ];
37
38
    private $entityMethods = [
39
        ConfigInterface::STATE_MACHINE => ConfigInterface::STATE_MACHINE_METHOD,
40
        ConfigInterface::SPELL_CHECK   => ConfigInterface::SPELL_CHECK_METHOD,
41
        ConfigInterface::BIT_MASK      => ConfigInterface::BIT_MASK_METHOD,
42
        ConfigInterface::CACHE         => ConfigInterface::CACHE_METHOD,
43
    ];
44
45
    /**
46
     * Config constructor.
47
     * @param $generator
48
     */
49
    public function __construct($generator)
50
    {
51
        $this->generator = $generator;
52
        $this->className = Classes::getClassName($this->generator->objectName);
53
    }
54
55
    /**
56
     *  Creates common config file
57
     */
58
    public function create()
59
    {
60
        $this->setContent();
61
        // create config file
62
        $file      = $this->generator->formatConfigPath() .
63
            ModulesInterface::CONFIG_FILENAME . PhpInterface::PHP_EXT;
64
        $isCreated = FileManager::createFile($file, $this->sourceCode, true);
65
        if ($isCreated) {
66
            Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN);
67
        }
68
    }
69
70
    /**
71
     * Constructs the config structure
72
     */
73
    private function setContent()
74
    {
75
        $this->setTag();
76
        $this->openRoot();
77
        $this->setParam(ModulesInterface::KEY_NAME, ApiInterface::RAML_TYPE_STRING, ucfirst($this->generator->version));
78
        $this->setQueryParams();
79
        $this->setTrees();
80
        $this->setJwtContent();
81
        $this->setConfigEntities();
82
        $this->closeRoot();
83
    }
84
85
    /**
86
     *  Sets query params e.g.:
87
     *  - limit
88
     *  - sort
89
     *  - access
90
     */
91
    private function setQueryParams()
92
    {
93
        if (empty($this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS]) === false) {
94
            $queryParams = $this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS];
95
            $this->openEntity(ConfigInterface::QUERY_PARAMS);
96
            foreach ($this->queryParams as $param) {
97
                if (empty($queryParams[$param][ApiInterface::RAML_KEY_DEFAULT]) === false) {
98
                    $this->setParam($param, $queryParams[$param][ApiInterface::RAML_TYPE], $queryParams[$param][ApiInterface::RAML_KEY_DEFAULT], 2);
99
                }
100
            }
101
            $this->closeEntities();
102
        }
103
    }
104
105
    /**
106
     *  Sets JWT config array
107
     * @example
108
     *    'jwt'                  => [
109
     *      'enabled'  => true,
110
     *      'table'    => 'user',
111
     *      'activate' => 30,
112
     *      'expires'  => 3600,
113
     *    ],
114
     */
115
    private function setJwtContent()
116
    {
117
        foreach ($this->generator->types as $objName => $objData) {
118
            if (in_array($objName, $this->generator->customTypes) === false) { // if this is not a custom type generate resources
119
                $excluded = false;
120
                foreach ($this->generator->excludedSubtypes as $type) {
121
                    if (strpos($objName, $type) !== false) {
122
                        $excluded = true;
123
                    }
124
                }
125
                // if the type is among excluded - continue
126
                if ($excluded === true) {
127
                    continue;
128
                }
129
                $this->setJwtOptions($objName);
130
            }
131
        }
132
    }
133
134
    /**
135
     * Sets all global config entities by crawling yaml config
136
     *
137
     * @uses setFsmOptions
138
     * @uses setSpellOptions
139
     * @uses setBitMaskOptions
140
     * @uses setCacheOptions
141
     */
142
    private function setConfigEntities()
143
    {
144
        foreach ($this->entityMethods as $entity => $methodName) {
145
            $this->openEntity($entity);
146
            foreach ($this->generator->types as $objName => $objData) {
147
                if (in_array($objName, $this->generator->customTypes) === false) { // if this is not a custom type generate resources
148
                    $excluded = false;
149
                    foreach ($this->generator->excludedSubtypes as $type) {
150
                        if (strpos($objName, $type) !== false) {
151
                            $excluded = true;
152
                        }
153
                    }
154
                    // if the type is among excluded - continue
155
                    if ($excluded === true) {
156
                        continue;
157
                    }
158
                    $this->setOptions($objName, $methodName);
159
                }
160
            }
161
            $this->closeEntities();
162
        }
163
    }
164
165
    /**
166
     * @param string $objName
167
     * @param string $methodName
168
     */
169
    private function setOptions(string $objName, string $methodName)
170
    {
171
        if (empty($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS]) === false) {
172
            foreach ($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS] as $propKey => $propVal) {
173
                $this->$methodName($objName, $propKey, $propVal);
174
            }
175
        }
176
    }
177
178
    /**
179
     * Sets cache config enabled option true to activate caching mechanism
180
     * @param string $objName
181
     */
182
    private function setCacheOptions(string $objName)
183
    {
184
        if (empty($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_TYPE]) === false
185
            && $this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_TYPE] === CustomsInterface::CUSTOM_TYPE_REDIS) {
186
            $this->openCache($objName);
187
            foreach ($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_PROPS] as $prop => $value) {
188
                $this->setParam($prop, $value[ApiInterface::RAML_TYPE], $value[ApiInterface::RAML_KEY_DEFAULT], 3);
189
            }
190
            $this->closeEntity(2, true);
191
            // unset cache to prevent doubling
192
            unset($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE]);
193
        }
194
    }
195
196
    /**
197
     * @param string $objName
198
     * @param string $propKey
199
     * @param $propVal
200
     */
201
    private function setSpellOptions(string $objName, string $propKey, $propVal)
202
    {
203
        if (is_array($propVal) && empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::SPELL_CHECK]) === false) {
204
            // found FSM definition
205
            $this->openSc($objName, $propKey);
206
            $this->setParam(ConfigInterface::LANGUAGE, ApiInterface::RAML_TYPE_STRING, empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::SPELL_LANGUAGE])
207
                ? ConfigInterface::DEFAULT_LANGUAGE
208
                : $propVal[ApiInterface::RAML_FACETS][ConfigInterface::SPELL_LANGUAGE], 4);
209
            $this->closeEntities();
210
        }
211
    }
212
213
    /**
214
     * @param string $objName
215
     * @param string $propKey
216
     * @param $propVal
217
     */
218
    private function setFsmOptions(string $objName, string $propKey, $propVal)
219
    {
220
        if (is_array($propVal)) {// create fsm
221
            if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::STATE_MACHINE]) === false) {
222
                // found FSM definition
223
                $this->openFsm($objName, $propKey);
224
                foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::STATE_MACHINE] as $key => &$val) {
225
                    $this->setTabs(5);
226
                    $this->setArrayProperty($key, (array)$val);
227
                }
228
                $this->closeEntities();
229
            }
230
        }
231
    }
232
233
    /**
234
     * @param string $objName
235
     * @param string $propKey
236
     * @param $propVal
237
     * @example
238
     * 'bit_mask'=> [
239
     *  'user'=> [
240
     *       'permissions'=> [
241
     *       'enabled' => true,
242
     *       'flags'=> [
243
     *           'publisher' => 1,
244
     *           'editor' => 2,
245
     *           'manager' => 4,
246
     *           'photo_reporter' => 8,
247
     *           'admin' => 16,
248
     *           ],
249
     *       ],
250
     *     ],
251
     *   ],
252
     */
253
    private function setBitMaskOptions(string $objName, string $propKey, $propVal)
254
    {
255
        if (is_array($propVal)) {
256
            if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK]) === false) {
257
                // found FSM definition
258
                $this->openBitMask($objName, $propKey);
259
                foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK] as $key => $val) {
260
                    $this->setParam($key, ApiInterface::RAML_TYPE_INTEGER, $val, 5);
261
                }
262
                $this->closeEntities();
263
            }
264
        }
265
    }
266
267
    /**
268
     * Sets jwt config options
269
     * @param string $objName
270
     * @example
271
     *  'jwt'=> [
272
     *   'enabled' => true,
273
     *   'table' => 'user',
274
     *   'activate' => 30,
275
     *   'expires' => 3600,
276
     *  ],
277
     */
278
    private function setJwtOptions(string $objName)
279
    {
280
        if (empty($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS]) === false) {
281
            foreach ($this->generator->types[$objName . CustomsInterface::CUSTOM_TYPES_ATTRIBUTES][ApiInterface::RAML_PROPS] as $propKey => $propVal) {
282
                if (is_array($propVal) && $propKey === CustomsInterface::CUSTOM_PROP_JWT) {// create jwt config setting
283
                    $this->openEntity(ConfigInterface::JWT);
284
                    $this->setParam(ConfigInterface::ENABLED, ApiInterface::RAML_TYPE_BOOLEAN, PhpInterface::PHP_TYPES_BOOL_TRUE, 2);
285
                    $this->setParam(ModelsInterface::MIGRATION_TABLE, ApiInterface::RAML_TYPE_STRING, MigrationsHelper::getTableName($objName), 2);
286
                    $this->setParam(ConfigInterface::ACTIVATE, ApiInterface::RAML_TYPE_INTEGER, ConfigInterface::DEFAULT_ACTIVATE, 2);
287
                    $this->setParam(ConfigInterface::EXPIRES, ApiInterface::RAML_TYPE_INTEGER, ConfigInterface::DEFAULT_EXPIRES, 2);
288
                    $this->closeEntities();
289
                }
290
            }
291
        }
292
    }
293
294
    /**
295
     *  Sets config trees structure
296
     * @example
297
     *   'trees'=> [
298
     *       'menu' => true,
299
     *   ],
300
     */
301
    private function setTrees()
302
    {
303
        if (empty($this->generator->types[CustomsInterface::CUSTOM_TYPES_TREES][ApiInterface::RAML_PROPS]) === false) {
304
            foreach ($this->generator->types[CustomsInterface::CUSTOM_TYPES_TREES][ApiInterface::RAML_PROPS] as $propKey => $propVal) {
305
                if (is_array($propVal) && empty($this->generator->types[ucfirst($propKey)]) === false) {
306
                    // ensure that there is a type of propKey ex.: Menu with parent_id field set
307
                    $this->openEntity(ConfigInterface::TREES);
308
                    $this->setParamDefault($propKey, $propVal[ApiInterface::RAML_KEY_DEFAULT]);
309
                    $this->closeEntities();
310
                }
311
            }
312
        }
313
    }
314
}