Passed
Push — master ( 28f242...472b4c )
by Gabriel
04:44
created

HasConfigTrait::getCachedConfigPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
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
        $this->initPaths('migrations');
0 ignored issues
show
Bug introduced by
It seems like initPaths() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
        $this->/** @scrutinizer ignore-call */ 
62
               initPaths('migrations');
Loading history...
62 2
        $this->initPaths('seeds');
63 2
        $this->addEnviromentFromEnv();
0 ignored issues
show
Bug introduced by
It seems like addEnviromentFromEnv() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        $this->/** @scrutinizer ignore-call */ 
64
               addEnviromentFromEnv();
Loading history...
64 2
    }
65
66
    /**
67
     * @return Config
68
     */
69 2
    protected function generateConfig()
70
    {
71 2
        $config = new Config();
72 2
        return $config;
73
    }
74
}
75