Completed
Push — master ( bec27e...637f9e )
by Mahmoud
03:05
created

SeederLoaderAbstract   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 53.95 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 41
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 5 1
A loadSeedersFromContainers() 9 9 2
A loadSeedersFromPort() 11 11 2
A loadSeeds() 21 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace App\Port\Loader\Loaders;
4
5
use App\Port\Loader\Helpers\Facade\LoaderHelper;
6
use Illuminate\Database\Seeder as LaravelSeeder;
7
8
/**
9
 * Class Seeder.
10
 *
11
 * This Class has inverted dependency :( you must extend this class from the default
12
 * seeder class provided by the framework (database/seeds/DatabaseSeeder.php)
13
 *
14
 * @author  Mahmoud Zalt <[email protected]>
15
 */
16
abstract class SeederLoaderAbstract extends LaravelSeeder
17
{
18
19
    /**
20
     * Default seeders directory for containers and port
21
     *
22
     * @var  string
23
     */
24
    protected $seedersPath = '/Data/Seeders';
25
26
    /**
27
     * Run the database seeds.
28
     * Then Automatically register all Seeders from Containers.
29
     *
30
     * @return void
31
     */
32
    public function run()
33
    {
34
        $this->loadSeedersFromContainers();
35
        $this->loadSeedersFromPort();
36
    }
37
38
    /**
39
     * loadSeedersFromContainers
40
     */
41 View Code Duplication
    private function loadSeedersFromContainers()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
    {
43
        foreach (LoaderHelper::getContainersNames() as $containerName) {
44
45
            $containersDirectory = base_path('app/Containers/' . $containerName . $this->seedersPath);
46
47
            $this->loadSeeds($containersDirectory);
48
        }
49
    }
50
51
    /**
52
     *
53
     */
54 View Code Duplication
    private function loadSeedersFromPort()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56
        // it has to do it's own loop for now
57
        foreach (LoaderHelper::getPortFoldersNames() as $portFolderName) {
58
59
            $portSeedersDirectory = base_path('app/Port/') . $portFolderName . $this->seedersPath;
60
61
            $this->loadSeeds($portSeedersDirectory);
62
        }
63
64
    }
65
66
    /**
67
     * @param $directory
68
     */
69 View Code Duplication
    private function loadSeeds($directory)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
    {
71
        if (\File::isDirectory($directory)) {
72
73
            $files = \File::allFiles($directory);
74
75
            foreach ($files as $seederClass) {
76
77
                if (\File::isFile($seederClass)) {
78
79
                    $seederClass = LoaderHelper::getClassFullNameFromFile($seederClass->getPathname());
80
81
                    // seed it
82
                    $this->call($seederClass);
83
                }
84
85
            }
86
87
        }
88
89
    }
90
91
}
92