| Conditions | 23 |
| Paths | 273 |
| Total Lines | 150 |
| Code Lines | 101 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 14 | ||
| 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 |
||
| 69 | public function actionMetaBundles( |
||
| 70 | string $sort = 'sourceName|asc', |
||
| 71 | int $page = 1, |
||
| 72 | int $per_page = 20, |
||
| 73 | $filter = '', |
||
| 74 | $siteId = 0 |
||
| 75 | ): Response { |
||
| 76 | $data = []; |
||
| 77 | $sortField = 'sourceName'; |
||
| 78 | $sortType = 'ASC'; |
||
| 79 | // Figure out the sorting type |
||
| 80 | if ($sort !== '') { |
||
| 81 | if (strpos($sort, '|') === false) { |
||
| 82 | $sortField = $sort; |
||
| 83 | } else { |
||
| 84 | list($sortField, $sortType) = explode('|', $sort); |
||
| 85 | } |
||
| 86 | } |
||
| 87 | $sortType = strtoupper($sortType); |
||
| 88 | $sortType = self::SORT_MAP[$sortType] ?? self::SORT_MAP['DESC']; |
||
| 89 | $sortParams = [ |
||
| 90 | $sortField => $sortType, |
||
| 91 | ]; |
||
| 92 | // Validate untrusted data |
||
| 93 | if (!in_array($sortField, self::ALLOWED_SORT_FIELDS, true)) { |
||
| 94 | throw new BadRequestHttpException(Craft::t('seomatic', 'Invalid sort field specified.')); |
||
| 95 | } |
||
| 96 | if ($sortField !== 'sourceName') { |
||
| 97 | $sortParams = array_merge($sortParams, [ |
||
| 98 | 'sourceName' => self::SORT_MAP['ASC'], |
||
| 99 | ]); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Query the db table |
||
| 103 | $offset = ($page - 1) * $per_page; |
||
| 104 | |||
| 105 | $currentSiteHandle = ''; |
||
| 106 | if ((int)$siteId !== 0) { |
||
| 107 | $site = Craft::$app->getSites()->getSiteById($siteId); |
||
| 108 | if ($site !== null) { |
||
| 109 | $currentSiteHandle = $site->handle; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | $bundleQuery = (new Query()) |
||
| 113 | ->select(['mb.*']) |
||
| 114 | ->offset($offset) |
||
| 115 | ->limit($per_page) |
||
| 116 | ->orderBy($sortParams) |
||
| 117 | ; |
||
| 118 | |||
| 119 | // Since sectionIds, CategoryIds, etc. are not unique, we need to special case for them |
||
| 120 | $seoElements = Seomatic::$plugin->seoElements->getAllSeoElementTypes(); |
||
| 121 | $tableIndex = 1; |
||
| 122 | foreach ($seoElements as $seoElement) { |
||
| 123 | $tableIndex++; |
||
| 124 | $subQuery = (new Query()) |
||
| 125 | ->from(['{{%seomatic_metabundles}}']) |
||
| 126 | ->where(['=', 'sourceBundleType', $seoElement::META_BUNDLE_TYPE]); |
||
| 127 | if ((int)$siteId !== 0) { |
||
| 128 | $subQuery->andWhere(['sourceSiteId' => $siteId]); |
||
| 129 | } |
||
| 130 | if ($filter !== '') { |
||
| 131 | $subQuery->andWhere(['like', 'sourceName', $filter]); |
||
| 132 | } |
||
| 133 | $bundleQuery |
||
| 134 | ->where(['mb'.$tableIndex.'.id' => null]) |
||
| 135 | ->from(['mb' => $subQuery]) |
||
| 136 | ->leftJoin(['mb'.$tableIndex => $subQuery], [ |
||
| 137 | 'and', |
||
| 138 | '[[mb.sourceId]] = [[mb'.$tableIndex.'.sourceId]]', |
||
| 139 | '[[mb.id]] < [[mb'.$tableIndex.'.id]]' |
||
| 140 | ]); |
||
| 141 | } |
||
| 142 | $bundles = $bundleQuery->all(); |
||
| 143 | if ($bundles) { |
||
| 144 | $dataArray = []; |
||
| 145 | // Add in the `addLink` field |
||
| 146 | foreach ($bundles as $bundle) { |
||
| 147 | $dataItem = []; |
||
| 148 | $metaBundle = MetaBundle::create($bundle); |
||
| 149 | if ($metaBundle !== null) { |
||
| 150 | $sourceBundleType = $metaBundle->sourceBundleType; |
||
| 151 | $sourceHandle = $metaBundle->sourceHandle; |
||
| 152 | // For all the emojis |
||
| 153 | $dataItem['sourceName'] = html_entity_decode($metaBundle->sourceName, ENT_NOQUOTES, 'UTF-8'); |
||
| 154 | $dataItem['sourceType'] = $metaBundle->sourceType; |
||
| 155 | $dataItem['contentSeoUrl'] = UrlHelper::cpUrl( |
||
| 156 | "seomatic/edit-content/general/{$sourceBundleType}/{$sourceHandle}/{$currentSiteHandle}" |
||
| 157 | ); |
||
| 158 | // Fill in the number of entries |
||
| 159 | $entries = 0; |
||
| 160 | $seoElement = null; |
||
| 161 | if ($metaBundle->sourceBundleType) { |
||
| 162 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType( |
||
| 163 | $metaBundle->sourceBundleType |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | /** @var SeoElementInterface $seoElement */ |
||
| 167 | if ($seoElement !== null) { |
||
| 168 | // Ensure `null` so that the resulting element query is correct |
||
| 169 | if (empty($metaBundle->metaSitemapVars->sitemapLimit)) { |
||
| 170 | $metaBundle->metaSitemapVars->sitemapLimit = null; |
||
| 171 | } |
||
| 172 | $query = $seoElement::sitemapElementsQuery($metaBundle); |
||
| 173 | $entries = $query->count(); |
||
| 174 | } |
||
| 175 | $dataItem['entries'] = $entries; |
||
| 176 | // Basic configuration setup |
||
| 177 | $dataItem['title'] = $metaBundle->metaGlobalVars->seoTitle !== '' ? 'enabled' : 'disabled'; |
||
| 178 | $dataItem['description'] = $metaBundle->metaGlobalVars->seoDescription !== '' ? 'enabled' : 'disabled'; |
||
| 179 | $dataItem['image'] = $metaBundle->metaGlobalVars->seoImage !== '' ? 'enabled' : 'disabled'; |
||
| 180 | $dataItem['sitemap'] = $metaBundle->metaSitemapVars->sitemapUrls ? 'enabled' : 'disabled'; |
||
| 181 | $dataItem['robots'] = $metaBundle->metaGlobalVars->robots; |
||
| 182 | // Calculate the setup stat |
||
| 183 | $stat = 0; |
||
| 184 | $numGrades = \count(SettingsController::SETUP_GRADES); |
||
| 185 | $numFields = \count(SettingsController::SEO_SETUP_FIELDS); |
||
| 186 | foreach (SettingsController::SEO_SETUP_FIELDS as $setupField => $setupLabel) { |
||
| 187 | $stat += (int)!empty($metaBundle->metaGlobalVars[$setupField]); |
||
| 188 | $value = $variables['contentSetupChecklist'][$setupField]['value'] ?? 0; |
||
| 189 | $variables['contentSetupChecklist'][$setupField] = [ |
||
| 190 | 'label' => $setupLabel, |
||
| 191 | 'value' => $value + (int)!empty($metaBundle->metaGlobalVars[$setupField]), |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | $stat = round($numGrades - (($stat * $numGrades) / $numFields)); |
||
| 195 | if ($stat >= $numGrades) { |
||
| 196 | $stat = $numGrades - 1; |
||
| 197 | } |
||
| 198 | $dataItem['setup'] = SettingsController::SETUP_GRADES[$stat]; |
||
| 199 | |||
| 200 | $dataArray[] = $dataItem; |
||
| 201 | } |
||
| 202 | } |
||
| 203 | // Format the data for the API |
||
| 204 | $data['data'] = $dataArray; |
||
| 205 | $count = $bundleQuery->count(); |
||
| 206 | $data['links']['pagination'] = [ |
||
| 207 | 'total' => $count, |
||
| 208 | 'per_page' => $per_page, |
||
| 209 | 'current_page' => $page, |
||
| 210 | 'last_page' => ceil($count / $per_page), |
||
| 211 | 'next_page_url' => null, |
||
| 212 | 'prev_page_url' => null, |
||
| 213 | 'from' => $offset + 1, |
||
| 214 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
| 215 | ]; |
||
| 216 | } |
||
| 217 | |||
| 218 | return $this->asJson($data); |
||
| 219 | } |
||
| 224 |