Passed
Push — main ( 25ff95...174661 )
by Garbuz
13:35
created

FileGenerator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 97.44%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 44
c 3
b 0
f 0
dl 0
loc 96
ccs 38
cts 39
cp 0.9744
rs 10

5 Methods

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