| Total Complexity | 50 |
| Total Lines | 299 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Config implements ConfigInterface |
||
| 23 | { |
||
| 24 | use ContentManager, ConfigTrait; |
||
|
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected string $sourceCode = ''; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var BaseCommand |
||
| 33 | */ |
||
| 34 | protected BaseCommand $generator; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected string $className; |
||
| 40 | |||
| 41 | private array $queryParams = [ |
||
| 42 | ModelsInterface::PARAM_LIMIT, |
||
| 43 | ModelsInterface::PARAM_SORT, |
||
| 44 | ModelsInterface::PARAM_PAGE, |
||
| 45 | JSONApiInterface::PARAM_ACCESS_TOKEN, |
||
| 46 | ]; |
||
| 47 | |||
| 48 | private array $entityMethods = [ |
||
| 49 | ConfigInterface::STATE_MACHINE => ConfigInterface::STATE_MACHINE_METHOD, |
||
| 50 | ConfigInterface::SPELL_CHECK => ConfigInterface::SPELL_CHECK_METHOD, |
||
| 51 | ConfigInterface::BIT_MASK => ConfigInterface::BIT_MASK_METHOD, |
||
| 52 | ConfigInterface::CACHE => ConfigInterface::CACHE_METHOD, |
||
| 53 | ]; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Config constructor. |
||
| 57 | * @param $generator |
||
| 58 | */ |
||
| 59 | public function __construct($generator) |
||
| 60 | { |
||
| 61 | $this->generator = $generator; |
||
| 62 | $this->className = Classes::getClassName($this->generator->objectName); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Creates common config file |
||
| 67 | */ |
||
| 68 | public function create() |
||
| 69 | { |
||
| 70 | $this->setContent(); |
||
| 71 | // create config file |
||
| 72 | $file = $this->generator->formatConfigPath() . |
||
| 73 | ModulesInterface::CONFIG_FILENAME . PhpInterface::PHP_EXT; |
||
| 74 | $isCreated = FileManager::createFile($file, $this->sourceCode, true); |
||
| 75 | if ($isCreated) { |
||
| 76 | Console::out($file . PhpInterface::SPACE . Console::CREATED, Console::COLOR_GREEN); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Constructs the config structure |
||
| 82 | */ |
||
| 83 | private function setContent() |
||
| 84 | { |
||
| 85 | $this->setTag(); |
||
| 86 | $this->openRoot(); |
||
| 87 | $this->setParam(ModulesInterface::KEY_NAME, ApiInterface::RAML_TYPE_STRING, ucfirst($this->generator->version)); |
||
| 88 | $this->setParam(ConfigInterface::ATTRIBUTES_CASE, ApiInterface::RAML_TYPE_STRING, ConfigInterface::DEFAULT_CASE); |
||
| 89 | $this->setQueryParams(); |
||
| 90 | $this->setTrees(); |
||
| 91 | $this->setJwtContent(); |
||
| 92 | $this->setConfigEntities(); |
||
| 93 | $this->closeRoot(); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Sets query params e.g.: |
||
| 98 | * - limit |
||
| 99 | * - sort |
||
| 100 | * - access |
||
| 101 | */ |
||
| 102 | private function setQueryParams() |
||
| 103 | { |
||
| 104 | if (empty($this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS]) === false) { |
||
| 105 | $queryParams = $this->generator->types[CustomsInterface::CUSTOM_TYPES_QUERY_PARAMS][ApiInterface::RAML_PROPS]; |
||
| 106 | $this->openEntity(ConfigInterface::QUERY_PARAMS); |
||
| 107 | foreach ($this->queryParams as $param) { |
||
| 108 | if (empty($queryParams[$param][ApiInterface::RAML_KEY_DEFAULT]) === false) { |
||
| 109 | $this->setParam($param, $queryParams[$param][ApiInterface::RAML_TYPE], $queryParams[$param][ApiInterface::RAML_KEY_DEFAULT], 2); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $this->closeEntities(); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Sets JWT config array |
||
| 118 | * @example |
||
| 119 | * 'jwt' => [ |
||
| 120 | * 'enabled' => true, |
||
| 121 | * 'table' => 'user', |
||
| 122 | * 'activate' => 30, |
||
| 123 | * 'expires' => 3600, |
||
| 124 | * ], |
||
| 125 | */ |
||
| 126 | private function setJwtContent() |
||
| 127 | { |
||
| 128 | foreach ($this->generator->types as $objName => $objData) { |
||
| 129 | if (in_array($objName, $this->generator->customTypes) === false) { // if this is not a custom type generate resources |
||
| 130 | $excluded = false; |
||
| 131 | foreach ($this->generator->excludedSubtypes as $type) { |
||
| 132 | if (strpos($objName, $type) !== false) { |
||
| 133 | $excluded = true; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | // if the type is among excluded - continue |
||
| 137 | if ($excluded === true) { |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | $this->setJwtOptions($objName); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Sets all global config entities by crawling yaml config |
||
| 147 | * |
||
| 148 | * @uses setFsmOptions |
||
| 149 | * @uses setSpellOptions |
||
| 150 | * @uses setBitMaskOptions |
||
| 151 | * @uses setCacheOptions |
||
| 152 | */ |
||
| 153 | private function setConfigEntities() |
||
| 154 | { |
||
| 155 | foreach ($this->entityMethods as $entity => $methodName) { |
||
| 156 | $this->openEntity($entity); |
||
| 157 | foreach ($this->generator->types as $objName => $objData) { |
||
| 158 | if (in_array($objName, $this->generator->customTypes) === false) { // if this is not a custom type generate resources |
||
| 159 | $excluded = false; |
||
| 160 | foreach ($this->generator->excludedSubtypes as $type) { |
||
| 161 | if (strpos($objName, $type) !== false) { |
||
| 162 | $excluded = true; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | // if the type is among excluded - continue |
||
| 166 | if ($excluded === true) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | $this->setOptions($objName, $methodName); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | $this->closeEntities(); |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $objName |
||
| 178 | * @param string $methodName |
||
| 179 | */ |
||
| 180 | private function setOptions(string $objName, string $methodName) |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets cache config enabled option true to activate caching mechanism |
||
| 191 | * @param string $objName |
||
| 192 | */ |
||
| 193 | private function setCacheOptions(string $objName) |
||
| 194 | { |
||
| 195 | if (empty($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_TYPE]) === false |
||
| 196 | && $this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_TYPE] === CustomsInterface::CUSTOM_TYPE_REDIS) { |
||
| 197 | $this->openCache($objName); |
||
| 198 | foreach ($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE][ApiInterface::RAML_PROPS] as $prop => $value) { |
||
| 199 | $this->setParam($prop, $value[ApiInterface::RAML_TYPE], $value[ApiInterface::RAML_KEY_DEFAULT], 3); |
||
| 200 | } |
||
| 201 | $this->closeEntity(2, true); |
||
| 202 | // unset cache to prevent doubling |
||
| 203 | unset($this->generator->types[$objName][ApiInterface::RAML_PROPS][ConfigInterface::CACHE]); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $objName |
||
| 209 | * @param string $propKey |
||
| 210 | * @param $propVal |
||
| 211 | */ |
||
| 212 | private function setSpellOptions(string $objName, string $propKey, $propVal) |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param string $objName |
||
| 226 | * @param string $propKey |
||
| 227 | * @param $propVal |
||
| 228 | */ |
||
| 229 | private function setFsmOptions(string $objName, string $propKey, $propVal) |
||
| 230 | { |
||
| 231 | if (is_array($propVal)) {// create fsm |
||
| 232 | if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::STATE_MACHINE]) === false) { |
||
| 233 | // found FSM definition |
||
| 234 | $this->openFsm($objName, $propKey); |
||
| 235 | foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::STATE_MACHINE] as $key => &$val) { |
||
| 236 | $this->setTabs(5); |
||
| 237 | $this->setArrayProperty($key, (array)$val); |
||
| 238 | } |
||
| 239 | $this->closeEntities(); |
||
| 240 | } |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $objName |
||
| 246 | * @param string $propKey |
||
| 247 | * @param $propVal |
||
| 248 | * @example |
||
| 249 | * 'bit_mask'=> [ |
||
| 250 | * 'user'=> [ |
||
| 251 | * 'permissions'=> [ |
||
| 252 | * 'enabled' => true, |
||
| 253 | * 'flags'=> [ |
||
| 254 | * 'publisher' => 1, |
||
| 255 | * 'editor' => 2, |
||
| 256 | * 'manager' => 4, |
||
| 257 | * 'photo_reporter' => 8, |
||
| 258 | * 'admin' => 16, |
||
| 259 | * ], |
||
| 260 | * ], |
||
| 261 | * ], |
||
| 262 | * ], |
||
| 263 | */ |
||
| 264 | private function setBitMaskOptions(string $objName, string $propKey, $propVal) |
||
| 265 | { |
||
| 266 | if (is_array($propVal)) { |
||
| 267 | if (empty($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK]) === false) { |
||
| 268 | // found FSM definition |
||
| 269 | $this->openBitMask($objName, $propKey); |
||
| 270 | foreach ($propVal[ApiInterface::RAML_FACETS][ConfigInterface::BIT_MASK] as $key => $val) { |
||
| 271 | $this->setParam($key, ApiInterface::RAML_TYPE_INTEGER, $val, 5); |
||
| 272 | } |
||
| 273 | $this->closeEntities(); |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Sets jwt config options |
||
| 280 | * @param string $objName |
||
| 281 | * @example |
||
| 282 | * 'jwt'=> [ |
||
| 283 | * 'enabled' => true, |
||
| 284 | * 'table' => 'user', |
||
| 285 | * 'activate' => 30, |
||
| 286 | * 'expires' => 3600, |
||
| 287 | * ], |
||
| 288 | */ |
||
| 289 | private function setJwtOptions(string $objName) |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Sets config trees structure |
||
| 307 | * @example |
||
| 308 | * 'trees'=> [ |
||
| 309 | * 'menu' => true, |
||
| 310 | * ], |
||
| 311 | */ |
||
| 312 | private function setTrees() |
||
| 325 | } |