Completed
Push — master ( 184560...9805df )
by ARCANEDEV
21s queued 18s
created

HasConfig::publishConfig()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 6
ccs 0
cts 5
cp 0
c 0
b 0
f 0
cc 2
nop 1
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Support\Providers\Concerns;
6
7
use Illuminate\Support\Str;
8
9
/**
10
 * Trait     HasConfig
11
 *
12
 * @package  Arcanedev\Support\Providers\Concerns
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
trait HasConfig
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Merge multiple config files into one instance (package name as root key)
24
     *
25
     * @var bool
26
     */
27
    protected $multiConfigs = false;
28
29
    /* -----------------------------------------------------------------
30
     |  Main Methods
31
     | -----------------------------------------------------------------
32
     */
33
34
    /**
35
     * Get config folder.
36
     *
37
     * @return string
38
     */
39
    protected function getConfigFolder(): string
40
    {
41
        return realpath($this->getBasePath().DIRECTORY_SEPARATOR.'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...
42
    }
43
44
    /**
45
     * Get config key.
46
     *
47
     * @return string
48
     */
49 18
    protected function getConfigKey(): string
50
    {
51 18
        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...
52
    }
53
54
    /**
55
     * Get config file path.
56
     *
57
     * @return string
58
     */
59 18
    protected function getConfigFile(): string
60
    {
61 18
        return $this->getConfigFolder().DIRECTORY_SEPARATOR."{$this->package}.php";
62
    }
63
64
    /**
65
     * Register configs.
66
     *
67
     * @param  string  $separator
68
     */
69 18
    protected function registerConfig(string $separator = '.'): void
70
    {
71 18
        $this->multiConfigs
72
            ? $this->registerMultipleConfigs($separator)
73 18
            : $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...
74 18
    }
75
76
    /**
77
     * Register all package configs.
78
     *
79
     * @param  string  $separator
80
     */
81
    private function registerMultipleConfigs(string $separator = '.'): void
82
    {
83
        foreach (glob($this->getConfigFolder().'/*.php') as $configPath) {
84
            $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...
85
                $configPath, $this->getConfigKey().$separator.basename($configPath, '.php')
86
            );
87
        }
88
    }
89
90
    /**
91
     * Publish the config file.
92
     *
93
     * @param  string|null  $path
94
     */
95
    protected function publishConfig(?string $path = null): void
96
    {
97
        $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...
98
            $this->getConfigFile() => $path ?: config_path("{$this->package}.php"),
99
        ], [$this->package, 'config', "{$this->package}-config"]);
100
    }
101
}
102