Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Push — feature-deny-all-access ( a51d43 )
by
unknown
11:54
created

Settings   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 158
rs 10
c 1
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 3 1
A get() 0 3 1
A operationSetting() 0 5 1
A setting() 0 7 2
A getOperationSetting() 0 5 1
A setOperationSetting() 0 5 1
A loadDefaultOperationSettingsFromConfig() 0 9 4
A has() 0 7 2
A hasOperationSetting() 0 5 1
A settings() 0 4 1
A getAvailableOperationsList() 0 8 1
1
<?php
2
3
namespace Backpack\CRUD\app\Library\CrudPanel\Traits;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Key-value store for operations.
9
 */
10
trait Settings
11
{
12
    protected $settings = [];
13
14
    /**
15
     * Getter for the settings key-value store.
16
     *
17
     * @param  string  $key  Usually operation.name (ex: list.exportButtons)
18
     * @return mixed [description]
19
     */
20
    public function get(string $key)
21
    {
22
        return $this->settings[$key] ?? null;
23
    }
24
25
    /**
26
     * Setter for the settings key-value store.
27
     *
28
     * @param  string  $key  Usually operation.name (ex: reorder.max_level)
29
     * @param  mixed  $value  The value you want to store.
30
     * @return mixed Setting value.
31
     */
32
    public function set(string $key, $value)
33
    {
34
        return $this->settings[$key] = $value;
35
    }
36
37
    /**
38
     * Check if the settings key is used (has a value).
39
     *
40
     * @param  string  $key  Usually operation.name (ex: reorder.max_level)
41
     * @return bool
42
     */
43
    public function has(string $key)
44
    {
45
        if (isset($this->settings[$key])) {
46
            return true;
47
        }
48
49
        return false;
50
    }
51
52
    /**
53
     * Get all operation settings, ordered by key.
54
     *
55
     * @return array
56
     */
57
    public function settings()
58
    {
59
        return Arr::sort($this->settings, function ($value, $key) {
60
            return $key;
61
        });
62
    }
63
64
    /**
65
     * Getter and setter for the settings key-value store.
66
     *
67
     * @param  string  $key  Usually operation.name (ex: list.exportButtons)
68
     * @param  mixed  $value  The value you want to store.
69
     * @return mixed Setting value for setter. True/false for getter.
70
     */
71
    public function setting(string $key, $value = null)
72
    {
73
        if ($value === null) {
74
            return $this->get($key);
75
        }
76
77
        return $this->set($key, $value);
78
    }
79
80
    /**
81
     * Convenience method for getting or setting a key on the current operation.
82
     *
83
     * @param  string  $key  Has no operation prepended. (ex: exportButtons)
84
     * @param  mixed  $value  The value you want to store.
85
     * @return mixed Setting value for setter. True/false for getter.
86
     */
87
    public function operationSetting(string $key, $value = null, $operation = null)
88
    {
89
        $operation = $operation ?? $this->getCurrentOperation();
0 ignored issues
show
Bug introduced by
It seems like getCurrentOperation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

89
        $operation = $operation ?? $this->/** @scrutinizer ignore-call */ getCurrentOperation();
Loading history...
90
91
        return $this->setting($operation.'.'.$key, $value);
92
    }
93
94
    /**
95
     * Getter for the settings key-value store on a certain operation.
96
     * Defaults to the current operation.
97
     *
98
     * @param  string  $key  Has no operation prepended. (ex: exportButtons)
99
     * @return mixed [description]
100
     */
101
    public function getOperationSetting(string $key, $operation = null)
102
    {
103
        $operation = $operation ?? $this->getCurrentOperation();
104
105
        return $this->get($operation.'.'.$key) ?? config('backpack.crud.operations.'.$this->getCurrentOperation().'.'.$key) ?? null;
106
    }
107
108
    /**
109
     * Check if the settings key is used (has a value).
110
     * Defaults to the current operation.
111
     *
112
     * @param  string  $key  Has no operation prepended. (ex: exportButtons)
113
     * @return mixed [description]
114
     */
115
    public function hasOperationSetting(string $key, $operation = null)
116
    {
117
        $operation = $operation ?? $this->getCurrentOperation();
118
119
        return $this->has($operation.'.'.$key);
120
    }
121
122
    /**
123
     * Setter for the settings key-value store for a certain operation.
124
     * Defaults to the current operation.
125
     *
126
     * @param  string  $key  Has no operation prepended. (ex: max_level)
127
     * @param  bool  $value  True/false depending on success.
128
     */
129
    public function setOperationSetting(string $key, $value, $operation = null)
130
    {
131
        $operation = $operation ?? $this->getCurrentOperation();
132
133
        return $this->set($operation.'.'.$key, $value);
134
    }
135
136
    /**
137
     * Automatically set values in config file (config/backpack/crud)
138
     * as settings values for that operation.
139
     *
140
     * @param  string  $configPath  Config string that leads to where the configs are stored.
141
     */
142
    public function loadDefaultOperationSettingsFromConfig($configPath = null)
143
    {
144
        $operation = $this->getCurrentOperation();
145
        $configPath = $configPath ?? 'backpack.operations.'.$operation;
146
        $configSettings = config($configPath);
147
148
        if (is_array($configSettings) && count($configSettings)) {
149
            foreach ($configSettings as $key => $value) {
150
                $this->setOperationSetting($key, $value);
151
            }
152
        }
153
    }
154
155
    /**
156
     * Get the current CRUD operations loaded.
157
     *
158
     * @return array operaiton names
159
     */
160
    public function getAvailableOperationsList(): array
161
    {
162
        return collect($this->settings)
0 ignored issues
show
Bug introduced by
$this->settings of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

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

162
        return collect(/** @scrutinizer ignore-type */ $this->settings)
Loading history...
163
            ->keys()
164
            ->filter(fn(string $key): bool => str_contains($key, '.access'))
165
            ->map(fn(string $key): string => str_replace('.access', '', $key))
166
            ->values()
167
            ->toArray();
168
    }
169
}
170