Completed
Push — master ( cb32ca...f4aa28 )
by ARCANEDEV
25s
created

HasConfig   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 34.78%

Importance

Changes 0
Metric Value
dl 0
loc 95
ccs 8
cts 23
cp 0.3478
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigFolder() 0 4 1
A getConfigKey() 0 4 1
A getConfigFile() 0 4 1
A getConfigFileDestination() 0 4 1
A registerConfig() 0 6 2
A registerMultipleConfigs() 0 8 2
A publishConfig() 0 6 1
1
<?php
2
3
namespace Arcanedev\Support\Providers\Concerns;
4
5
use Illuminate\Support\Str;
6
7
/**
8
 * Trait     HasConfig
9
 *
10
 * @package  Arcanedev\Support\Providers\Concerns
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
trait HasConfig
14
{
15
    /* -----------------------------------------------------------------
16
     |  Properties
17
     | -----------------------------------------------------------------
18
     */
19
20
    /**
21
     * Merge multiple config files into one instance (package name as root key)
22
     *
23
     * @var bool
24
     */
25
    protected $multiConfigs = false;
26
27
    /* -----------------------------------------------------------------
28
     |  Main Methods
29
     | -----------------------------------------------------------------
30
     */
31
32
    /**
33
     * Get config folder.
34
     *
35
     * @return string
36
     */
37
    protected function getConfigFolder()
38
    {
39
        return realpath($this->getBasePath().DS.'config');
0 ignored issues
show
Bug introduced by
It seems like getBasePath() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
    }
41
42
    /**
43
     * Get config key.
44
     *
45
     * @return string
46
     */
47 6
    protected function getConfigKey()
48
    {
49 6
        return Str::slug($this->package);
0 ignored issues
show
Bug introduced by
The property package does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
50
    }
51
52
    /**
53
     * Get config file path.
54
     *
55
     * @return string
56
     */
57 6
    protected function getConfigFile()
58
    {
59 6
        return $this->getConfigFolder().DS."{$this->package}.php";
60
    }
61
62
    /**
63
     * Get config file destination path.
64
     *
65
     * @return string
66
     */
67
    protected function getConfigFileDestination()
68
    {
69
        return config_path("{$this->package}.php");
70
    }
71
72
    /**
73
     * Register configs.
74
     *
75
     * @param  string  $separator
76
     */
77 6
    protected function registerConfig($separator = '.')
78
    {
79 6
        $this->multiConfigs
80
            ? $this->registerMultipleConfigs($separator)
81 6
            : $this->mergeConfigFrom($this->getConfigFile(), $this->getConfigKey());
0 ignored issues
show
Bug introduced by
It seems like mergeConfigFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82 6
    }
83
84
    /**
85
     * Register all package configs.
86
     *
87
     * @param  string  $separator
88
     */
89
    private function registerMultipleConfigs($separator = '.')
90
    {
91
        foreach (glob($this->getConfigFolder().'/*.php') as $configPath) {
92
            $this->mergeConfigFrom(
0 ignored issues
show
Bug introduced by
It seems like mergeConfigFrom() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
93
                $configPath, $this->getConfigKey().$separator.basename($configPath, '.php')
94
            );
95
        }
96
    }
97
98
    /**
99
     * Publish the config file.
100
     */
101
    protected function publishConfig()
102
    {
103
        $this->publishes([
0 ignored issues
show
Bug introduced by
It seems like publishes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
104
            $this->getConfigFile() => $this->getConfigFileDestination()
105
        ], 'config');
106
    }
107
}
108