RepositoryPatternService::ImplementNow()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 2
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pratiksh\Adminetic\Services;
4
5
use Illuminate\Support\Facades\Artisan;
6
use Illuminate\Support\Str;
7
use Pratiksh\Adminetic\Services\Helper\CommandHelper;
8
9
class RepositoryPatternService extends CommandHelper
10
{
11
    public static function ImplementNow($name, $makeRequest = false)
12
    {
13
        if (! file_exists($repository_path = app_path('/Repositories'))) {
14
            mkdir($repository_path, 0777, true);
15
        }
16
        if (! file_exists($contract_path = app_path('/Contracts'))) {
17
            mkdir($contract_path, 0777, true);
18
        }
19
20
        self::MakeInterface($name);
21
        self::MakeRepositoryClass($name);
22
        if ($makeRequest) {
23
            self::makeRequest($name);
24
        }
25
    }
26
27
    protected static function MakeInterface($name)
28
    {
29
        $template = str_replace(
30
            [
31
                '{{modelName}}',
32
                '{{modelNameLowerCase}}',
33
                '{{modelNamePluralLowerCase}}',
34
            ],
35
            [
36
                $name,
37
                strtolower($name),
38
                strtolower(Str::plural($name)),
39
            ],
40
41
            self::getStub('RepositoryInterface')
42
        );
43
44
        file_put_contents(app_path("/Contracts/{$name}RepositoryInterface.php"), $template);
45
    }
46
47
    protected static function MakeRepositoryClass($name)
48
    {
49
        $template = str_replace(
50
            [
51
                '{{modelName}}',
52
                '{{modelNameLowerCase}}',
53
                '{{modelNamePluralLowerCase}}',
54
            ],
55
            [
56
                $name,
57
                strtolower($name),
58
                strtolower(Str::plural($name)),
59
            ],
60
            self::getStub('Repository')
61
        );
62
63
        file_put_contents(app_path("/Repositories/{$name}Repository.php"), $template);
64
    }
65
66
    protected static function makeRequest($name)
67
    {
68
        Artisan::call('make:request '.$name.'Request');
69
    }
70
}
71