Completed
Push — master ( 0f23dc...93fd79 )
by Ryan
02:08
created

ConfigurationRepository::purge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
 * @package       Anomaly\ConfigurationModule\ConfigurationInterface
17
 */
18
class ConfigurationRepository extends EntryRepository implements ConfigurationRepositoryInterface
19
{
20
21
    /**
22
     * The configuration model.
23
     *
24
     * @var ConfigurationModel
25
     */
26
    protected $model;
27
28
    /**
29
     * The configurations collection.
30
     *
31
     * @var ConfigurationCollection
32
     */
33
    protected $configurations;
34
35
    /**
36
     * Create a new ConfigurationRepositoryInterface instance.
37
     *
38
     * @param ConfigurationModel  $model
39
     * @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...
40
     * @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...
41
     */
42
    public function __construct(ConfigurationModel $model)
43
    {
44
        $this->model = $model;
45
46
        $this->configurations = $this->model->all();
47
    }
48
49
    /**
50
     * Get a configuration.
51
     *
52
     * @param $key
53
     * @param $scope
54
     * @return ConfigurationInterface|null
55
     */
56
    public function get($key, $scope)
57
    {
58
        return $this->configurations->get($key . $scope);
59
    }
60
61
    /**
62
     * Set a configurations value.
63
     *
64
     * @param $key
65
     * @param $scope
66
     * @param $value
67
     * @return bool
68
     */
69
    public function set($key, $scope, $value)
70
    {
71
        $configuration = $this->findByKeyAndScopeOrNew($key, $scope);
72
73
        $configuration->setValue($value);
74
75
        return $this->save($configuration);
76
    }
77
78
    /**
79
     * Get a configuration value.
80
     *
81
     * @param      $key
82
     * @param      $scope
83
     * @param null $default
84
     * @return mixed|null
85
     */
86
    public function value($key, $scope, $default = null)
87
    {
88
        if ($configuration = $this->get($key, $scope)) {
89
            return $configuration->getValue();
90
        }
91
92
        return $default;
93
    }
94
95
    /**
96
     * Get a configuration value presenter instance.
97
     *
98
     * @param $key
99
     * @param $scope
100
     * @return FieldTypePresenter|null
101
     */
102
    public function presenter($key, $scope)
103
    {
104
        if ($configuration = $this->get($key, $scope)) {
105
            return $configuration->getFieldTypePresenter('value');
106
        }
107
108
        return null;
109
    }
110
111
    /**
112
     * Find a configuration by it's key
113
     * or return a new instance.
114
     *
115
     * @param $key
116
     * @param $scope
117
     * @return ConfigurationInterface
118
     */
119
    public function findByKeyAndScopeOrNew($key, $scope)
120
    {
121
        if (!$configuration = $this->model->where('key', $key)->where('scope', $scope)->first()) {
122
123
            $configuration = $this->model->newInstance();
124
125
            $configuration->setKey($key);
126
            $configuration->setScope($scope);
127
        }
128
129
        return $configuration;
130
    }
131
132
    /**
133
     * Purge a namespace's configuration.
134
     *
135
     * @param $namespace
136
     * @return $this
137
     */
138
    public function purge($namespace)
139
    {
140
        $this->model->where('key', 'LIKE', $namespace . '::%')->delete();
141
142
        return $this;
143
    }
144
}
145