Passed
Pull Request — master (#276)
by
unknown
13:05
created

Config   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 1

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isPolymorphic() 0 4 1
A isStrict() 0 4 1
A isTranslated() 0 4 1
A getNotifiedModel() 0 8 2
A getNotifiedModelOwnerKey() 0 4 1
A getAdditionalFields() 0 4 1
A getAdditionalRequiredFields() 0 4 1
A getTranslationDomain() 0 4 1
A get() 0 4 1
A has() 0 4 1
A set() 0 5 1
A reload() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
1
<?php
2
3
namespace Fenos\Notifynder\Collections;
4
5
use Illuminate\Support\Arr;
6
use InvalidArgumentException;
7
use Fenos\Notifynder\Contracts\ConfigContract;
8
9
/**
10
 * Class Config.
11
 */
12
class Config implements ConfigContract
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $items;
18
19
    /**
20
     * Config constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->reload();
25
    }
26
27
    /**
28
     * @return bool
29
     */
30
    public function isPolymorphic()
31
    {
32
        return (bool) $this->get('polymorphic');
33
    }
34
35
    /**
36
     * @return bool
37
     */
38
    public function isStrict()
39
    {
40
        return (bool) $this->get('strict_extra');
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    public function isTranslated()
47
    {
48
        return (bool) $this->get('translation.enabled');
49
    }
50
51
    /**
52
     * @return string
53
     * @throws InvalidArgumentException
54
     */
55
    public function getNotifiedModel()
56
    {
57
        $class = $this->get('model');
58
        if (class_exists($class)) {
59
            return $class;
60
        }
61
        throw new InvalidArgumentException("The model class [{$class}] doesn't exist.");
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getNotifiedModelOwnerKey()
68
    {
69
        return (string) $this->get('model_owner_key', 'id');
70
    }
71
72
    /**
73
     * @return array
74
     */
75
    public function getAdditionalFields()
76
    {
77
        return Arr::flatten($this->get('additional_fields', []));
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function getAdditionalRequiredFields()
84
    {
85
        return Arr::flatten($this->get('additional_fields.required', []));
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getTranslationDomain()
92
    {
93
        return $this->get('translation.domain', 'notifynder');
94
    }
95
96
    /**
97
     * @param string $key
98
     * @param null|mixed $default
99
     * @return mixed
100
     */
101
    public function get($key, $default = null)
102
    {
103
        return Arr::get($this->items, $key, $default);
104
    }
105
106
    /**
107
     * @param string $key
108
     * @return bool
109
     */
110
    public function has($key)
111
    {
112
        return Arr::has($this->items, $key);
113
    }
114
115
    /**
116
     * @param string $key
117
     * @param null $value
118
     */
119
    public function set($key, $value = null)
120
    {
121
        Arr::set($this->items, $key, $value);
122
        app('config')->set('notifynder.'.$key, $value);
123
    }
124
125
    public function reload()
126
    {
127
        $this->items = app('config')->get('notifynder');
128
    }
129
130
    /**
131
     * @param string $key
132
     * @return mixed
133
     */
134
    public function __get($key)
135
    {
136
        return $this->get($key);
137
    }
138
139
    /**
140
     * @param string $key
141
     * @param mixed $value
142
     */
143
    public function __set($key, $value)
144
    {
145
        $this->set($key, $value);
146
    }
147
}
148