Completed
Push — master ( baa311...ce5686 )
by
unknown
02:40 queued 13s
created

AttachmentFormGroup::showNotesAbove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Charcoal\Admin\Widget\FormGroup;
4
5
use RuntimeException;
6
7
// From 'charcoal-admin'
8
use Charcoal\Admin\Ui\NestedWidgetContainerInterface;
9
use Charcoal\Admin\Ui\NestedWidgetContainerTrait;
10
use Charcoal\Admin\Ui\ObjectContainerInterface;
11
use Charcoal\Admin\Ui\ObjectContainerTrait;
12
13
// From 'charcoal-ui'
14
use Charcoal\Ui\FormGroup\AbstractFormGroup;
15
16
// From 'charcoal-config'
17
use Charcoal\Config\ConfigurableInterface;
18
19
// from 'charcoal-factory'
20
use Charcoal\Factory\FactoryInterface;
21
22
// from 'charcoal-translator'
23
use Charcoal\Translator\Translation;
24
25
// From 'pimple'
26
use Pimple\Container;
27
28
// from 'charcoal-attachment'
29
use Charcoal\Attachment\Traits\ConfigurableAttachmentsTrait;
30
31
/**
32
 * Attachment widget, as form group.
33
 */
34
class AttachmentFormGroup extends AbstractFormGroup implements
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: hasPermissions, isAuthorized
Loading history...
35
    ConfigurableInterface,
36
    NestedWidgetContainerInterface,
37
    ObjectContainerInterface
38
{
39
    use ConfigurableAttachmentsTrait;
40
    use NestedWidgetContainerTrait;
41
    use ObjectContainerTrait {
42
        ObjectContainerTrait::createOrLoadObj as createOrCloneOrLoadObj;
43
    }
44
45
    /**
46
     * @var string
47
     */
48
    private $widgetId;
49
50
    /**
51
     * Store the widget factory instance for the current class.
52
     *
53
     * @var FactoryInterface
54
     */
55
    private $widgetFactory;
56
57
    /**
58
     * Whether notes should be display before or after the form fields.
59
     *
60
     * @var boolean
61
     */
62
    private $showNotesAbove = false;
63
64
    /**
65
     * Set the widget's data.
66
     *
67
     * @param array $data The widget data.
68
     * @return self
69
     */
70
    public function setData(array $data)
71
    {
72
        /**
73
         * @todo Kinda hacky, but works with the concept of form.
74
         *     Should work embeded in a form group or in a dashboard.
75
         */
76
        $data = array_merge($_GET, $data);
77
78
        parent::setData($data);
79
80
        /** Merge any available presets */
81
        $data = $this->mergePresets($data);
82
83
        parent::setData($data);
84
85
        return $this;
86
    }
87
88
    /**
89
     * Retrieve the default nested widget options.
90
     *
91
     * @return array
92
     */
93
    public function defaultWidgetData()
94
    {
95
        return [
96
            'type'               => 'charcoal/admin/widget/attachment',
97
            'group'              => $this['group'],
98
            'attachment_options' => $this['attachmentOptions'],
99
            'attachable_objects' => $this['attachableObjects'],
100
            'preset'             => $this['preset'],
101
            'obj'                => $this->obj(),
102
            'objId'              => $this->objId(),
103
            'objType'            => $this->objType()
104
        ];
105
    }
106
107
    /**
108
     * Retrieve the widget's ID.
109
     *
110
     * @return string
111
     */
112
    public function widgetId()
113
    {
114
        if (!$this->widgetId) {
115
            $this->widgetId = 'attachment_widget_'.uniqid();
116
        }
117
118
        return $this->widgetId;
119
    }
120
121
    /**
122
     * Set the widget's ID.
123
     *
124
     * @param string $widgetId The widget identifier.
125
     * @return self
126
     */
127
    public function setWidgetId($widgetId)
128
    {
129
        $this->widgetId = $widgetId;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return Translation|string|null
136
     */
137
    public function description()
138
    {
139
        return $this->renderTemplate((string)parent::description());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->renderTemp...parent::description()); (string) is incompatible with the return type declared by the interface Charcoal\Ui\UiItemInterface::description of type Charcoal\Translator\Translation|null.

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...
140
    }
141
142
    /**
143
     * @return Translation|string|null
144
     */
145
    public function notes()
146
    {
147
        return $this->renderTemplate((string)parent::notes());
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->renderTemp...ring) parent::notes()); (string) is incompatible with the return type declared by the interface Charcoal\Ui\UiItemInterface::notes of type Charcoal\Translator\Translation|null.

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...
148
    }
149
150
    /**
151
     * Show/hide the widget's notes.
152
     *
153
     * @param boolean|string $show Whether to show or hide notes.
154
     * @return self Chainable
155
     */
156
    public function setShowNotes($show)
157
    {
158
        $this->showNotesAbove = ($show === 'above');
159
        parent::setShowNotes($show);
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return boolean
166
     */
167
    public function showNotesAbove()
168
    {
169
        return $this->showNotesAbove && $this->showNotes();
170
    }
171
172
    /**
173
     * @param Container $container The DI container.
174
     * @return void
175
     */
176
    protected function setDependencies(Container $container)
177
    {
178
        parent::setDependencies($container);
179
180
        $this->setWidgetFactory($container['widget/factory']);
181
        $this->setModelFactory($container['model/factory']);
182
183
        if (isset($container['attachments/config'])) {
184
            $this->setConfig($container['attachments/config']);
185
        } else if (isset($container['config']['attachments'])) {
186
            $this->setConfig($container['config']['attachments']);
187
        }
188
189
        // Satisfies Charcoal\View\ViewableInterface dependencies
190
        $this->setView($container['view']);
191
    }
192
193
    /**
194
     * Set the widget factory.
195
     *
196
     * @param FactoryInterface $factory The factory to create widgets.
197
     * @return self
198
     */
199
    protected function setWidgetFactory(FactoryInterface $factory)
200
    {
201
        $this->widgetFactory = $factory;
202
203
        return $this;
204
    }
205
206
    /**
207
     * Retrieve the widget factory.
208
     *
209
     * @return FactoryInterface
210
     * @throws RuntimeException If the widget factory was not previously set.
211
     */
212 View Code Duplication
    protected function widgetFactory()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214
        if ($this->widgetFactory === null) {
215
            throw new RuntimeException(sprintf(
216
                'Widget Factory is not defined for "%s"',
217
                get_class($this)
218
            ));
219
        }
220
221
        return $this->widgetFactory;
222
    }
223
}
224