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