DocTemplate::createDashboardConfig()   B
last analyzed

Complexity

Conditions 10
Paths 42

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 26
nc 42
nop 0
dl 0
loc 46
rs 7.6666
c 0
b 0
f 0

How to fix   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\Docs\Template\Object;
4
5
use Exception;
6
7
// From Pimple
8
use Pimple\Container;
9
10
// From 'charcoal-admin'
11
use Charcoal\Admin\AdminTemplate;
12
use Charcoal\Admin\Ui\DashboardContainerInterface;
13
use Charcoal\Admin\Ui\DashboardContainerTrait;
14
use Charcoal\Admin\Ui\ObjectContainerInterface;
15
use Charcoal\Admin\Ui\ObjectContainerTrait;
16
17
/**
18
 * Object Edit Template
19
 */
20
class DocTemplate extends AdminTemplate implements
21
    DashboardContainerInterface,
22
    ObjectContainerInterface
23
{
24
    use DashboardContainerTrait;
25
    use ObjectContainerTrait;
26
27
    /**
28
     * Retrieve the list of parameters to extract from the HTTP request.
29
     *
30
     * @return string[]
31
     */
32
    protected function validDataFromRequest()
33
    {
34
        return array_merge([
35
            'obj_type'
36
        ], parent::validDataFromRequest());
37
    }
38
39
    /**
40
     * Retrieve the header menu.
41
     *
42
     * @return array
43
     */
44
    public function headerMenu()
45
    {
46
        if ($this->headerMenu === null) {
47
            $dashboardConfig = $this->dashboardConfig();
48
49
            if (isset($dashboardConfig['secondary_menu'])) {
50
                $this->headerMenu = $this->createHeaderMenu($dashboardConfig['secondary_menu']);
0 ignored issues
show
Bug Best Practice introduced by
The property headerMenu does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The method createHeaderMenu() does not exist on Charcoal\Admin\Docs\Template\Object\DocTemplate. ( Ignorable by Annotation )

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

50
                /** @scrutinizer ignore-call */ 
51
                $this->headerMenu = $this->createHeaderMenu($dashboardConfig['secondary_menu']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
            } else {
52
                $this->headerMenu = $this->createHeaderMenu();
53
            }
54
        }
55
56
        return $this->headerMenu;
57
    }
58
59
    /**
60
     * Retrieve the title of the page.
61
     *
62
     * @return \Charcoal\Translator\Translation
63
     */
64
    public function title()
65
    {
66
        if (isset($this->title)) {
67
            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...
68
        }
69
70
        try {
71
            $config = $this->dashboardConfig();
72
73
            if (isset($config['title'])) {
74
                $this->title = $this->translator()->translation($config['title']);
75
76
                return $this->title;
77
            }
78
        } catch (Exception $e) {
79
            $this->logger->error($e->getMessage());
80
        }
81
82
        $obj = $this->obj();
83
        $metadata = $obj->metadata();
84
        $objLabel = null;
85
86
        if (!$objLabel && isset($metadata['admin']['forms'])) {
0 ignored issues
show
introduced by
$objLabel is of type null, thus it always evaluated to false.
Loading history...
87
            $adminMetadata = $metadata['admin'];
88
89
            $formIdent = filter_input(INPUT_GET, 'form_ident', FILTER_SANITIZE_STRING);
90
            if (!$formIdent) {
91
                $formIdent = (isset($adminMetadata['default_form']) ? $adminMetadata['default_form'] : '');
92
            }
93
94
            if (isset($adminMetadata['forms'][$formIdent]['label'])) {
95
                $objLabel = $this->translator()->translation($adminMetadata['forms'][$formIdent]['label']);
96
            }
97
        }
98
99
        if (!$objLabel) {
100
            $objType = (isset($metadata['labels']['singular_name']) ?
101
                $this->translator()->translation($metadata['labels']['singular_name']) : null);
102
103
            $objLabel = $this->translator()->translation('Documentation: {{ objType }}');
104
105
            if ($objType) {
106
                $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

106
                $objLabel = strtr(/** @scrutinizer ignore-type */ $objLabel, [
Loading history...
107
                    '{{ objType }}' => $objType
108
                ]);
109
            }
110
        }
111
112
        if ($obj->view()) {
0 ignored issues
show
Bug introduced by
The method view() does not exist on Charcoal\Model\ModelInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Charcoal\Queue\QueueItemInterface or Charcoal\Object\ContentInterface or Charcoal\Object\UserDataInterface or Charcoal\User\UserInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

112
        if ($obj->/** @scrutinizer ignore-call */ view()) {
Loading history...
113
            $this->title = $obj->render((string)$objLabel, $obj);
0 ignored issues
show
Bug introduced by
The method render() does not exist on Charcoal\Model\ModelInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Charcoal\Queue\QueueItemInterface or Charcoal\Object\ContentInterface or Charcoal\Object\UserDataInterface or Charcoal\User\UserInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

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

113
            /** @scrutinizer ignore-call */ 
114
            $this->title = $obj->render((string)$objLabel, $obj);
Loading history...
114
        } else {
115
            $this->title = (string)$objLabel;
116
        }
117
118
        return $this->title;
119
    }
120
121
    /**
122
     * @param Container $container DI container.
123
     * @return void
124
     */
125
    protected function setDependencies(Container $container)
126
    {
127
        parent::setDependencies($container);
128
129
        // Required ObjectContainerInterface dependencies
130
        $this->setModelFactory($container['model/factory']);
131
132
        // Required dependencies.
133
        $this->dashboardBuilder = $container['dashboard/builder'];
134
    }
135
136
    /**
137
     * @throws Exception If the object's dashboard config can not be loaded.
138
     * @return array
139
     */
140
    protected function createDashboardConfig()
141
    {
142
        $adminMetadata  = $this->objAdminMetadata();
143
        $dashboardIdent = $this->dashboardIdent();
144
145
        if (empty($dashboardIdent)) {
146
            $dashboardIdent = filter_input(INPUT_GET, 'dashboard_ident', FILTER_SANITIZE_STRING);
147
        }
148
149
        if (empty($dashboardIdent)) {
150
            if (isset($adminMetadata['default_doc_dashboard'])) {
151
                $dashboardIdent = $adminMetadata['default_doc_dashboard'];
152
            }
153
        }
154
155
        $overrideType = false;
156
157
        if (empty($dashboardIdent)) {
158
            if (!isset($adminMetadata['default_edit_dashboard'])) {
159
                throw new Exception(sprintf(
160
                    'No default doc dashboard defined in admin metadata for %s',
161
                    get_class($this->obj())
162
                ));
163
            }
164
            $overrideType = true;
165
            $dashboardIdent = $adminMetadata['default_edit_dashboard'];
166
        }
167
168
        if (!isset($adminMetadata['dashboards']) || !isset($adminMetadata['dashboards'][$dashboardIdent])) {
169
            throw new Exception(
170
                'Dashboard config is not defined.'
171
            );
172
        }
173
174
        $dashboardConfig = $adminMetadata['dashboards'][$dashboardIdent];
175
176
        if ($overrideType) {
177
            $widgets = $dashboardConfig['widgets'];
178
            foreach ($widgets as $ident => $widget) {
179
                $dashboardConfig['widgets'][$ident]['type'] = 'charcoal/admin/widget/doc';
180
                $dashboardConfig['widgets'][$ident]['show_header'] = true;
181
                $dashboardConfig['widgets'][$ident]['show_title'] = true;
182
            }
183
        }
184
185
        return $dashboardConfig;
186
    }
187
188
    /**
189
     * @throws Exception If the object's admin metadata is not set.
190
     * @return \ArrayAccess
191
     */
192
    private function objAdminMetadata()
193
    {
194
        $obj = $this->obj();
195
196
        $objMetadata = $obj->metadata();
197
198
        $adminMetadata = isset($objMetadata['admin']) ? $objMetadata['admin'] : null;
199
        if ($adminMetadata === null) {
200
            throw new Exception(sprintf(
201
                'The object %s does not have an admin metadata.',
202
                get_class($obj)
203
            ));
204
        }
205
206
        return $adminMetadata;
207
    }
208
}
209