WidgetContentControllerExtension   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 26
c 1
b 0
f 1
dl 0
loc 65
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B handleWidget() 0 44 11
1
<?php
2
3
namespace SilverStripe\Widgets\Controllers;
4
5
use SilverStripe\Core\Extension;
6
use SilverStripe\Widgets\Model\WidgetArea;
7
8
/**
9
 * Add this to ContentController to enable widgets
10
 *
11
 * @package widgets
12
 */
13
class WidgetContentControllerExtension extends Extension
14
{
15
    /**
16
     *
17
     * @var array
18
     */
19
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
20
        'handleWidget'
21
    );
22
23
    /**
24
     * Handles widgets attached to a page through one or more {@link WidgetArea}
25
     * elements.
26
     *
27
     * Iterated through each $has_one relation with a {@link WidgetArea} and
28
     * looks for connected widgets by their database identifier.
29
     *
30
     * Assumes URLs in the following format: <URLSegment>/widget/<Widget-ID>.
31
     *
32
     * @return RequestHandler
0 ignored issues
show
Bug introduced by
The type SilverStripe\Widgets\Controllers\RequestHandler 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...
33
     */
34
    public function handleWidget()
35
    {
36
        $SQL_id = $this->owner->getRequest()->param('ID');
37
        if (!$SQL_id) {
38
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SilverStripe\Widgets\Controllers\RequestHandler.
Loading history...
39
        }
40
        /** @var SiteTree $widgetOwner */
41
        $widgetOwner = $this->owner->data();
42
        while ($widgetOwner->InheritSideBar && $widgetOwner->Parent()->exists()) {
43
            $widgetOwner = $widgetOwner->Parent();
44
        }
45
46
        // find WidgetArea relations
47
        $widgetAreaRelations = array();
48
        $hasOnes = $widgetOwner->hasOne();
49
50
        if (!$hasOnes) {
51
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type SilverStripe\Widgets\Controllers\RequestHandler.
Loading history...
52
        }
53
54
        foreach ($hasOnes as $hasOneName => $hasOneClass) {
55
            if ($hasOneClass == WidgetArea::class || is_subclass_of($hasOneClass, WidgetArea::class)) {
56
                $widgetAreaRelations[] = $hasOneName;
57
            }
58
        }
59
60
        // find widget
61
        $widget = null;
62
63
        foreach ($widgetAreaRelations as $widgetAreaRelation) {
64
            if ($widget) {
65
                break;
66
            }
67
68
            $widget = $widgetOwner->$widgetAreaRelation()->Widgets()
69
                ->filter('ID', $SQL_id)
70
                ->First();
71
        }
72
73
        if (!$widget) {
74
            user_error('No widget found', E_USER_ERROR);
75
        }
76
77
        return $widget->getController();
78
    }
79
}
80