ConfigAwareTrait::newConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Config;
4
5
/**
6
 * Class ConfigAwareTrait
7
 * @package Nip\Config
8
 */
9
trait ConfigAwareTrait
10
{
11
    /**
12
     * @var Config|Config|null
13
     */
14
    protected $config = null;
15
16
    public function registerContainerConfig()
17
    {
18
        app()->add('config', $this->getConfig(), true);
19
    }
20
21
    /**
22
     * Get the container.
23
     *
24
     * @return Config
25
     */
26
    public function getConfig()
27
    {
28
        if ($this->config == null) {
29
            $this->initConfig();
30
        }
31
32
        return $this->config;
33
    }
34
35
    /**
36
     * Set a container.
37
     *
38
     * @param  Config $config
39
     * @return $this
40
     */
41
    public function setConfig($config)
42
    {
43
        $this->config = $config;
44
45
        return $this;
46
    }
47
48
    public function initConfig()
49
    {
50
        if (app()->has('config')) {
51
            $config = app()->get('config');
52
        } else {
53
            $config = $this->newConfig();
54
        }
55
        $this->setConfig($config);
56
    }
57
58
    /**
59
     * @return Config
60
     */
61
    public function newConfig()
62
    {
63
        return new Config();
64
    }
65
}
66