| Conditions | 15 |
| Paths | 8 |
| Total Lines | 91 |
| Code Lines | 43 |
| 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 |
||
| 53 | public function process(): void |
||
| 54 | { |
||
| 55 | // creates a Menu collection for each language, with a default "main" menu |
||
| 56 | foreach ($this->config->getLanguages() as $language) { |
||
| 57 | $this->menus[$language['code']] = new MenusCollection('menus'); |
||
| 58 | $this->menus[$language['code']]->add(new Menu('main')); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->createMenusFromPages(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Removing/adding/replacing menus entries from config. |
||
| 65 | * ie: |
||
| 66 | * menus: |
||
| 67 | * main: |
||
| 68 | * # remove |
||
| 69 | * - id: about |
||
| 70 | * enabled: false |
||
| 71 | * # add |
||
| 72 | * - id: example |
||
| 73 | * name: "Example" |
||
| 74 | * url: https://example.com |
||
| 75 | * weight: 999 |
||
| 76 | * # replace |
||
| 77 | * - id: index |
||
| 78 | * name: "Home page" |
||
| 79 | */ |
||
| 80 | foreach ($this->config->getLanguages() as $language) { |
||
| 81 | if ($menusConfig = (array) $this->config->get('menus', $language['code'], false)) { |
||
| 82 | $totalConfig = array_sum(array_map('count', $menusConfig)); |
||
| 83 | $countConfig = 0; |
||
| 84 | |||
| 85 | foreach ($menusConfig as $menuConfig => $entry) { |
||
| 86 | if (!\is_array($entry)) { |
||
| 87 | break; |
||
| 88 | } |
||
| 89 | // add Menu if not exists |
||
| 90 | if (!$this->menus[$language['code']]->has($menuConfig)) { |
||
| 91 | $this->menus[$language['code']]->add(new Menu($menuConfig)); |
||
| 92 | } |
||
| 93 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 94 | $menu = $this->menus[$language['code']]->get($menuConfig); |
||
| 95 | foreach ($entry as $key => $properties) { |
||
| 96 | $countConfig++; |
||
| 97 | $updated = false; |
||
| 98 | |||
| 99 | // ID is required |
||
| 100 | if (!isset($properties['id'])) { |
||
| 101 | $this->builder->getLogger()->error(\sprintf('Config menu entry: key "id" is required for entry at position %s in "%s" menu', $key, $menu), ['progress' => [$countConfig, $totalConfig]]); |
||
| 102 | continue; |
||
| 103 | } |
||
| 104 | /** @var \Cecil\Collection\Menu\Entry $item */ |
||
| 105 | $item = (new Entry($properties['id'])) |
||
| 106 | ->setName($properties['name'] ?? ucfirst($properties['id'])) |
||
| 107 | ->setUrl($properties['url'] ?? '404') |
||
| 108 | ->setWeight((int) ($properties['weight'] ?? 0)); |
||
| 109 | // is entry already exists? |
||
| 110 | if ($menu->has($properties['id'])) { |
||
| 111 | // removes a not enabled entry |
||
| 112 | if (isset($properties['enabled']) && $properties['enabled'] === false) { |
||
| 113 | $menu->remove($properties['id']); |
||
| 114 | |||
| 115 | $message = \sprintf('Config menu entry "%s (%s) > %s" removed', (string) $menu, $language['code'], $properties['id']); |
||
| 116 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 117 | continue; |
||
| 118 | } |
||
| 119 | // merges properties |
||
| 120 | $current = $menu->get($properties['id'])->toArray(); |
||
| 121 | $properties = array_merge($current, $properties); |
||
| 122 | /** @var \Cecil\Collection\Menu\Entry $item */ |
||
| 123 | $item = clone $menu->get($properties['id']); |
||
| 124 | $item->setName($properties['name']) |
||
| 125 | ->setUrl($properties['url']) |
||
| 126 | ->setWeight($properties['weight']); |
||
| 127 | $updated = true; |
||
| 128 | } |
||
| 129 | // abord if entry is not enabled |
||
| 130 | if (isset($properties['enabled']) && $properties['enabled'] === false) { |
||
| 131 | continue; |
||
| 132 | } |
||
| 133 | // adds/replaces entry |
||
| 134 | $menu->add($item); |
||
| 135 | |||
| 136 | $message = \sprintf('Config menu entry "%s (%s) > %s" %s {name: %s, url: %s, weight: %s}', (string) $menu, $language['code'], $item->getId(), $updated ? 'updated' : 'created', $item-> getName(), $item->getUrl(), $item->getWeight()); |
||
| 137 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $this->builder->setMenus($this->menus); |
||
| 144 | } |
||
| 226 |