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 — merge-dev-to-master ( fdc6fe )
by
unknown
11:18
created

MultiSelectAjax::setSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SleepingOwl\Admin\Form\Element;
4
5
use AdminSection;
0 ignored issues
show
Bug introduced by
The type AdminSection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Support\Arr;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Router;
9
use Illuminate\Database\Eloquent\Builder;
10
use Illuminate\Database\Eloquent\Collection;
11
use SleepingOwl\Admin\Contracts\Initializable;
12
use SleepingOwl\Admin\Traits\SelectAjaxFunctions;
13
use SleepingOwl\Admin\Contracts\WithRoutesInterface;
14
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
15
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
16
use SleepingOwl\Admin\Contracts\Repositories\RepositoryInterface;
17
18
class MultiSelectAjax extends MultiSelect implements Initializable, WithRoutesInterface
19
{
20
    use SelectAjaxFunctions;
21
22
    protected static $route = 'multiselectajax';
23
    protected $view = 'form.element.selectajax';
24
25
    /**
26
     * @var string|null
27
     */
28
    protected $language;
29
30
    /**
31
     * MultiSelectAjax constructor.
32
     * @param $path
33
     * @param null $label
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $label is correct as it would always require null to be passed?
Loading history...
34
     * @throws \SleepingOwl\Admin\Exceptions\Form\Element\SelectException
35
     * @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException
36
     */
37
    public function __construct($path, $label = null)
38
    {
39
        parent::__construct($path, $label);
40
41
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
42
        // This code add closure, that modify query so, that select will have only one value.
43
        // This feature need for optimized selectajax initiation for also existing records.
44
        // Make some changes for availability to use setLoadOptionsQueryPreparer() in Sections.
45
        $this->setLoadOptionsQueryPreparer(function ($item, Builder $query) {
46
            $repository = app(RepositoryInterface::class);
47
            $repository->setModel($this->getModelForOptions());
48
            $key = $repository->getModel()->getKeyName();
49
50
            return $query->whereIn($key, $this->getValueFromModel() ? $this->getValueFromModel() : []);
51
        });
52
        */
53
54
        $this->default_query_preparer = function ($item, Builder $query) {
55
            $repository = app(RepositoryInterface::class);
56
            $repository->setModel($this->getModelForOptions());
57
            $key = $repository->getModel()->getKeyName();
58
59
            return $query->whereIn($key, $this->getValueFromModel() ? $this->getValueFromModel() : []);
60
        };
61
62
        $this->setLanguage(config('app.locale'));
63
    }
64
65
    /**
66
     * @return string|null
67
     */
68
    public function getLanguage()
69
    {
70
        return $this->language;
71
    }
72
73
    /**
74
     * @param $language
75
     * @return $this
76
     */
77
    public function setLanguage($language)
78
    {
79
        $this->language = $language;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param Router $router
86
     */
87
    public static function registerRoutes(Router $router)
88
    {
89
        $routeName = 'admin.form.element.'.static::$route;
90
91
        if (! $router->has($routeName)) {
92
            $router->post('{adminModel}/'.static::$route.'/{field}/{id?}', [
93
                'as' => $routeName,
94
                'uses' => 'SleepingOwl\Admin\Http\Controllers\FormElementController@multiselectSearch',
95
            ]);
96
        }
97
    }
98
99
    /**
100
     * Getter of search url.
101
     * @return string
102
     */
103
    public function getSearchUrl()
104
    {
105
        return $this->search_url ? $this->search_url : route('admin.form.element.'.static::$route, [
106
            'adminModel' => AdminSection::getModel($this->model)->getAlias(),
107
            'field' => $this->getFieldName(),
108
            'id' => $this->model->getKey(),
109
        ]);
110
    }
111
112
    /**
113
     * Search url for ajax.
114
     * @param $url
115
     * @return $this
116
     */
117
    public function setSearchUrl($url)
118
    {
119
        $this->search_url = $url;
120
121
        return $this;
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function toArray()
128
    {
129
        $this->setLoadOptionsQueryPreparer($this->default_query_preparer);
130
131
        $this->setHtmlAttributes([
132
            'id' => $this->getId(),
133
            'class' => 'form-control js-data-ajax',
134
            'multiple',
135
            //'model' => get_class($this->getModelForOptions()),
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
136
            //'field' => $this->getDisplay(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
            //'search' => $this->getSearch(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
            'search_url' => $this->getSearchUrl(),
139
            'data-min-symbols' => $this->getMinSymbols(),
140
        ]);
141
142
        if ($this->getDataDepends() != '[]') {
143
            $this->setHtmlAttributes([
144
                'data-language' => $this->getLanguage(),
145
                'data-depends' => $this->getDataDepends(),
146
                'data-url' => $this->getSearchUrl(),
147
                'class' => 'input-select input-select-dependent',
148
            ]);
149
        }
150
151
        return ['attributes' => $this->getHtmlAttributes()] + parent::toArray();
152
    }
153
154
    /**
155
     * Get Field name for search url.
156
     * @return mixed
157
     */
158
    public function getFieldName()
159
    {
160
        return str_replace('[]', '', $this->getName());
161
    }
162
163
    /**
164
     * @return array
165
     */
166
    protected function loadOptions()
167
    {
168
        $repository = app(RepositoryInterface::class);
169
        $repository->setModel($this->getModelForOptions());
170
        $key = $repository->getModel()->getKeyName();
171
172
        $options = $repository->getQuery();
173
        $relation = $this->getModelAttributeKey();
174
175
        if ($this->isEmptyRelation() && ! is_null($foreignKey = $this->getForeignKey())) {
176
            $model = $this->getModel();
177
178
            if ($model->{$relation}() instanceof HasOneOrMany) {
179
                $options->where($foreignKey, 0)->orWhereNull($foreignKey);
180
            }
181
        }
182
183
        if (count($this->getFetchColumns()) > 0) {
184
            $options->select(
185
                array_merge([$key], $this->getFetchColumns())
186
            );
187
        }
188
189
        // call the pre load options query preparer if has be set
190
        if (! is_null($preparer = $this->getLoadOptionsQueryPreparer())) {
0 ignored issues
show
introduced by
The condition is_null($preparer = $thi...OptionsQueryPreparer()) is always false.
Loading history...
191
            $options = $preparer($this, $options);
192
        }
193
194
        if (method_exists($this->getModel(), $relation) && $this->getModel()->{$relation}() instanceof BelongsToMany) {
195
            $options = $this->getModel()->{$relation}();
196
        }
197
198
        $options = $options->get();
199
200
        if (is_callable($makeDisplay = $this->getDisplay())) {
201
            // make dynamic display text
202
            if ($options instanceof Collection) {
203
                $options = $options->all();
204
            }
205
206
            // iterate for all options and redefine it as
207
            // list of KEY and TEXT pair
208
            $options = array_map(function ($opt) use ($key, $makeDisplay) {
209
                // get the KEY and make the display text
210
                return [data_get($opt, $key), $makeDisplay($opt)];
211
            }, $options);
212
213
            // take options as array with KEY => VALUE pair
214
            $options = Arr::pluck($options, 1, 0);
215
        } elseif ($options instanceof Collection) {
216
            // take options as array with KEY => VALUE pair
217
            $options = Arr::pluck($options->all(), $this->getDisplay(), $key);
218
        } else {
219
            // take options as array with KEY => VALUE pair
220
            $options = Arr::pluck($options, $this->getDisplay(), $key);
221
        }
222
223
        return $options;
224
    }
225
226
    /**
227
     * @param Request $request
228
     *
229
     * @return void
230
     */
231
    public function afterSave(Request $request)
232
    {
233
        $attribute = $this->getModelAttributeKey();
234
235
        if (! method_exists($this->getModel(), $attribute)) {
236
            return;
237
        }
238
239
        parent::afterSave($request);
240
    }
241
}
242