ModelAsController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A controller_for_object() 0 9 3
B getNestedController() 0 54 10
1
<?php
2
3
namespace SilverCommerce\CatalogueFrontend\Control;
4
5
use SilverStripe\Core\ClassInfo;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\CMS\Controllers\ModelAsController as CMSModelAsController;
10
use SilverCommerce\CatalogueAdmin\Model\CatalogueCategory;
11
12
/**
13
 * Customise default @link ModelAsController to allow for finding and setting
14
 * of catalogue categories or products.
15
 */
16
class ModelAsController extends CMSModelAsController
17
{
18
    /**
19
     * Get the appropriate {@link CatalogueProductController} or
20
     * {@link CatalogueProductController} for handling the relevent
21
     * object.
22
     *
23
     * @param  $object A {@link DataObject} with the getControllerName() method
0 ignored issues
show
Bug introduced by
The type SilverCommerce\CatalogueFrontend\Control\A was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     * @param  string                                                          $action
25
     * @return CatalogueController
26
     */
27
    public static function controller_for_object($object, $action = null)
28
    {
29
        $controller = $object->getControllerName();
30
31
        if ($action && class_exists($controller . '_' . ucfirst($action))) {
32
            $controller = $controller . '_' . ucfirst($action);
33
        }
34
35
        return Injector::inst()->create($controller, $object);
36
    }
37
38
    /**
39
     * @return ContentController
0 ignored issues
show
Bug introduced by
The type SilverCommerce\Catalogue...ntrol\ContentController was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
     * @throws Exception If URLSegment not passed in as a request parameter.
41
     */
42
    public function getNestedController()
43
    {
44
        $request = $this->getRequest();
45
46
        if (!$URLSegment = $request->param('URLSegment')) {
47
            throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
0 ignored issues
show
Bug introduced by
The type SilverCommerce\CatalogueFrontend\Control\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
48
        }
49
50
        // Find page by link, regardless of current locale settings
51
        if (class_exists('Translatable')) {
52
            Translatable::disable_locale_filter();
0 ignored issues
show
Bug introduced by
The type SilverCommerce\Catalogue...nd\Control\Translatable was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
        }
54
55
        // Select child page
56
        $sitetree_conditions = ["URLSegment" => rawurlencode($URLSegment)];
57
        $cat_conditions = $sitetree_conditions;
58
        $product_conditions = $sitetree_conditions;
0 ignored issues
show
Unused Code introduced by
The assignment to $product_conditions is dead and can be removed.
Loading history...
59
        
60
        if (SiteTree::config()->get('nested_urls')) {
61
            $sitetree_conditions['ParentID'] = 0;
62
        }
63
64
        $cat_conditions['ParentID'] = 0;
65
66
        $object = SiteTree::get()
67
            ->filter($sitetree_conditions)
68
            ->first();
69
70
        if (!$object) {
71
            $object = CatalogueCategory::get()
72
                ->filter($cat_conditions)
73
                ->first();
74
        }
75
76
        // Check translation module
77
        // @todo Refactor out module specific code
78
        if (class_exists('Translatable')) {
79
            Translatable::enable_locale_filter();
80
        }
81
82
        if (!$object) {
83
            $this->httpError(404, 'The requested page could not be found.');
84
        }
85
86
        // Enforce current locale setting to the loaded SiteTree object
87
        if (class_exists('Translatable') && $object->Locale) {
88
            Translatable::set_current_locale($object->Locale);
89
        }
90
91
        if (isset($_REQUEST['debug'])) {
92
            Debug::message("Using record #$object->ID of type " . get_class($object) . " with link {$sitetree->Link()}");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sitetree does not exist. Did you maybe mean $sitetree_conditions?
Loading history...
Bug introduced by
The type SilverCommerce\CatalogueFrontend\Control\Debug was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
        }
94
95
        return self::controller_for_object($object, $this->getRequest()->param('Action'));
96
    }
97
}