CollectionTemplate::title()   F
last analyzed

Complexity

Conditions 17
Paths 1362

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 39
nc 1362
nop 0
dl 0
loc 69
rs 1.0499
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Charcoal\Admin\Template\Object;
4
5
use Exception;
6
use InvalidArgumentException;
7
8
// From PSR-7
9
use Psr\Http\Message\RequestInterface;
10
11
// From Pimple
12
use Pimple\Container;
13
14
15
// From 'charcoal-admin'
16
use Charcoal\Admin\AdminTemplate;
17
use Charcoal\Admin\Ui\CollectionContainerInterface;
18
use Charcoal\Admin\Ui\CollectionContainerTrait;
19
use Charcoal\Admin\Ui\DashboardContainerInterface;
20
use Charcoal\Admin\Ui\DashboardContainerTrait;
21
use Charcoal\Admin\Widget\SearchWidget;
22
23
/**
24
 * Object collection template (table with a list of objects).
25
 */
26
class CollectionTemplate extends AdminTemplate implements
27
    CollectionContainerInterface,
28
    DashboardContainerInterface
29
{
30
    use CollectionContainerTrait;
31
    use DashboardContainerTrait;
32
33
    /**
34
     * @param RequestInterface $request PSR-7 request.
35
     * @return boolean
36
     */
37
    public function init(RequestInterface $request)
38
    {
39
        parent::init($request);
40
        $this->createObjTable();
41
42
        return true;
43
    }
44
45
    /**
46
     * Retrieve the list of parameters to extract from the HTTP request.
47
     *
48
     * @return string[]
49
     */
50
    protected function validDataFromRequest()
51
    {
52
        return array_merge([
53
            'obj_type'
54
        ], parent::validDataFromRequest());
55
    }
56
57
    /**
58
     * Sets the search widget accodingly
59
     * Uses the "default_search_list" ident that should point
60
     * on ident in the "lists"
61
     *
62
     * @return \Charcoal\Admin\Widget\SearchWidget
63
     */
64
    public function searchWidget()
65
    {
66
        $widget = $this->widgetFactory()->create(SearchWidget::class);
67
        $widget->setObjType($this->objType());
68
69
        $listIdent = $this->metadataListIdent();
70
71
        // Note that if the ident doesn't match a list,
72
        // it will return basicly every properties of the object
73
        $widget->setCollectionIdent($listIdent);
74
75
        return $widget;
76
    }
77
78
    /**
79
     * Retrieve the title of the page.
80
     *
81
     * @return \Charcoal\Translator\Translation
82
     */
83
    public function title()
84
    {
85
        if (isset($this->title)) {
86
            return $this->title;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->title also could return the type string which is incompatible with the documented return type Charcoal\Translator\Translation.
Loading history...
87
        }
88
89
        $translator = $this->translator();
90
91
        try {
92
            $config = $this->dashboardConfig();
93
94
            if (isset($config['title'])) {
95
                $this->title = $translator->translation($config['title']);
96
                return $this->title;
97
            }
98
        } catch (Exception $e) {
99
            $this->logger->error($e->getMessage());
100
        }
101
102
        $model    = $this->proto();
103
        $metadata = $model->metadata();
104
        $objLabel = null;
105
106
        if (!$objLabel && isset($metadata['admin']['lists'])) {
0 ignored issues
show
introduced by
$objLabel is of type null, thus it always evaluated to false.
Loading history...
107
            $adminMetadata = $metadata['admin'];
108
109
            $listIdent = filter_input(INPUT_GET, 'collection_ident', FILTER_SANITIZE_STRING);
110
            if (!$listIdent) {
111
                $listIdent = $this->collectionIdent();
112
            }
113
114
            if (!$listIdent) {
115
                $listIdent = $this->collectionIdentFallback();
116
            }
117
118
            if ($listIdent && $model->view()) {
119
                $listIdent = $model->render($listIdent);
120
            }
121
122
            if (isset($adminMetadata['lists'][$listIdent]['label'])) {
123
                $objLabel = $translator->translation($adminMetadata['lists'][$listIdent]['label']);
124
            }
125
        }
126
127
        if (!$objLabel && isset($metadata['labels']['all_items'])) {
128
            $objLabel = $translator->translation($metadata['labels']['all_items']);
129
        }
130
131
        if (!$objLabel) {
132
            $objType = (isset($metadata['labels']['name'])
133
                        ? $translator->translation($metadata['labels']['name'])
134
                        : null);
135
136
            $objLabel = $translator->translation('Collection: {{ objType }}');
137
138
            if ($objType) {
139
                $objLabel = strtr($objLabel, [
0 ignored issues
show
Bug introduced by
It seems like $objLabel can also be of type null; however, parameter $str of strtr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

139
                $objLabel = strtr(/** @scrutinizer ignore-type */ $objLabel, [
Loading history...
140
                    '{{ objType }}' => $objType
141
                ]);
142
            }
143
        }
144
145
        if ($model->view()) {
146
            $this->title = $model->render((string)$objLabel, $model);
147
        } else {
148
            $this->title = (string)$objLabel;
149
        }
150
151
        return $this->title;
152
    }
153
154
    /**
155
     * @param Container $container DI Container.
156
     * @return void
157
     */
158
    protected function setDependencies(Container $container)
159
    {
160
        parent::setDependencies($container);
161
162
        // Required collection dependencies
163
        $this->setModelFactory($container['model/factory']);
164
        $this->setCollectionLoader($container['model/collection/loader']);
165
166
        // Required dashboard dependencies.
167
        $this->setDashboardBuilder($container['dashboard/builder']);
168
    }
169
170
    /**
171
     * @throws Exception If the dashboard config can not be loaded.
172
     * @return array
173
     */
174
    protected function createDashboardConfig()
175
    {
176
        $adminMetadata  = $this->objAdminMetadata();
177
        $dashboardIdent = $this->dashboardIdent();
178
179
        if (!$dashboardIdent) {
180
            $dashboardIdent = $this->metadataDashboardIdent();
181
        }
182
183
        if (!isset($adminMetadata['dashboards']) || !isset($adminMetadata['dashboards'][$dashboardIdent])) {
184
            throw new Exception(
185
                'Dashboard config is not defined.'
186
            );
187
        }
188
189
        $dashboardConfig = $adminMetadata['dashboards'][$dashboardIdent];
190
191
        return $dashboardConfig;
192
    }
193
194
    /**
195
     * @return void
196
     */
197
    private function createObjTable()
198
    {
199
        $obj = $this->proto();
200
        if (!$obj) {
0 ignored issues
show
introduced by
$obj is of type object, thus it always evaluated to true.
Loading history...
201
            return;
202
        }
203
204
        if ($obj->source()->tableExists() === false) {
205
            $obj->source()->createTable();
206
            $msg = $this->translator()->translate('Database table created for "{{ objType }}".', [
207
                '{{ objType }}' => $obj->objType()
208
            ]);
209
            $this->addFeedback(
210
                'notice',
211
                '<span class="fa fa-asterisk" aria-hidden="true"></span><span>&nbsp; '.$msg.'</span>'
212
            );
213
        }
214
    }
215
216
    /**
217
     * @return string
218
     */
219
    private function metadataListIdent()
220
    {
221
        $adminMetadata = $this->objAdminMetadata();
222
223
        if (isset($adminMetadata['default_search_list'])) {
224
            $listIdent = $adminMetadata['default_search_list'];
225
        } elseif (isset($adminMetadata['default_list'])) {
226
            $listIdent = $adminMetadata['default_list'];
227
        } else {
228
            $listIdent = 'default';
229
        }
230
231
        return $listIdent;
232
    }
233
234
    /**
235
     * @throws Exception If no default collection is defined.
236
     * @return string
237
     */
238
    private function metadataDashboardIdent()
239
    {
240
        $dashboardIdent = filter_input(INPUT_GET, 'dashboard_ident', FILTER_SANITIZE_STRING);
241
        if ($dashboardIdent) {
242
            return $dashboardIdent;
243
        }
244
245
        $adminMetadata = $this->objAdminMetadata();
246
        if (!isset($adminMetadata['default_collection_dashboard'])) {
247
            throw new Exception(sprintf(
248
                'No default collection dashboard defined in admin metadata for %s.',
249
                get_class($this->proto())
250
            ));
251
        }
252
253
        return $adminMetadata['default_collection_dashboard'];
254
    }
255
256
    /**
257
     * @throws Exception If the object's admin metadata is not set.
258
     * @return \ArrayAccess
259
     */
260
    private function objAdminMetadata()
261
    {
262
        $obj = $this->proto();
263
264
        $objMetadata = $obj->metadata();
265
266
        $adminMetadata = isset($objMetadata['admin']) ? $objMetadata['admin'] : [];
267
268
        return $adminMetadata;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $adminMetadata also could return the type array which is incompatible with the documented return type ArrayAccess.
Loading history...
269
    }
270
}
271