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