ConfigurationRepository::findByKeyAndScopeOrNew()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
1
<?php namespace Anomaly\ConfigurationModule\Configuration;
2
3
use Anomaly\ConfigurationModule\Configuration\Contract\ConfigurationInterface;
4
use Anomaly\ConfigurationModule\Configuration\Contract\ConfigurationRepositoryInterface;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypeCollection;
6
use Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter;
7
use Anomaly\Streams\Platform\Entry\EntryRepository;
8
use Illuminate\Config\Repository;
9
10
/**
11
 * Class ConfigurationRepositoryInterface
12
 *
13
 * @link          http://pyrocms.com/
14
 * @author        PyroCMS, Inc. <[email protected]>
15
 * @author        Ryan Thompson <[email protected]>
16
 */
17
class ConfigurationRepository extends EntryRepository implements ConfigurationRepositoryInterface
18
{
19
20
    /**
21
     * The configuration model.
22
     *
23
     * @var ConfigurationModel
24
     */
25
    protected $model;
26
27
    /**
28
     * The configurations collection.
29
     *
30
     * @var ConfigurationCollection
31
     */
32
    protected $configurations;
33
34
    /**
35
     * Create a new ConfigurationRepositoryInterface instance.
36
     *
37
     * @param ConfigurationModel  $model
38
     * @param Repository          $config
0 ignored issues
show
Bug introduced by
There is no parameter named $config. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
39
     * @param FieldTypeCollection $fieldTypes
0 ignored issues
show
Bug introduced by
There is no parameter named $fieldTypes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     */
41
    public function __construct(ConfigurationModel $model)
42
    {
43
        $this->model = $model;
44
45
        $this->configurations = $this->model->all();
46
    }
47
48
    /**
49
     * Get a configuration.
50
     *
51
     * @param $key
52
     * @param $scope
53
     * @return ConfigurationInterface|null
54
     */
55
    public function get($key, $scope)
56
    {
57
        return $this->configurations->get($key . $scope);
58
    }
59
60
    /**
61
     * Set a configurations value.
62
     *
63
     * @param $key
64
     * @param $scope
65
     * @param $value
66
     * @return bool
67
     */
68
    public function set($key, $scope, $value)
69
    {
70
        $configuration = $this->findByKeyAndScopeOrNew($key, $scope);
71
72
        $configuration->setValue($value);
73
74
        return $this->save($configuration);
75
    }
76
77
    /**
78
     * Get a configuration value.
79
     *
80
     * @param             $key
81
     * @param             $scope
82
     * @param  null       $default
83
     * @return mixed|null
84
     */
85
    public function value($key, $scope, $default = null)
86
    {
87
        if ($configuration = $this->get($key, $scope)) {
88
            return $configuration->getValue();
89
        }
90
91
        return $default;
92
    }
93
94
    /**
95
     * Get a configuration value presenter instance.
96
     *
97
     * @param $key
98
     * @param $scope
99
     * @return FieldTypePresenter|null
100
     */
101
    public function presenter($key, $scope)
102
    {
103
        if ($configuration = $this->get($key, $scope)) {
104
            return $configuration->getFieldTypePresenter('value');
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * Find a configuration by it's key
112
     * or return a new instance.
113
     *
114
     * @param $key
115
     * @param $scope
116
     * @return ConfigurationInterface
117
     */
118
    public function findByKeyAndScopeOrNew($key, $scope)
119
    {
120
        if (!$configuration = $this->model->where('key', $key)->where('scope', $scope)->first()) {
121
122
            $configuration = $this->model->newInstance();
123
124
            $configuration->setKey($key);
125
            $configuration->setScope($scope);
126
        }
127
128
        return $configuration;
129
    }
130
131
    /**
132
     * Purge a namespace's configuration.
133
     *
134
     * @param $namespace
135
     * @return $this
136
     */
137
    public function purge($namespace)
138
    {
139
        $this->model->where('key', 'LIKE', $namespace . '::%')->delete();
140
141
        return $this;
142
    }
143
}
144