Passed
Push — 9.x ( d63f62...9358ea )
by Andrey
20:32 queued 05:32
created

Processor::whenPackage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Processors;
4
5
use Helldar\LaravelLangPublisher\Concerns\Containable;
6
use Helldar\LaravelLangPublisher\Concerns\Contains;
7
use Helldar\LaravelLangPublisher\Concerns\Logger;
8
use Helldar\LaravelLangPublisher\Concerns\Pathable;
9
use Helldar\LaravelLangPublisher\Contracts\Processor as Contract;
10
use Helldar\LaravelLangPublisher\Services\Comparators\Manage;
11
use Helldar\LaravelLangPublisher\Services\Filesystem\Manager;
12
use Helldar\Support\Concerns\Makeable;
13
use Helldar\Support\Facades\Helpers\Filesystem\File;
14
15
abstract class Processor implements Contract
16
{
17
    use Containable;
18
    use Contains;
19
    use Logger;
0 ignored issues
show
Bug introduced by
The trait Helldar\LaravelLangPublisher\Concerns\Logger requires the property $output which is not provided by Helldar\LaravelLangPubli...es\Processors\Processor.
Loading history...
20
    use Makeable;
21
    use Pathable;
22
23
    protected $package;
24
25
    protected $locale;
26
27
    protected $source_path;
28
29
    protected $target_path;
30
31
    protected $force;
32
33
    protected $full = false;
34
35
    public function package(string $package): Contract
36
    {
37
        $this->package = $package;
38
39
        return $this;
40
    }
41
42
    public function locale(string $locale): Contract
43
    {
44
        $this->locale = $locale;
45
46
        return $this;
47
    }
48
49
    public function filename(string $filename, bool $is_inline = true): Contract
50
    {
51
        $this->setSourcePath($filename, $is_inline);
52
        $this->setTargetPath($filename);
53
54
        return $this;
55
    }
56
57
    public function force(bool $force = false): Contract
58
    {
59
        $this->force = $force;
60
61
        return $this;
62
    }
63
64
    public function full(bool $full = false): Contract
65
    {
66
        $this->full = $full;
67
68
        return $this;
69
    }
70
71
    public function whenPackage(?string $package): Contract
72
    {
73
        if ($package) {
74
            $this->package($package);
75
        }
76
77
        return $this;
78
    }
79
80
    public function whenLocale(?string $locale): Contract
81
    {
82
        if ($locale) {
83
            $this->locale($locale);
84
        }
85
86
        return $this;
87
    }
88
89
    public function whenFilename(?string $filename, bool $is_inline = true): Contract
90
    {
91
        if ($filename) {
92
            $this->filename($filename, $is_inline);
93
        }
94
95
        return $this;
96
    }
97
98
    protected function setSourcePath(string $filename, bool $is_inline): void
99
    {
100
        $this->log('Setting the path to the source file:', $filename);
101
102
        if ($this->isValidation($filename) && $is_inline) {
103
            $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline validator messages. Processing in progress...');
104
105
            $name      = $this->pathFilename($filename);
106
            $extension = $this->pathExtension($filename);
107
108
            $filename = $name . '-inline.' . $extension;
109
        } elseif ($this->isJson($filename)) {
110
            $filename = $this->locale . '.json';
111
        }
112
113
        $this->source_path = $this->pathSource($this->package, $this->locale) . '/' . $filename;
114
    }
115
116
    protected function setTargetPath(string $filename): void
117
    {
118
        $this->log('Setting the path to the target file:', $filename);
119
120
        $is_json = $this->isJson($filename);
121
122
        $this->target_path = $this->pathTargetFull($this->locale, $filename, $is_json);
123
    }
124
125
    protected function compare(array $source, array $target): array
126
    {
127
        $this->log('Find an object and perform object comparison.');
128
129
        return Manage::make()
130
            ->filename($this->source_path)
131
            ->full($this->full)
132
            ->source($source)
133
            ->target($target)
134
            ->find()
135
            ->toArray();
136
    }
137
138
    protected function load(string $path): array
139
    {
140
        $this->log('Loading an array:', $path);
141
142
        return $this->manager()->load($path);
143
    }
144
145
    protected function store(string $path, array $content): void
146
    {
147
        $this->log('Saving an array to a file:', $path);
148
149
        $this->manager()->store($path, $content);
150
    }
151
152
    protected function manager(): Manager
153
    {
154
        $this->log('Getting a comparison object...');
155
156
        return $this->container(Manager::class);
157
    }
158
159
    protected function doesntExists(): bool
160
    {
161
        $this->log('Checking for the existence of a file:', $this->target_path);
162
163
        return ! File::exists($this->target_path);
164
    }
165
}
166