Completed
Push — master ( b847e9...aea582 )
by Ryan
02:11
created

WidgetModel::isPinned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Anomaly\DashboardModule\Widget;
2
3
use Anomaly\DashboardModule\Widget\Command\GetSortableFlag;
4
use Anomaly\DashboardModule\Widget\Contract\WidgetInterface;
5
use Anomaly\DashboardModule\Widget\Extension\Contract\WidgetExtensionInterface;
6
use Anomaly\Streams\Platform\Model\Dashboard\DashboardWidgetsEntryModel;
7
use Anomaly\UsersModule\Role\RoleCollection;
8
9
/**
10
 * Class WidgetModel
11
 *
12
 * @link          http://pyrocms.com/
13
 * @author        PyroCMS, Inc. <[email protected]>
14
 * @author        Ryan Thompson <[email protected]>
15
 * @package       Anomaly\DashboardModule\Widget
16
 */
17
class WidgetModel extends DashboardWidgetsEntryModel implements WidgetInterface
18
{
19
20
    /**
21
     * The widget data.
22
     *
23
     * @var WidgetCollection
24
     */
25
    protected $data;
26
27
    /**
28
     * Create a new WidgetModel instance.
29
     *
30
     * @param array $attributes
31
     */
32
    public function __construct(array $attributes = [])
33
    {
34
        $this->data = new WidgetCollection();
35
36
        parent::__construct($attributes);
37
    }
38
39
    /**
40
     * Get the pinned flag.
41
     *
42
     * @return bool
43
     */
44
    public function isPinned()
45
    {
46
        return $this->pinned;
47
    }
48
49
    /**
50
     * Get the column.
51
     *
52
     * @return int
53
     */
54
    public function getColumn()
55
    {
56
        return $this->column;
57
    }
58
59
    /**
60
     * Get the extension.
61
     *
62
     * @return WidgetExtensionInterface
63
     */
64
    public function getExtension()
65
    {
66
        return $this->extension;
67
    }
68
69
    /**
70
     * Get the allowed roles.
71
     *
72
     * @return RoleCollection
73
     */
74
    public function getAllowedRoles()
75
    {
76
        return $this->allowedRoles()->get();
77
    }
78
79
    /**
80
     * Get the content.
81
     *
82
     * @return string
83
     */
84
    public function getContent()
85
    {
86
        return $this->content;
87
    }
88
89
    /**
90
     * Set the content.
91
     *
92
     * @param $content
93
     * @return $this
94
     */
95
    public function setContent($content)
96
    {
97
        $this->content = $content;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Get the data.
104
     *
105
     * @return WidgetData
106
     */
107
    public function getData()
108
    {
109
        return $this->data;
110
    }
111
112
    /**
113
     * Add to the widget data.
114
     *
115
     * @param $key
116
     * @param $data
117
     * @return $this
118
     */
119
    public function addData($key, $data)
120
    {
121
        $this->data->put($key, $data);
122
123
        return $this;
124
    }
125
126
    /**
127
     * Return the sortable flag.
128
     *
129
     * @return bool
130
     */
131
    public function isSortable()
132
    {
133
        return $this->dispatch(new GetSortableFlag($this));
134
    }
135
136
    /**
137
     * Return the widget's context.
138
     *
139
     * @return string
140
     */
141
    public function context()
142
    {
143
        $extension = $this->getExtension();
144
145
        return $extension->getContext();
146
    }
147
148
    /**
149
     * Return the widget output.
150
     *
151
     * @return string
152
     */
153
    public function output()
154
    {
155
        $extension = $this->getExtension();
156
157
        return $extension->output($this);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $extension->output($this); (Illuminate\Contracts\View\View) is incompatible with the return type declared by the interface Anomaly\DashboardModule\...WidgetInterface::output of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
158
    }
159
}
160