Passed
Push — main ( c0376c...6e7d22 )
by Garbuz
03:23
created

FileGenerator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 97.3%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 7
eloc 42
c 3
b 0
f 0
dl 0
loc 88
ccs 36
cts 37
cp 0.973
rs 10

5 Methods

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