WidgetArea   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 23
dl 0
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A forTemplate() 0 3 1
A ItemsToRender() 0 3 1
A Items() 0 3 1
A setTemplate() 0 3 1
A WidgetControllers() 0 16 3
1
<?php
2
3
namespace SilverStripe\Widgets\Model;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\ORM\HasManyList;
9
use SilverStripe\ORM\SS_List;
10
use SilverStripe\Versioned\Versioned;
11
12
/**
13
 * Represents a set of widgets shown on a page.
14
 */
15
class WidgetArea extends DataObject
16
{
17
    private static $has_many = [
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
18
        "Widgets" => Widget::class
19
    ];
20
21
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
22
        'Widgets',
23
    ];
24
25
    private static $cascade_deletes = [
0 ignored issues
show
introduced by
The private property $cascade_deletes is not used, and could be removed.
Loading history...
26
        'Widgets',
27
    ];
28
29
    private static $extensions = [
0 ignored issues
show
introduced by
The private property $extensions is not used, and could be removed.
Loading history...
30
        Versioned::class,
31
    ];
32
33
    private static $table_name = 'WidgetArea';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
34
35
    public $template = __CLASS__;
36
37
    /**
38
     * Used in template instead of {@link Widgets()} to wrap each widget in its
39
     * controller, making it easier to access and process form logic and
40
     * actions stored in {@link WidgetController}.
41
     *
42
     * @return SS_List - Collection of {@link WidgetController} instances.
43
     */
44
    public function WidgetControllers()
45
    {
46
        $controllers = new ArrayList();
47
        $items = $this->ItemsToRender();
48
        if (!is_null($items)) {
49
            foreach ($items as $widget) {
50
                /** @var Widget $widget */
51
52
                /** @var Controller $controller */
53
                $controller = $widget->getController();
54
55
                $controller->doInit();
56
                $controllers->push($controller);
57
            }
58
        }
59
        return $controllers;
60
    }
61
62
    /**
63
     * @return HasManyList
64
     */
65
    public function Items()
66
    {
67
        return $this->Widgets();
0 ignored issues
show
Bug introduced by
The method Widgets() does not exist on SilverStripe\Widgets\Model\WidgetArea. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
        return $this->/** @scrutinizer ignore-call */ Widgets();
Loading history...
68
    }
69
70
    /**
71
     * @return HasManyList
72
     */
73
    public function ItemsToRender()
74
    {
75
        return $this->Items()->filter('Enabled', 1);
76
    }
77
78
    /**
79
     * @return string - HTML
80
     */
81
    public function forTemplate()
82
    {
83
        return $this->renderWith($this->template);
84
    }
85
86
    /**
87
     *
88
     * @param string $template
89
     */
90
    public function setTemplate($template)
91
    {
92
        $this->template = $template;
93
    }
94
}
95