Passed
Push — main ( d04005...d016cc )
by Garbuz
02:22
created

FileGenerator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 97.14%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 6
eloc 41
c 3
b 0
f 0
dl 0
loc 85
ccs 34
cts 35
cp 0.9714
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A replacement() 0 22 1
A make() 0 8 1
A templates() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GarbuzIvan\LaravelGeneratorPackage\Builder;
6
7
use GarbuzIvan\LaravelGeneratorPackage\Builder\Components\Migration;
8
use GarbuzIvan\LaravelGeneratorPackage\Builder\Components\Model;
9
use GarbuzIvan\LaravelGeneratorPackage\Builder\Components\Seed;
10
use GarbuzIvan\LaravelGeneratorPackage\Configuration;
11
use Illuminate\Support\Facades\File;
12
13
class FileGenerator
14
{
15
    /**
16
     * @var Configuration
17
     */
18
    private Configuration $config;
19
20
    /**
21
     * @var Package
22
     */
23
    private Package $package;
24
25
    /**
26
     * FileGenerator constructor.
27
     * @param Configuration $config
28
     */
29 2
    public function __construct(Configuration $config)
30
    {
31 2
        $this->config = $config;
32 2
    }
33
34
    /**
35
     * @param Package $package
36
     * @return bool
37
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
38
     */
39 2
    public function make(Package $package): bool
40
    {
41 2
        $this->package = $package;
42 2
        $this->templates();
43 2
        app(Migration::class)->make($package);
44 2
        app(Model::class)->make($package);
45 2
        app(Seed::class)->make($package);
46 2
        return true;
47
    }
48
49
    /**
50
     * Copy all templates file
51
     *
52
     * @return bool
53
     */
54 2
    public function templates(): bool
55
    {
56 2
        $templatesDir = realpath(__DIR__ . '/../../templates');
57 2
        $files = File::allFiles($templatesDir, true);
58 2
        foreach ($files as $file) {
59 2
            $pathDir = str_replace($templatesDir, '', $file->getPath());
60 2
            $pathDir = $this->package->getPath($pathDir);
61 2
            if (!file_exists($pathDir)) {
62
                continue;
63
            }
64 2
            $content = $this->replacement(file_get_contents($file->getRealPath()));
65 2
            $newPathFile = str_replace([$templatesDir, '.stub'], '', $file->getRealPath());
66 2
            $newPathFile = $this->package->getPath($newPathFile);
67 2
            file_put_contents($newPathFile, $content);
68
        }
69 2
        return true;
70
    }
71
72
    /**
73
     * @param string|null $content
74
     * @return string|null
75
     */
76 2
    public function replacement(?string $content = null): ?string
77
    {
78 2
        $content = str_replace([
79 2
            '%PACKAGE_GIT%',
80
            '%PACKAGE_PVENDOR%',
81
            '%PACKAGE_PNAME%',
82
            '%PACKAGE_NAMESPACE%',
83
            '%PACKAGE_NAMESPACE_SLASHES%',
84
            '%PACKAGE_CONFIG_NAME%',
85
            '%PACKAGE_NAME%',
86
            '%PACKAGE_DESCRIPTIONS%',
87
        ], [
88 2
            $this->package->getPackageVendor() . '/' . $this->package->getPackageName(),
89 2
            $this->package->getPackageVendor(),
90 2
            $this->package->getPackageName(),
91 2
            $this->package->getNamespace(),
92 2
            $this->package->getNamespaceSlashes(),
93 2
            $this->package->getPackageVendor() . '-' . $this->package->getPackageName(),
94 2
            $this->package->getName(),
95 2
            $this->package->getDescripton(),
96
        ], $content);
97 2
        return $content;
98
    }
99
}
100