Passed
Push — master ( 472b4c...8e27d6 )
by Gabriel
04:44
created

HasConfigTrait::generateConfig()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 0
dl 0
loc 14
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\Migrations\Migrator\Traits;
4
5
use ByTIC\Migrations\Config\Config;
6
use ByTIC\Migrations\Utility\Helper;
7
8
/**
9
 * Trait HasConfigTrait
10
 * @package ByTIC\Migrations\Migrator\Traits
11
 */
12
trait HasConfigTrait
13
{
14
    /**
15
     * @var null|Config
16
     */
17
    protected $config = null;
18
19
    /**
20
     * @return Config
21
     */
22 2
    public function getConfig()
23
    {
24 2
        if ($this->config === null) {
25 2
            $this->initConfig();
26
        }
27 2
        return $this->config;
28
    }
29
30
    /**
31
     * @param null $config
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $config is correct as it would always require null to be passed?
Loading history...
32
     */
33 2
    public function setConfig($config)
34
    {
35 2
        $this->config = $config;
36 2
    }
37
38
    /**
39
     * @return string
40
     */
41 1
    public function getCachedConfigPath()
42
    {
43 1
        $path = Helper::normalizePath(Helper::getBasePath(), 'bootstrap', 'cache', 'migrations.php');
44 1
        if (!file_exists($path)) {
45 1
            $this->generateCachedConfig($path);
46
        }
47 1
        return $path;
48
    }
49
50
    /**
51
     * @param $path
52
     */
53 1
    protected function generateCachedConfig($path)
54
    {
55 1
        $this->getConfig()->savePhp($path);
56 1
    }
57
58 2
    protected function initConfig()
59
    {
60 2
        $this->setConfig($this->generateConfig());
61 2
    }
62
63
    /**
64
     * @return Config
65
     */
66 2
    protected function generateConfig()
67
    {
68
        try {
69 2
            if (config()->has('migrations')) {
70
                return Config::fromConfig(config()->get('migrations')->toArray());
71
            }
72 2
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
73
        }
74
75 2
        $config = new Config();
76 2
        $config->initPaths('migrations');
77 2
        $config->initPaths('seeds');
78 2
        $config->addEnviromentFromEnv();
79 2
        return $config;
80
    }
81
}
82