Completed
Pull Request — master (#4)
by
unknown
03:19
created

AttachmentFormGroup::setDependencies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
            'preset'   => $this['preset'],
98
            'objId'    => $this->objId(),
99
            'objType'  => $this->objType()
100
        ];
101
    }
102
103
    /**
104
     * Retrieve the widget's ID.
105
     *
106
     * @return string
107
     */
108
    public function widgetId()
109
    {
110
        if (!$this->widgetId) {
111
            $this->widgetId = 'attachment_widget_'.uniqid();
112
        }
113
114
        return $this->widgetId;
115
    }
116
117
    /**
118
     * Set the widget's ID.
119
     *
120
     * @param string $widgetId The widget identifier.
121
     * @return self
122
     */
123
    public function setWidgetId($widgetId)
124
    {
125
        $this->widgetId = $widgetId;
126
127
        return $this;
128
    }
129
130
    /**
131
     * @return Translation|string|null
132
     */
133
    public function description()
134
    {
135
        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...
136
    }
137
138
    /**
139
     * @return Translation|string|null
140
     */
141
    public function notes()
142
    {
143
        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...
144
    }
145
146
    /**
147
     * Show/hide the widget's notes.
148
     *
149
     * @param boolean|string $show Whether to show or hide notes.
150
     * @return self Chainable
151
     */
152
    public function setShowNotes($show)
153
    {
154
        $this->showNotesAbove = ($show === 'above');
155
        parent::setShowNotes($show);
156
157
        return $this;
158
    }
159
160
    /**
161
     * @return boolean
162
     */
163
    public function showNotesAbove()
164
    {
165
        return $this->showNotesAbove && $this->showNotes();
166
    }
167
168
    /**
169
     * @param Container $container The DI container.
170
     * @return void
171
     */
172
    protected function setDependencies(Container $container)
173
    {
174
        parent::setDependencies($container);
175
176
        $this->setWidgetFactory($container['widget/factory']);
177
        $this->setModelFactory($container['model/factory']);
178
179
        if (isset($container['attachments/config'])) {
180
            $this->setConfig($container['attachments/config']);
181
        } else if (isset($container['config']['attachments'])) {
182
            $this->setConfig($container['config']['attachments']);
183
        }
184
185
        // Satisfies Charcoal\View\ViewableInterface dependencies
186
        $this->setView($container['view']);
187
    }
188
189
    /**
190
     * Set the widget factory.
191
     *
192
     * @param FactoryInterface $factory The factory to create widgets.
193
     * @return self
194
     */
195
    protected function setWidgetFactory(FactoryInterface $factory)
196
    {
197
        $this->widgetFactory = $factory;
198
199
        return $this;
200
    }
201
202
    /**
203
     * Retrieve the widget factory.
204
     *
205
     * @return FactoryInterface
206
     * @throws RuntimeException If the widget factory was not previously set.
207
     */
208 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...
209
    {
210
        if ($this->widgetFactory === null) {
211
            throw new RuntimeException(sprintf(
212
                'Widget Factory is not defined for "%s"',
213
                get_class($this)
214
            ));
215
        }
216
217
        return $this->widgetFactory;
218
    }
219
}
220