Total Complexity | 45 |
Total Lines | 267 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MultiSite often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MultiSite, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class MultiSite |
||
26 | { |
||
27 | // Constants |
||
28 | // ========================================================================= |
||
29 | |||
30 | // Public Static Methods |
||
31 | // ========================================================================= |
||
32 | |||
33 | /** |
||
34 | * @param array $variables |
||
35 | */ |
||
36 | public static function setSitesMenuVariables(array &$variables) |
||
37 | { |
||
38 | // Set defaults based on the section settings |
||
39 | $variables['sitesMenu'] = [ |
||
40 | 0 => Craft::t( |
||
41 | 'webperf', |
||
42 | 'All Sites' |
||
43 | ), |
||
44 | ]; |
||
45 | // Enabled sites |
||
46 | $sites = Craft::$app->getSites(); |
||
47 | if (Craft::$app->getIsMultiSite()) { |
||
48 | |||
49 | /** @var Site $site */ |
||
50 | foreach ($sites->getAllGroups() as $group) { |
||
51 | $groupSites = $sites->getSitesByGroupId($group->id); |
||
52 | $variables['sitesMenu'][$group->name] |
||
53 | = ['optgroup' => $group->name]; |
||
54 | foreach ($groupSites as $groupSite) { |
||
55 | $variables['sitesMenu'][$groupSite->id] = $groupSite->name; |
||
56 | } |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $siteHandle |
||
63 | * @param $siteId |
||
64 | * @param $variables |
||
65 | * |
||
66 | * @throws \yii\web\ForbiddenHttpException |
||
67 | */ |
||
68 | public static function setMultiSiteVariables($siteHandle, &$siteId, array &$variables) |
||
69 | { |
||
70 | // Enabled sites |
||
71 | $sites = Craft::$app->getSites(); |
||
72 | if (Craft::$app->getIsMultiSite()) { |
||
73 | // Set defaults based on the section settings |
||
74 | $variables['enabledSiteIds'] = []; |
||
75 | $variables['siteIds'] = []; |
||
76 | |||
77 | /** @var Site $site */ |
||
78 | foreach ($sites->getEditableSiteIds() as $editableSiteId) { |
||
79 | $variables['enabledSiteIds'][] = $editableSiteId; |
||
80 | $variables['siteIds'][] = $editableSiteId; |
||
81 | } |
||
82 | |||
83 | // Make sure the $siteId they are trying to edit is in our array of editable sites |
||
84 | if (!\in_array($siteId, $variables['enabledSiteIds'], false)) { |
||
85 | if (!empty($variables['enabledSiteIds'])) { |
||
86 | if ($siteId !== 0) { |
||
87 | $siteId = reset($variables['enabledSiteIds']); |
||
88 | } |
||
89 | } else { |
||
90 | self::requirePermission('editSite:'.$siteId); |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | // Set the currentSiteId and currentSiteHandle |
||
95 | $variables['currentSiteId'] = empty($siteId) ? 0 : $siteId; |
||
96 | $variables['currentSiteHandle'] = empty($siteHandle) |
||
97 | ? Craft::$app->getSites()->currentSite->handle |
||
98 | : $siteHandle; |
||
99 | |||
100 | // Page title |
||
101 | $variables['showSites'] = ( |
||
102 | Craft::$app->getIsMultiSite() && |
||
103 | \count($variables['enabledSiteIds']) |
||
104 | ); |
||
105 | |||
106 | if ($variables['showSites']) { |
||
107 | if ($variables['currentSiteId'] === 0) { |
||
108 | $variables['sitesMenuLabel'] = Craft::t( |
||
109 | 'webperf', |
||
110 | 'All Sites' |
||
111 | ); |
||
112 | } else { |
||
113 | $variables['sitesMenuLabel'] = Craft::t( |
||
114 | 'site', |
||
115 | $sites->getSiteById((int)$variables['currentSiteId'])->name |
||
116 | ); |
||
117 | } |
||
118 | } else { |
||
119 | $variables['currentSiteId'] = 0; |
||
120 | $variables['sitesMenuLabel'] = ''; |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Return a siteId from a siteHandle |
||
126 | * |
||
127 | * @param string $siteHandle |
||
128 | * |
||
129 | * @return int|null |
||
130 | * @throws NotFoundHttpException |
||
131 | */ |
||
132 | public static function getSiteIdFromHandle($siteHandle) |
||
133 | { |
||
134 | // Get the site to edit |
||
135 | if ($siteHandle !== null) { |
||
136 | $site = Craft::$app->getSites()->getSiteByHandle($siteHandle); |
||
137 | if (!$site) { |
||
138 | throw new NotFoundHttpException('Invalid site handle: '.$siteHandle); |
||
139 | } |
||
140 | $siteId = $site->id; |
||
141 | } else { |
||
142 | $siteId = 0; |
||
143 | } |
||
144 | |||
145 | return $siteId; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns the site that most closely matches the requested URL. |
||
150 | * Adapted from craft\web\Request.php |
||
151 | * |
||
152 | * @param string $url |
||
153 | * |
||
154 | * @return Site |
||
155 | * @throws \craft\errors\SiteNotFoundException |
||
156 | */ |
||
157 | public static function getSiteFromUrl(string $url): Site |
||
222 | } |
||
223 | |||
224 | // Protected Static Methods |
||
225 | // ========================================================================= |
||
226 | |||
227 | /** |
||
228 | * @param string $permissionName |
||
229 | * |
||
230 | * @throws ForbiddenHttpException |
||
231 | */ |
||
232 | protected static function requirePermission(string $permissionName) |
||
233 | { |
||
234 | if (!Craft::$app->getUser()->checkPermission($permissionName)) { |
||
235 | throw new ForbiddenHttpException('User is not permitted to perform this action'); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Normalizes a URI path by trimming leading/trailing slashes and removing double slashes. |
||
241 | * |
||
242 | * @param string $path |
||
243 | * @return string |
||
244 | */ |
||
245 | protected static function normalizePath(string $path): string |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Returns the site’s base URL. |
||
252 | * |
||
253 | * @param Site $site |
||
254 | * |
||
255 | * @return string|null |
||
256 | */ |
||
257 | protected static function getBaseUrl(Site $site): string |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Checks if a string references an environment variable (`$VARIABLE_NAME`) |
||
268 | * and/or an alias (`@aliasName`), and returns the referenced value. |
||
269 | * |
||
270 | * --- |
||
271 | * |
||
272 | * ```php |
||
273 | * $value1 = Craft::parseEnv('$SMPT_PASSWORD'); |
||
274 | * $value2 = Craft::parseEnv('@webroot'); |
||
275 | * ``` |
||
276 | * |
||
277 | * @param string|null $str |
||
278 | * @return string|null The parsed value, or the original value if it didn’t |
||
279 | * reference an environment variable and/or alias. |
||
280 | */ |
||
281 | protected static function parseEnv(string $str = null) |
||
294 |