| Total Complexity | 51 |
| Total Lines | 353 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 2 |
Complex classes like CatalogueController 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 CatalogueController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class CatalogueController extends ContentController |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Find a filter from the URL that we can apply to the products list |
||
| 33 | * |
||
| 34 | * @return array |
||
| 35 | */ |
||
| 36 | public function getFilter() |
||
| 37 | { |
||
| 38 | $filter = ['Disabled' => 0]; |
||
| 39 | $tag = $this->getRequest()->getVar("t"); |
||
| 40 | |||
| 41 | if ($tag) { |
||
| 42 | $filter["Tags.URLSegment"] = $tag; |
||
| 43 | } |
||
| 44 | |||
| 45 | $this->extend("updateFilter", $filter); |
||
| 46 | |||
| 47 | return $filter; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get a paginated list of products contained in this category |
||
| 52 | * |
||
| 53 | * @return PaginatedList |
||
| 54 | */ |
||
| 55 | public function PaginatedProducts($limit = 10) |
||
| 56 | { |
||
| 57 | $products = $this->SortedProducts(); |
||
| 58 | $filter = $this->getFilter(); |
||
| 59 | |||
| 60 | if (count($filter)) { |
||
| 61 | $products = $products->filter($filter); |
||
| 62 | } |
||
| 63 | |||
| 64 | return PaginatedList::create( |
||
| 65 | $products, |
||
| 66 | $this->getRequest() |
||
| 67 | )->setPageLength($limit); |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Get a paginated list of all products at this level and below |
||
| 73 | * |
||
| 74 | * @return PaginatedList |
||
| 75 | */ |
||
| 76 | public function PaginatedAllProducts($limit = 10) |
||
| 77 | { |
||
| 78 | $products = $this->AllProducts(); |
||
| 79 | $filter = $this->getFilter(); |
||
| 80 | |||
| 81 | if (count($filter)) { |
||
| 82 | $products = $products->filter($filter); |
||
| 83 | } |
||
| 84 | |||
| 85 | return PaginatedList::create( |
||
| 86 | $products, |
||
| 87 | $this->getRequest() |
||
| 88 | )->setPageLength($limit); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The Controller will take the URLSegment parameter from the URL |
||
| 93 | * and use that to look up a record. |
||
| 94 | */ |
||
| 95 | public function __construct($dataRecord = null) |
||
| 96 | { |
||
| 97 | if (!$dataRecord) { |
||
| 98 | $dataRecord = new CatalogueCategory(); |
||
| 99 | if ($this->hasMethod("Title")) { |
||
| 100 | $dataRecord->Title = $this->Title(); |
||
| 101 | } |
||
| 102 | $dataRecord->URLSegment = get_class($this); |
||
| 103 | $dataRecord->ID = -1; |
||
| 104 | } |
||
| 105 | |||
| 106 | parent::__construct($dataRecord); |
||
| 107 | } |
||
| 108 | |||
| 109 | protected function init() |
||
| 110 | { |
||
| 111 | parent::init(); |
||
| 112 | |||
| 113 | // Check for subsites and add support |
||
| 114 | if (class_exists(Subsite::class)) { |
||
| 115 | $subsite = Subsite::currentSubsite(); |
||
| 116 | |||
| 117 | if ($subsite && $subsite->Theme) { |
||
| 118 | SSViewer::add_themes([$subsite->Theme]); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($subsite && i18n::getData()->validate($subsite->Language)) { |
||
| 122 | i18n::set_locale($subsite->Language); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * The productimage action is used to determine the default image that will |
||
| 129 | * appear related to a product |
||
| 130 | * |
||
| 131 | * @return Image |
||
| 132 | */ |
||
| 133 | public function ProductImage() |
||
| 134 | { |
||
| 135 | $image = $this->SortedImages()->first(); |
||
| 136 | |||
| 137 | $this->extend("updateProductImage", $image); |
||
| 138 | |||
| 139 | return $image; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Return the title, description, keywords and language metatags. |
||
| 144 | * NOTE: Shamelessley taken from SiteTree |
||
| 145 | * |
||
| 146 | * @param bool $includeTitle Show default <title>-tag, set to false for custom templating |
||
| 147 | * |
||
| 148 | * @return string The XHTML metatags |
||
| 149 | */ |
||
| 150 | public function MetaTags($includeTitle = true) |
||
| 151 | { |
||
| 152 | $tags = []; |
||
| 153 | |||
| 154 | if ($includeTitle && strtolower($includeTitle) != 'false') { |
||
| 155 | $tags[] = HTML::createTag( |
||
| 156 | 'title', |
||
| 157 | [], |
||
| 158 | $this->obj('Title')->forTemplate() |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | $generator = trim(Config::inst()->get(self::class, 'meta_generator')); |
||
| 163 | if (!empty($generator)) { |
||
| 164 | $tags[] = HTML::createTag( |
||
| 165 | 'meta', |
||
| 166 | [ |
||
| 167 | 'name' => 'generator', |
||
| 168 | 'content' => $generator, |
||
| 169 | ] |
||
| 170 | ); |
||
| 171 | } |
||
| 172 | |||
| 173 | $charset = ContentNegotiator::config()->uninherited('encoding'); |
||
| 174 | $tags[] = HTML::createTag( |
||
| 175 | 'meta', |
||
| 176 | [ |
||
| 177 | 'http-equiv' => 'Content-Type', |
||
| 178 | 'content' => 'text/html; charset=' . $charset, |
||
| 179 | ] |
||
| 180 | ); |
||
| 181 | if ($this->MetaDescription) { |
||
| 182 | $tags[] = HTML::createTag( |
||
| 183 | 'meta', |
||
| 184 | [ |
||
| 185 | 'name' => 'description', |
||
| 186 | 'content' => $this->MetaDescription, |
||
| 187 | ] |
||
| 188 | ); |
||
| 189 | } |
||
| 190 | |||
| 191 | if (Permission::check('CMS_ACCESS_CMSMain') && $this->exists()) { |
||
| 192 | $tags[] = HTML::createTag( |
||
| 193 | 'meta', |
||
| 194 | [ |
||
| 195 | 'name' => 'x-page-id', |
||
| 196 | 'content' => $this->obj('ID')->forTemplate() |
||
| 197 | ] |
||
| 198 | ); |
||
| 199 | $tags[] = HTML::createTag( |
||
| 200 | 'meta', |
||
| 201 | [ |
||
| 202 | 'name' => 'x-cms-edit-link', |
||
| 203 | 'content' => $this->obj('CMSEditLink')->forTemplate() |
||
| 204 | ] |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | $tags = implode("\n", $tags); |
||
| 209 | if ($this->ExtraMeta) { |
||
| 210 | $tags .= $this->obj('ExtraMeta')->forTemplate(); |
||
| 211 | } |
||
| 212 | |||
| 213 | $this->dataRecord->extend('MetaTags', $tags); |
||
| 214 | |||
| 215 | return $tags; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * This acts the same as {@link Controller::handleRequest()}, but if an action cannot be found this will attempt to |
||
| 220 | * fall over to a child controller in order to provide functionality for nested URLs. |
||
| 221 | * |
||
| 222 | * @param HTTPRequest $request |
||
| 223 | * @return HTTPResponse |
||
| 224 | * @throws HTTPResponse_Exception |
||
| 225 | */ |
||
| 226 | public function handleRequest(HTTPRequest $request) |
||
| 227 | { |
||
| 228 | /** |
||
| 229 | * @var SiteTree $child |
||
| 230 | */ |
||
| 231 | $child = null; |
||
| 232 | $action = $request->param('Action'); |
||
| 233 | |||
| 234 | // If nested URLs are enabled, and there is no action handler for the current request then attempt to pass |
||
| 235 | // control to a child controller. This allows for the creation of chains of controllers which correspond to a |
||
| 236 | // nested URL. |
||
| 237 | if ($action && !$this->hasAction($action)) { |
||
| 238 | // look for a child category with this URLSegment |
||
| 239 | $child = CatalogueCategory::get()->filter( |
||
| 240 | [ |
||
| 241 | 'ParentID' => $this->ID, |
||
| 242 | 'URLSegment' => rawurlencode($action), |
||
| 243 | 'Disabled' => 0 |
||
| 244 | ] |
||
| 245 | )->first(); |
||
| 246 | |||
| 247 | // Next check to see if the child os a product |
||
| 248 | if (!$child) { |
||
| 249 | $child = CatalogueProduct::get()->filter( |
||
| 250 | [ |
||
| 251 | "Categories.ID" => $this->ID, |
||
| 252 | "URLSegment" => rawurldecode($action), |
||
| 253 | 'Disabled' => 0 |
||
| 254 | ] |
||
| 255 | )->first(); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | // we found a page with this URLSegment. |
||
| 260 | if ($child) { |
||
| 261 | $request->shiftAllParams(); |
||
| 262 | $request->shift(); |
||
| 263 | |||
| 264 | $response = ModelAsController::controller_for_object($child)->handleRequest($request); |
||
| 265 | } else { |
||
| 266 | // If a specific locale is requested, and it doesn't match the page found by URLSegment, |
||
| 267 | // look for a translation and redirect (see #5001). Only happens on the last child in |
||
| 268 | // a potentially nested URL chain. |
||
| 269 | if (class_exists('Translatable')) { |
||
| 270 | $locale = $request->getVar('locale'); |
||
| 271 | if ($locale |
||
| 272 | && i18n::getData()->validate($locale) |
||
| 273 | && $this->dataRecord |
||
| 274 | && $this->dataRecord->Locale != $locale |
||
| 275 | ) { |
||
| 276 | $translation = $this->dataRecord->getTranslation($locale); |
||
| 277 | if ($translation) { |
||
| 278 | $response = new HTTPResponse(); |
||
| 279 | $response->redirect($translation->Link(), 301); |
||
| 280 | throw new HTTPResponse_Exception($response); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | Director::set_current_page($this->data()); |
||
| 286 | |||
| 287 | try { |
||
| 288 | $response = parent::handleRequest($request); |
||
| 289 | |||
| 290 | Director::set_current_page(null); |
||
| 291 | } catch (HTTPResponse_Exception $e) { |
||
| 292 | $this->popCurrent(); |
||
| 293 | |||
| 294 | Director::set_current_page(null); |
||
| 295 | |||
| 296 | throw $e; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | return $response; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Overwrite default SSViewer call to get a custom |
||
| 305 | * template list |
||
| 306 | * |
||
| 307 | * @param $action string |
||
| 308 | * @return SSViewer |
||
| 309 | */ |
||
| 310 | public function getViewer($action) |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns a fixed navigation menu of the given level. |
||
| 344 | * |
||
| 345 | * @return SS_List |
||
| 346 | */ |
||
| 347 | public function CategoryMenu($level = 1) |
||
| 381 | } |
||
| 382 | } |
||
| 383 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths