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