Completed
Push — master ( b9dd45...d03d4d )
by Mahmoud
03:06
created

ProvidersLoaderTrait::loadProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace App\Port\Provider\Loaders;
4
5
use App;
6
use App\Port\Butler\Portals\Facade\PortButler;
7
use DB;
8
use File;
9
use Log;
10
11
/**
12
 * Class ProvidersLoaderTrait.
13
 *
14
 * @author  Mahmoud Zalt <[email protected]>
15
 */
16
trait ProvidersLoaderTrait
17
{
18
19
    protected $portProvidersDirectories = [
20
21
    ];
22
23
    public function runProvidersAutoLoader()
24
    {
25
        $this->loadProvidersFromPort();
26
        $this->loadProvidersFromContainers();
27
    }
28
29
    private function loadProvidersFromContainers()
30
    {
31
        $mainServiceProviderNameStartWith = 'Main';
32
33 View Code Duplication
        foreach (PortButler::getContainersNames() as $containerName) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
35
            $containerProvidersDirectory = base_path('app/Containers/' . $containerName . '/Providers');
36
37
            if (File::isDirectory($containerProvidersDirectory)) {
38
39
                $files = \File::allFiles($containerProvidersDirectory);
40
41
                foreach ($files as $file) {
42
43
                    if (\File::isFile($file)) {
44
45
                        // Check if this is the Main Service Provider
46
                        if (PortButler::stringStartsWith($file->getFilename(), $mainServiceProviderNameStartWith)) {
47
                            $serviceProviderClass = PortButler::getClassFullNameFromFile($file->getPathname());
48
49
                            $this->loadProvider($serviceProviderClass);
50
                        }
51
                    }
52
                }
53
            }
54
        }
55
    }
56
57
    private function loadProvidersFromPort()
58
    {
59
        foreach ($this->serviceProviders as $providerClass) {
0 ignored issues
show
Bug introduced by
The property serviceProviders 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...
60
            $this->loadProvider($providerClass);
61
        }
62
    }
63
64
    private function loadProvider($provider)
65
    {
66
        App::register($provider);
67
    }
68
69
    public function loadContainersInternalProviders(Array $providers = [])
70
    {
71
        foreach ($providers as $provider) {
72
            $this->loadProvider($provider);
73
        }
74
    }
75
}
76