Builder::isIgnore()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6111
cc 5
nc 3
nop 2
crap 5
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\Log;
9
10
class Builder
11
{
12
    /**
13
     * @var Configuration
14
     */
15
    private Configuration $config;
16
17
    /**
18
     * @var string|null
19
     */
20
    private ?string $packageVendor = null;
21
    private ?string $packageName = null;
22
23
    /**
24
     * Builder constructor.
25
     * @param Configuration $config
26
     */
27 7
    public function __construct(Configuration $config)
28
    {
29 7
        $this->config = $config;
30 7
    }
31
32
    /**
33
     * @param string|null $packageFilter
34
     * @return bool
35
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
36
     */
37 3
    public function init(?string $packageFilter = null): bool
38
    {
39 3
        $this->initFilter($packageFilter);
40 3
        $configGenerator = $this->config->getGenerator();
41 3
        $countPackage = 0;
42 3
        foreach ($configGenerator as $package) {
43 3
            $package = app(Package::class)->init($package);
44 3
            if ($this->isIgnore(/** @scrutinizer ignore-type */ $package->getPackageVendor(), /** @scrutinizer ignore-type */ $package->getPackageName())) {
45 1
                continue;
46
            }
47
            /* Generation */
48 2
            echo ++$countPackage . ': Generation: ' . $package->getPackageVendor() . '/' . $package->getPackageName() . "\n";
49 2
            app(DirGenerator::class)->make($package);
50 2
            app(FileGenerator::class)->make($package);
51
        }
52 3
        return true;
53
    }
54
55
    /**
56
     * @param string $vandor
57
     * @param string $name
58
     * @return bool
59
     */
60 4
    public function isIgnore(string $vandor, string $name): bool
61
    {
62 4
        if (!is_null($this->getPackageVendor()) && $this->getPackageVendor() != $vandor) {
63 2
            return true;
64
        }
65 3
        if (!is_null($this->getPackageName()) && $this->getPackageName() != $name) {
66 1
            return true;
67
        }
68 3
        return false;
69
    }
70
71
    /**
72
     * @param string|null $package
73
     * @return bool
74
     */
75 7
    public function initFilter(?string $package = null): bool
76
    {
77 7
        if (is_null($package)) {
78 2
            return true;
79
        }
80 5
        $package = mb_strtolower($package);
81 5
        $package = str_replace('\\', '/', $package);
82 5
        if (!mb_stripos($package, '/')) {
83 1
            $this->packageVendor = $package;
84 1
            return true;
85
        }
86 5
        $match = explode('/', $package, 2);
87 5
        $this->packageVendor = $match[0];
88 5
        $this->packageName = mb_strlen(trim($match[1])) > 0 ? $match[1] : null;
89 5
        return true;
90
    }
91
92
    /**
93
     * @return string|null
94
     */
95 7
    public function getPackageVendor(): ?string
96
    {
97 7
        return $this->packageVendor;
98
    }
99
100
    /**
101
     * @return string|null
102
     */
103 6
    public function getPackageName(): ?string
104
    {
105 6
        return $this->packageName;
106
    }
107
}
108