Completed
Push — master ( 60840d...857394 )
by Mahmoud
03:05
created

ConfigsLoaderTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 52
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A runConfigsAutoLoader() 0 5 1
A loadConfigsFromContainers() 0 6 2
A loadConfigsFromPort() 0 7 2
A loadConfigs() 0 15 3
1
<?php
2
3
namespace App\Port\Loader\Loaders;
4
5
use App;
6
use App\Port\Foundation\Portals\Facade\PortButler;
7
use DB;
8
use File;
9
10
/**
11
 * Class ConfigsLoaderTrait.
12
 *
13
 * @author  Mahmoud Zalt <[email protected]>
14
 */
15
trait ConfigsLoaderTrait
16
{
17
18
    /**
19
     * runConfigsAutoLoader
20
     */
21
    public function runConfigsAutoLoader()
22
    {
23
        $this->loadConfigsFromPort();
24
        $this->loadConfigsFromContainers();
25
    }
26
27
    /**
28
     * loadConfigsFromContainers
29
     */
30
    private function loadConfigsFromContainers()
31
    {
32
        foreach (PortButler::getContainersNames() as $containerName) {
33
            $this->loadConfigs(base_path('app/Containers/' . $containerName . '/Configs'));
34
        }
35
    }
36
37
    /**
38
     * loadConfigsFromPort
39
     */
40
    private function loadConfigsFromPort()
41
    {
42
        // $this->portConfigsDirectories is defined on the main service provider class
43
        foreach ($this->portConfigsDirectories as $portConfigsDirectory) {
0 ignored issues
show
Bug introduced by
The property portConfigsDirectories 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...
44
            $this->loadConfigs(base_path('app/Port/') . $portConfigsDirectory);
45
        }
46
    }
47
48
    /**
49
     * @param $directory
50
     */
51
    private function loadConfigs($directory)
52
    {
53
        if (File::isDirectory($directory)) {
54
55
            $files = File::allFiles($directory);
56
57
            foreach ($files as $file) {
58
                // build the key from the file name (just remove the .php extension from the file name)
59
                $fileNameOnly = str_replace('.php', '', $file->getFilename());
60
61
                // merge the config file
62
                $this->mergeConfigFrom($file->getPathname(), $fileNameOnly);
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...
63
            }
64
        }
65
    }
66
}
67