| Conditions | 17 |
| Paths | 288 |
| Total Lines | 91 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 46 | public function process(): void |
||
| 47 | { |
||
| 48 | // creates a 'menus' collection for each language, with a default 'main' menu |
||
| 49 | foreach ($this->config->getLanguages() as $language) { |
||
| 50 | $this->menus[$language['code']] = new MenusCollection('menus'); |
||
| 51 | $this->menus[$language['code']]->add(new Menu('main')); |
||
| 52 | } |
||
| 53 | |||
| 54 | // collects 'menu' entries from pages |
||
| 55 | $this->collectPages(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Removing/adding/replacing menus entries from config. |
||
| 59 | * ie: |
||
| 60 | * menus: |
||
| 61 | * main: |
||
| 62 | * - id: example |
||
| 63 | * name: "Example" |
||
| 64 | * url: https://example.com |
||
| 65 | * weight: 999 |
||
| 66 | * - id: about |
||
| 67 | * enabled: false. |
||
| 68 | */ |
||
| 69 | foreach ($this->config->getLanguages() as $language) { |
||
| 70 | if ($menusConfig = (array) $this->config->get('menus', $language['code'], false)) { |
||
| 71 | $totalConfig = array_sum(array_map('count', $menusConfig)); |
||
| 72 | $countConfig = 0; |
||
| 73 | $suffix = $language['code'] !== $this->config->getLanguageDefault() ? '.'.$language['code'] : ''; |
||
| 74 | |||
| 75 | foreach ($menusConfig as $menuConfig => $entry) { |
||
| 76 | // add Menu if not exists |
||
| 77 | if (!$this->menus[$language['code']]->has($menuConfig)) { |
||
| 78 | $this->menus[$language['code']]->add(new Menu($menuConfig)); |
||
| 79 | } |
||
| 80 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 81 | $menu = $this->menus[$language['code']]->get($menuConfig); |
||
| 82 | foreach ($entry as $key => $property) { |
||
| 83 | $countConfig++; |
||
| 84 | $enabled = true; |
||
| 85 | $updated = false; |
||
| 86 | |||
| 87 | // ID is required |
||
| 88 | if (!isset($property['id'])) { |
||
| 89 | throw new RuntimeException(\sprintf('"id" is required for entry at position %s in "%s" menu', $key, $menu)); |
||
| 90 | } |
||
| 91 | // enabled? |
||
| 92 | if (isset($property['enabled']) && false === $property['enabled']) { |
||
| 93 | $enabled = false; |
||
| 94 | if (!$menu->has($property['id'])) { |
||
| 95 | $message = \sprintf('Config menu entry "%s > %s%s" disabled', (string) $menu, $property['id'], $suffix); |
||
| 96 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | // is entry already exists? |
||
| 100 | if ($menu->has($property['id'])) { |
||
| 101 | // removes a disabled entry |
||
| 102 | if (!$enabled) { |
||
| 103 | $menu->remove($property['id']); |
||
| 104 | |||
| 105 | $message = \sprintf('Config menu entry "%s > %s%s" removed', (string) $menu, $property['id'], $suffix); |
||
| 106 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 107 | continue; |
||
| 108 | } |
||
| 109 | // merges properties |
||
| 110 | $updated = true; |
||
| 111 | $current = $menu->get($property['id'])->toArray(); |
||
| 112 | $property = array_merge($current, $property); |
||
| 113 | |||
| 114 | $message = \sprintf('Config menu entry "%s > %s%s" updated', (string) $menu, $property['id'], $suffix); |
||
| 115 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 116 | } |
||
| 117 | // adds/replaces entry |
||
| 118 | if ($enabled) { |
||
| 119 | $url404 = $language['code'] !== $this->config->getLanguageDefault() ? $language['code'].'/404.html' : '404.html'; |
||
| 120 | $item = (new Entry($property['id'])) |
||
| 121 | ->setName($property['name'] ?? ucfirst($property['id'])) |
||
| 122 | ->setUrl($property['url'] ?? $url404) |
||
| 123 | ->setWeight($property['weight'] ?? 0); |
||
| 124 | $menu->add($item); |
||
| 125 | |||
| 126 | if (!$updated) { |
||
| 127 | $message = \sprintf('Config menu entry "%s > %s%s" created', (string) $menu, $property['id'], $suffix); |
||
| 128 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | $this->builder->setMenus($this->menus); |
||
| 137 | } |
||
| 228 |