Passed
Pull Request — master (#95)
by Fèvre
22:54 queued 17:29
created

settings()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 3
nop 2
dl 0
loc 21
rs 9.9666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use Illuminate\Database\Eloquent\Model;
6
use Psr\Container\ContainerExceptionInterface;
7
use Psr\Container\NotFoundExceptionInterface;
8
use Xetaravel\Settings\Settings;
9
10
if (! function_exists('settings')) {
11
    /**
12
     * @throws ContainerExceptionInterface
13
     * @throws NotFoundExceptionInterface
14
     */
15
    function settings($key = null, $context = null)
16
    {
17
        $settings = app(Settings::class);
18
19
        // If nothing is passed in to the function, simply return the settings instance.
20
        if ($key === null) {
21
            return $settings;
22
        }
23
24
        // We must reset the context to the default value.
25
        $settings->setContext([
26
            'model_type' => null,
27
            'model_id' => null
28
        ]);
29
30
        // If context is not null, set it.
31
        if ($context instanceof Model || is_array($context)) {
32
            $settings->setContext($context);
33
        }
34
35
        return $settings->get(key: $key);
36
    }
37
}
38