FileGenerator::make()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 3
    public function __construct(Configuration $config)
30
    {
31 3
        $this->config = $config;
32 3
    }
33
34
    /**
35
     * @param Package $package
36
     * @return bool
37
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
38
     */
39 3
    public function make(Package $package): bool
40
    {
41 3
        $this->package = $package;
42 3
        $this->templates();
43 3
        app(Migration::class)->make($package);
44 3
        app(Model::class)->make($package);
45 3
        app(Seed::class)->make($package);
46 3
        return true;
47
    }
48
49
    /**
50
     * Copy all templates file
51
     *
52
     * @return bool
53
     */
54 3
    public function templates(): bool
55
    {
56 3
        $templatesDir = realpath(__DIR__ . '/../../templates');
57 3
        $files = File::allFiles($templatesDir, true);
58 3
        foreach ($files as $file) {
59 3
            $pathDir = str_replace($templatesDir, '', $file->getPath());
60 3
            $pathDir = $this->package->getPath($pathDir);
61 3
            if (!file_exists($pathDir)) {
62
                continue;
63
            }
64 3
            $content = $this->replacement(file_get_contents($file->getRealPath()));
65 3
            $newPathFile = str_replace([$templatesDir, '.stub'], '', $file->getRealPath());
66 3
            $newPathFile = $this->package->getPath($newPathFile);
67 3
            file_put_contents($newPathFile, $content);
68
        }
69 3
        return true;
70
    }
71
72
    /**
73
     * @param string|null $content
74
     * @return string|null
75
     */
76 3
    public function replacement(?string $content = null): ?string
77
    {
78 3
        $content = str_replace([
79 3
            '%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 3
            $this->package->getPackageVendor() . '/' . $this->package->getPackageName(),
89 3
            $this->package->getPackageVendor(),
90 3
            $this->package->getPackageName(),
91 3
            $this->package->getNamespace(),
92 3
            $this->package->getNamespaceSlashes(),
93 3
            $this->package->getPackageVendor() . '-' . $this->package->getPackageName(),
94 3
            $this->package->getName(),
95 3
            $this->package->getDescripton(),
96
        ], $content);
97 3
        return $content;
98
    }
99
}
100