| Conditions | 14 |
| Paths | 64 |
| Total Lines | 82 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 | $page404 = '404.html'; |
||
| 74 | |||
| 75 | if ($language['code'] !== $this->config->getLanguageDefault()) { |
||
| 76 | $page404 = $language['code'] . '/404.html'; |
||
| 77 | } |
||
| 78 | |||
| 79 | foreach ($menusConfig as $menuConfig => $entry) { |
||
| 80 | // add Menu if not exists |
||
| 81 | if (!$this->menus[$language['code']]->has($menuConfig)) { |
||
| 82 | $this->menus[$language['code']]->add(new Menu($menuConfig)); |
||
| 83 | } |
||
| 84 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 85 | $menu = $this->menus[$language['code']]->get($menuConfig); |
||
| 86 | foreach ($entry as $key => $properties) { |
||
| 87 | $countConfig++; |
||
| 88 | $enabled = true; |
||
| 89 | $updated = false; |
||
| 90 | |||
| 91 | // ID is required |
||
| 92 | if (!isset($properties['id'])) { |
||
| 93 | throw new RuntimeException(sprintf('Key "id" is required for entry at position %s in "%s" menu', $key, $menu)); |
||
| 94 | } |
||
| 95 | // is entry already exists? |
||
| 96 | if ($menu->has($properties['id'])) { |
||
| 97 | // removes a not enabled entry |
||
| 98 | if (isset($properties['enabled']) && $properties['enabled'] === false) { |
||
| 99 | $enabled = false; |
||
|
|
|||
| 100 | $menu->remove($properties['id']); |
||
| 101 | |||
| 102 | $message = sprintf('Config menu entry "%s (%s) > %s" removed', (string) $menu, $language['code'], $properties['id']); |
||
| 103 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | // merges properties |
||
| 107 | $updated = true; |
||
| 108 | $current = $menu->get($properties['id'])->toArray(); |
||
| 109 | $properties = array_merge($current, $properties); |
||
| 110 | } |
||
| 111 | // adds/replaces entry |
||
| 112 | if ($enabled) { |
||
| 113 | $item = (new Entry($properties['id'])) |
||
| 114 | ->setName($properties['name'] ?? ucfirst($properties['id'])) |
||
| 115 | ->setUrl((new \Cecil\Assets\Url($this->builder, $properties['url'] ?? $page404))->getUrl()) |
||
| 116 | ->setWeight((int) ($properties['weight'] ?? 0)); |
||
| 117 | $menu->add($item); |
||
| 118 | |||
| 119 | $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()); |
||
| 120 | $this->builder->getLogger()->info($message, ['progress' => [$countConfig, $totalConfig]]); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->builder->setMenus($this->menus); |
||
| 128 | } |
||
| 219 |