ParlantConfigurator::getGlobal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sanderdekroon\Parlant\Configurator;
4
5
use Sanderdekroon\Parlant\Container;
6
7
class ParlantConfigurator implements ConfiguratorInterface
8
{
9
    /**
10
     * All local settings which will only be applied to the current query.
11
     * @var array
12
     */
13
    protected $local = [];
14
15
    /**
16
     * Global settings that are applied to all queries.
17
     * @var array
18
     */
19
    protected static $global = [
20
        'posts_per_page'    => -1,
21
        'post_type'         => 'any',
22
        'post_status'       => 'publish',
23
        'return'            => 'array',
24
    ];
25
26
    /**
27
     * Get a setting by it's name. Prefers local settings to global settings.
28
     * @param  string $name
29
     * @return mixed
30
     */
31 29
    public function get($name)
32
    {
33 29
        if ($this->hasLocal($name)) {
34 3
            return $this->getLocal($name);
35
        }
36
37 29
        return $this->getGlobal($name);
38
    }
39
40
    /**
41
     * Check if a setting is set in the configurator.
42
     * @param  string  $name
43
     * @return bool
44
     */
45 28
    public function has($name)
46
    {
47 28
        if ($this->hasLocal($name) || $this->hasGlobal($name)) {
48 28
            return true;
49
        }
50
51 28
        return false;
52
    }
53
54
    /**
55
     * Set a (or multiple) global settings.
56
     * @param  string|array $name  Supply an array to add multiple settings.
57
     * @param  mixed        $value
58
     * @return bool
59
     */
60 5
    public static function globally($name, $value = null)
61
    {
62 5
        if (is_array($name)) {
63 5
            return self::addArrayOfGlobalSettings($name);
64
        }
65
66 1
        return self::$global[$name] = $value;
67
    }
68
69
    /**
70
     * Add a local setting.
71
     * @param  string|array $name  Supply an array to add multiple local settings.
72
     * @param  mixed        $value
73
     * @return $this
74
     */
75 3
    public function add($name, $value = null)
76
    {
77 3
        if (is_array($name)) {
78
            return $this->addArrayOfLocalSettings($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->addArrayOfLocalSettings($name) returns the type true which is incompatible with the documented return type Sanderdekroon\Parlant\Co...tor\ParlantConfigurator.
Loading history...
79
        }
80
81 3
        $this->local[$name] = $value;
82 3
        return $this;
83
    }
84
85
    /**
86
     * Add an array of global settings.
87
     * @param array $settings
88
     */
89 5
    protected static function addArrayOfGlobalSettings(array $settings)
90
    {
91 5
        foreach ($settings as $name => $value) {
92 5
            self::$global[$name] = $value;
93
        }
94
95 5
        return true;
96
    }
97
98
    /**
99
     * Add an array of local settings.
100
     * @param array $settings
101
     */
102
    protected function addArrayOfLocalSettings($settings)
103
    {
104
        foreach ($settings as $name => $value) {
105
            $this->add($name, $value);
106
        }
107
108
        return true;
109
    }
110
111
    /**
112
     * Check if a setting exists in the local property.
113
     * @param  string  $name
114
     * @return bool
115
     */
116 29
    protected function hasLocal($name)
117
    {
118 29
        return array_key_exists($name, $this->local);
119
    }
120
121
    /**
122
     * Check if a setting exists in the global settings property.
123
     * @param  string  $name
124
     * @return bool
125
     */
126 28
    protected function hasGlobal($name)
127
    {
128 28
        return array_key_exists($name, self::$global);
129
    }
130
131
    /**
132
     * Get a setting from the local settings property.
133
     * @param  string $name
134
     * @return mixed
135
     */
136 3
    protected function getLocal($name)
137
    {
138 3
        return $this->local[$name];
139
    }
140
141
    /**
142
     * Get a setting from the global settings property.
143
     * @param  string $name
144
     * @return mixed
145
     */
146 29
    protected function getGlobal($name)
147
    {
148 29
        return self::$global[$name];
149
    }
150
151
    /**
152
     * Merge the global and local settings and return the result.
153
     * The local settings override the global settings.
154
     * @return array
155
     */
156
    public function dump()
157
    {
158
        return array_merge(self::$global, $this->local);
159
    }
160
}
161