Conditions | 20 |
Paths | 324 |
Total Lines | 124 |
Code Lines | 86 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
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 |
||
57 | public function actionMetaBundles( |
||
58 | string $sort = 'sourceName|asc', |
||
59 | int $page = 1, |
||
60 | int $per_page = 20, |
||
61 | $filter = '', |
||
62 | $siteId = 0 |
||
63 | ): Response { |
||
64 | $data = []; |
||
65 | $sortField = 'sourceName'; |
||
66 | $sortType = 'ASC'; |
||
67 | $additionalSort = ''; |
||
68 | // Figure out the sorting type |
||
69 | if ($sort !== '') { |
||
70 | if (strpos($sort, '|') === false) { |
||
71 | $sortField = $sort; |
||
72 | } else { |
||
73 | list($sortField, $sortType) = explode('|', $sort); |
||
74 | } |
||
75 | } |
||
76 | if ($sortField !== 'sourceName') { |
||
77 | $additionalSort = ', sourceName ASC'; |
||
78 | } |
||
79 | // Query the db table |
||
80 | $offset = ($page - 1) * $per_page; |
||
81 | |||
82 | $query = (new Query()) |
||
83 | ->from(['{{%seomatic_metabundles}}']) |
||
84 | ->offset($offset) |
||
85 | ->limit($per_page) |
||
86 | ->orderBy("{$sortField} {$sortType}".$additionalSort) |
||
87 | ->where(['!=', 'sourceBundleType', Seomatic::$plugin->metaBundles::GLOBAL_META_BUNDLE]) |
||
88 | ; |
||
89 | $currentSiteHandle = ''; |
||
90 | if ((int)$siteId !== 0) { |
||
91 | $query->andWhere(['sourceSiteId' => $siteId]); |
||
92 | $site = Craft::$app->getSites()->getSiteById($siteId); |
||
93 | if ($site !== null) { |
||
94 | $currentSiteHandle = $site->handle; |
||
95 | } |
||
96 | } |
||
97 | if ($filter !== '') { |
||
98 | $query->andWhere(['like', 'sourceName', $filter]); |
||
99 | } |
||
100 | $bundles = $query->all(); |
||
101 | if ($bundles) { |
||
102 | $dataArray = []; |
||
103 | // Add in the `addLink` field |
||
104 | foreach ($bundles as $bundle) { |
||
105 | $dataItem = []; |
||
106 | $metaBundle = MetaBundle::create($bundle); |
||
107 | if ($metaBundle !== null) { |
||
108 | $sourceBundleType = $metaBundle->sourceBundleType; |
||
109 | $sourceHandle = $metaBundle->sourceHandle; |
||
110 | $dataItem['sourceName'] = $metaBundle->sourceName; |
||
111 | $dataItem['sourceType'] = $metaBundle->sourceType; |
||
112 | $dataItem['contentSeoUrl'] = UrlHelper::cpUrl( |
||
113 | "seomatic/edit-content/general/{$sourceBundleType}/{$sourceHandle}/{$currentSiteHandle}" |
||
114 | ); |
||
115 | // Fill in the number of entries |
||
116 | $entries = 0; |
||
117 | $seoElement = Seomatic::$plugin->seoElements->getSeoElementByMetaBundleType( |
||
118 | $metaBundle->sourceBundleType |
||
119 | ); |
||
120 | /** @var SeoElementInterface $seoElement */ |
||
121 | if ($seoElement !== null) { |
||
122 | $query = $seoElement::sitemapElementsQuery($metaBundle); |
||
123 | $entries = $query->count(); |
||
124 | Craft::error('shit: ' . print_r($entries, true), __METHOD__); |
||
125 | } |
||
126 | $dataItem['entries'] = $entries; |
||
127 | // Basic configuration setup |
||
128 | $dataItem['title'] = $metaBundle->metaGlobalVars->seoTitle !== '' ? 'enabled' : 'disabled'; |
||
129 | $dataItem['description'] = $metaBundle->metaGlobalVars->seoDescription !== '' ? 'enabled' : 'disabled'; |
||
130 | $dataItem['image'] = $metaBundle->metaGlobalVars->seoImage !== '' ? 'enabled' : 'disabled'; |
||
131 | $dataItem['sitemap'] = $metaBundle->metaSitemapVars->sitemapUrls ? 'enabled' : 'disabled'; |
||
132 | $dataItem['robots'] = $metaBundle->metaGlobalVars->robots; |
||
133 | // Calculate the setup stat |
||
134 | $stat = 0; |
||
135 | $numGrades = \count(SettingsController::SETUP_GRADES); |
||
136 | $numFields = \count(SettingsController::SEO_SETUP_FIELDS); |
||
137 | foreach (SettingsController::SEO_SETUP_FIELDS as $setupField => $setupLabel) { |
||
138 | $stat += (int)!empty($metaBundle->metaGlobalVars[$setupField]); |
||
139 | $value = $variables['contentSetupChecklist'][$setupField]['value'] ?? 0; |
||
140 | $variables['contentSetupChecklist'][$setupField] = [ |
||
141 | 'label' => $setupLabel, |
||
142 | 'value' => $value + (int)!empty($metaBundle->metaGlobalVars[$setupField]), |
||
143 | ]; |
||
144 | } |
||
145 | $stat = round($numGrades - (($stat * $numGrades) / $numFields)); |
||
146 | if ($stat >= $numGrades) { |
||
147 | $stat = $numGrades - 1; |
||
148 | } |
||
149 | $dataItem['setup'] = SettingsController::SETUP_GRADES[$stat]; |
||
150 | |||
151 | $dataArray[] = $dataItem; |
||
152 | } |
||
153 | } |
||
154 | // Format the data for the API |
||
155 | $data['data'] = $dataArray; |
||
156 | $query = (new Query()) |
||
157 | ->from(['{{%seomatic_metabundles}}']) |
||
158 | ->orderBy("{$sortField} {$sortType}") |
||
159 | ->where(['!=', 'sourceBundleType', Seomatic::$plugin->metaBundles::GLOBAL_META_BUNDLE]) |
||
160 | ; |
||
161 | if ((int)$siteId !== 0) { |
||
162 | $query->andWhere(['sourceSiteId' => $siteId]); |
||
163 | } |
||
164 | if ($filter !== '') { |
||
165 | $query->andWhere(['like', 'sourceName', $filter]); |
||
166 | } |
||
167 | $count = $query->count(); |
||
168 | $data['links']['pagination'] = [ |
||
169 | 'total' => $count, |
||
170 | 'per_page' => $per_page, |
||
171 | 'current_page' => $page, |
||
172 | 'last_page' => ceil($count / $per_page), |
||
173 | 'next_page_url' => null, |
||
174 | 'prev_page_url' => null, |
||
175 | 'from' => $offset + 1, |
||
176 | 'to' => $offset + ($count > $per_page ? $per_page : $count), |
||
177 | ]; |
||
178 | } |
||
179 | |||
180 | return $this->asJson($data); |
||
181 | } |
||
186 |