GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — analysis-XlNZW9 ( b40860 )
by butschster
07:34 queued 18s
created

DisplayTabbed::isDisplayed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SleepingOwl\Admin\Display;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
use Illuminate\Database\Eloquent\Model;
8
use SleepingOwl\Admin\Traits\FormElements;
9
use Illuminate\Contracts\Support\Renderable;
10
use SleepingOwl\Admin\Traits\VisibleCondition;
11
use SleepingOwl\Admin\Contracts\Form\FormInterface;
12
use SleepingOwl\Admin\Contracts\Display\TabInterface;
13
use SleepingOwl\Admin\Contracts\Display\DisplayInterface;
14
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface;
15
16
/**
17
 * @property TabInterface[]|DisplayTabsCollection $elements
18
 */
19
class DisplayTabbed implements DisplayInterface, FormInterface
20
{
21
    use FormElements, VisibleCondition, \SleepingOwl\Admin\Traits\Renderable;
22
23
    /**
24
     * @var string
25
     */
26
    protected $view = 'display.tabbed';
27
28
    /**
29
     * DisplayTabbed constructor.
30
     *
31
     * @param Closure|TabInterface[] $tabs
32
     */
33
    public function __construct($tabs = null)
34
    {
35
        $this->elements = new DisplayTabsCollection();
36
37
        if (is_array($tabs) || is_callable($tabs)) {
38
            $this->setTabs($tabs);
39
        }
40
    }
41
42
    /**
43
     * Initialize tabbed interface.
44
     */
45
    public function initialize()
46
    {
47
        $this->initializeElements();
48
49
        $tabs = $this->getTabs();
50
51
        foreach ($tabs as $tab) {
52
            if (method_exists($tab->getContent(), 'getElements')) {
53
                $elements = $tab->getContent()->getElements();
54
                foreach ($elements as $element) {
55
                    if ($element instanceof self) {
56
                        foreach ($element->getTabs() as $subTab) {
57
                            if ($subTab->getName() == session('sleeping_owl_tab_id')) {
58
                                $tab->setActive(true);
59
                                $subTab->setActive(true);
60
                            }
61
                        }
62
                    }
63
                }
64
            }
65
66
            if ($tab->getName() == session('sleeping_owl_tab_id')) {
67
                $tab->setActive(true);
68
            }
69
        }
70
71
        $activeTabs = $this->getTabs()->filter(function (TabInterface $tab) {
72
            return $tab->isActive();
73
        })->count();
74
75
        if ($activeTabs === 0 && $firstTab = $this->getTabs()->first()) {
76
            $firstTab->setActive(true);
77
        }
78
    }
79
80
    /**
81
     * @return Model $model|null
82
     */
83
    public function getModel()
84
    {
85
        foreach ($this->getTabs() as $tab) {
86
            if ($tab->getContent() instanceof FormInterface) {
87
                return $tab->getContent()->getModel();
88
            }
89
        }
90
    }
91
92
    /**
93
     * @param string $class
94
     *
95
     * @return $this
96
     */
97
    public function setModelClass($class)
98
    {
99
        $this->getTabs()->each(function (TabInterface $tab) use ($class) {
100
            if ($tab instanceof DisplayInterface) {
101
                $tab->setModelClass($class);
102
            }
103
        });
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return TabInterface[]|\SleepingOwl\Admin\Form\FormElementsCollection
110
     */
111
    public function getTabs()
112
    {
113
        return $this->getElements();
114
    }
115
116
    /**
117
     * @param Closure|TabInterface[] $tabs
118
     *
119
     * @return $this
120
     */
121
    public function setTabs($tabs)
122
    {
123
        if (is_callable($tabs)) {
124
            $tabs = call_user_func($tabs, $this);
125
        }
126
127
        return $this->setElements($tabs);
128
    }
129
130
    /**
131
     * @param array $elements
132
     *
133
     * @return $this
134
     */
135
    public function setElements(array $elements)
136
    {
137
        foreach ($elements as $label => $tab) {
138
            if ($tab instanceof TabInterface) {
139
                $this->addElement($tab);
140
            } else {
141
                $this->appendTab($tab, $label);
142
            }
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param Renderable $display
150
     * @param string $label
151
     * @param bool|false $active
152
     *
153
     * @return TabInterface
154
     */
155
    public function appendTab(Renderable $display, $label, $active = false)
156
    {
157
        $this->addElement(
158
            $tab = app('sleeping_owl.display')->tab($display, $label)->setActive($active)
0 ignored issues
show
Bug introduced by
The method tab() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

158
            $tab = app('sleeping_owl.display')->/** @scrutinizer ignore-call */ tab($display, $label)->setActive($active)

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...
159
        );
160
161
        return $tab;
162
    }
163
164
    /**
165
     * @param TabInterface $element
166
     *
167
     * @return $this
168
     */
169
    public function addElement(TabInterface $element)
170
    {
171
        $this->elements->push($element);
172
173
        return $this;
174
    }
175
176
    /**
177
     * @param string $action
178
     *
179
     * @return $this
180
     */
181
    public function setAction($action)
182
    {
183
        $this->getTabs()->each(function (TabInterface $tab) use ($action) {
184
            if ($tab instanceof FormInterface) {
185
                $tab->setAction($action);
186
            }
187
        });
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param int $id
194
     *
195
     * @return $this
196
     */
197
    public function setId($id)
198
    {
199
        $this->getTabs()->each(function (TabInterface $tab) use ($id) {
200
            if ($tab instanceof FormInterface) {
201
                $tab->setId($id);
202
            }
203
        });
204
205
        return $this;
206
    }
207
208
    /**
209
     * @param \Illuminate\Http\Request $request
210
     * @param ModelConfigurationInterface $model
211
     *
212
     * @return void
213
     */
214
    public function validateForm(Request $request, ModelConfigurationInterface $model = null)
215
    {
216
        $this->getTabs()->each(function ($tab) use ($request, $model) {
217
            $tabId = $request->get('sleeping_owl_tab_id');
218
219
            if ($tab instanceof FormInterface && $tab->getName() == $tabId) {
0 ignored issues
show
Bug introduced by
The method getName() does not exist on SleepingOwl\Admin\Contracts\Form\FormInterface. It seems like you code against a sub-type of SleepingOwl\Admin\Contracts\Form\FormInterface such as SleepingOwl\Admin\Display\DisplayTab. ( Ignorable by Annotation )

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

219
            if ($tab instanceof FormInterface && $tab->/** @scrutinizer ignore-call */ getName() == $tabId) {
Loading history...
220
                $tab->validateForm($request, $model);
221
            }
222
        });
223
    }
224
225
    /**
226
     * @param \Illuminate\Http\Request $request
227
     * @param ModelConfigurationInterface $model
228
     *
229
     * @return void
230
     */
231
    public function saveForm(Request $request, ModelConfigurationInterface $model = null)
232
    {
233
        $this->getTabs()->each(function (TabInterface $tab) use ($request, $model) {
234
            $tabId = $request->get('sleeping_owl_tab_id');
235
236
            if ($tab instanceof FormInterface && $tab->getName() == $tabId) {
237
                $tab->saveForm($request, $model);
238
            }
239
        });
240
    }
241
242
    /**
243
     * @return null
244
     */
245
    public function getValue()
246
    {
247
    }
248
249
    /**
250
     * @return bool
251
     */
252
    public function isReadonly()
253
    {
254
        return false;
255
    }
256
257
    /**
258
     * @return bool
259
     */
260
    public function isDisplayed()
261
    {
262
        return true;
263
    }
264
265
    /**
266
     * @return bool
267
     */
268
    public function isValueSkipped()
269
    {
270
        return false;
271
    }
272
273
    /**
274
     * @return array
275
     */
276
    public function toArray()
277
    {
278
        return [
279
            'tabs' => $this->getTabs()->onlyVisible(),
280
        ];
281
    }
282
283
    /**
284
     * Using in trait FormElements;.
285
     *
286
     * @param $object
287
     *
288
     * @return mixed
289
     */
290
    protected function getElementContainer($object)
291
    {
292
        return $object->getContent();
293
    }
294
}
295