Completed
Push — master ( f3b14d...1fe4c3 )
by Damian
10s
created

WidgetArea   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 2
dl 0
loc 87
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A WidgetControllers() 0 14 3
A Items() 0 4 1
A ItemsToRender() 0 5 1
A forTemplate() 0 4 1
A setTemplate() 0 4 1
A onBeforeDelete() 0 7 2
1
<?php
2
3
namespace SilverStripe\Widgets\Model;
4
5
use SilverStripe\ORM\ArrayList;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Widgets\Model\Widget;
8
9
/**
10
 * Represents a set of widgets shown on a page.
11
 *
12
 * @package widgets
13
 */
14
class WidgetArea extends DataObject
15
{
16
    /**
17
     * @var array
18
     */
19
    private static $has_many = array(
0 ignored issues
show
Unused Code introduced by
The property $has_many is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
20
        "Widgets" => Widget::class
21
    );
22
23
    /**
24
     * @var string
25
     */
26
    private static $table_name = 'WidgetArea';
0 ignored issues
show
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
28
    /**
29
     *
30
     * @var string
31
     */
32
    public $template = __CLASS__;
33
34
    /**
35
     * Used in template instead of {@link Widgets()} to wrap each widget in its
36
     * controller, making it easier to access and process form logic and
37
     * actions stored in {@link WidgetController}.
38
     *
39
     * @return SS_List - Collection of {@link WidgetController} instances.
40
     */
41
    public function WidgetControllers()
42
    {
43
        $controllers = new ArrayList();
44
        $items = $this->ItemsToRender();
45
        if (!is_null($items)) {
46
            foreach ($items as $widget) {
47
                $controller = $widget->getController();
48
49
                $controller->doInit();
50
                $controllers->push($controller);
51
            }
52
        }
53
        return $controllers;
54
    }
55
56
    /**
57
     * @return HasManyList
58
     */
59
    public function Items()
60
    {
61
        return $this->getComponents('Widgets');
62
    }
63
64
    /**
65
     * @return HasManyList
66
     */
67
    public function ItemsToRender()
68
    {
69
        return $this->getComponents('Widgets')
70
            ->filter("Enabled", 1);
71
    }
72
73
    /**
74
     * @return string - HTML
75
     */
76
    public function forTemplate()
77
    {
78
        return $this->renderWith($this->template);
79
    }
80
81
    /**
82
     *
83
     * @param string $template
84
     */
85
    public function setTemplate($template)
86
    {
87
        $this->template = $template;
88
    }
89
90
    /**
91
     * Delete all connected Widgets when this WidgetArea gets deleted
92
     */
93
    public function onBeforeDelete()
94
    {
95
        parent::onBeforeDelete();
96
        foreach ($this->Widgets() as $widget) {
0 ignored issues
show
Documentation Bug introduced by
The method Widgets does not exist on object<SilverStripe\Widgets\Model\WidgetArea>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
97
            $widget->delete();
98
        }
99
    }
100
}
101