Passed
Push — main ( 8320d0...c2748c )
by Andrey
45:28 queued 42:35
created

Processor::whenTargetFilename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
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 14
    public function package(string $package): Contract
36
    {
37 14
        $this->package = $package;
38
39 14
        return $this;
40
    }
41
42 15
    public function locale(string $locale): Contract
43
    {
44 15
        $this->locale = $locale;
45
46 15
        return $this;
47
    }
48
49 14
    public function sourceFilename(string $filename, bool $is_inline = true): Contract
50
    {
51 14
        $this->setSourcePath($filename, $is_inline);
52
53 14
        return $this;
54
    }
55
56 14
    public function targetFilename(string $filename): Contract
57
    {
58 14
        $this->setTargetPath($filename);
59
60 14
        return $this;
61
    }
62
63 15
    public function force(bool $force = false): Contract
64
    {
65 15
        $this->force = $force;
66
67 15
        return $this;
68
    }
69
70 2
    public function full(bool $full = false): Contract
71
    {
72 2
        $this->full = $full;
73
74 2
        return $this;
75
    }
76
77 15
    public function whenPackage(?string $package): Contract
78
    {
79 15
        if ($package) {
80 14
            $this->package($package);
81
        }
82
83 15
        return $this;
84
    }
85
86 15
    public function whenLocale(?string $locale): Contract
87
    {
88 15
        if ($locale) {
89 15
            $this->locale($locale);
90
        }
91
92 15
        return $this;
93
    }
94
95 15
    public function whenSourceFilename(?string $filename, bool $is_inline = true): Contract
96
    {
97 15
        if ($filename) {
98 14
            $this->sourceFilename($filename, $is_inline);
99
        }
100
101 15
        return $this;
102
    }
103
104 15
    public function whenTargetFilename(?string $filename): Contract
105
    {
106 15
        if ($filename) {
107 14
            $this->targetFilename($filename);
108
        }
109
110 15
        return $this;
111
    }
112
113 14
    protected function main(): void
114
    {
115 14
        $this->process($this->source_path, $this->target_path);
116 14
    }
117
118 14
    protected function process(string $source_path, string $target_path): void
119
    {
120 14
        $this->log('The process of processing a file from', $source_path, 'to', $target_path, 'has begun.');
121
122 14
        $source = $this->load($source_path);
123 14
        $target = $this->load($target_path);
124
125 14
        $result = $this->compare($source, $target);
126
127 14
        $this->store($target_path, $result);
128 14
    }
129
130 14
    protected function setSourcePath(string $filename, bool $is_inline): void
131
    {
132 14
        $this->log('Setting the path to the source file:', $filename);
133
134 14
        $path = $this->pathSource($this->package, $this->locale);
135
136 14
        if ($is_inline) {
137 14
            $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline messages...');
138
139 14
            $directory = $this->pathDirectory($filename);
140 14
            $name      = $this->pathFilename($filename);
141 14
            $extension = $this->pathExtension($filename);
142
143 14
            $inline_file = $directory . '/' . $name . '-inline.' . $extension;
144
        }
145
146 14
        $this->source_path = File::exists($path . '/' . $inline_file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inline_file does not seem to be defined for all execution paths leading up to this point.
Loading history...
147 14
            ? $path . '/' . $inline_file
148 14
            : $path . '/' . $filename;
149 14
    }
150
151 14
    protected function setTargetPath(string $filename): void
152
    {
153 14
        $this->log('Setting the path to the target file:', $filename);
154
155 14
        $this->target_path = $this->pathTargetFull($this->locale, $filename);
156 14
    }
157
158 14
    protected function compare(array $source, array $target): array
159
    {
160 14
        $this->log('Find an object and perform object comparison.');
161
162 14
        return Manage::make()
163 14
            ->filename($this->source_path)
164 14
            ->full($this->full)
165 14
            ->source($source)
166 14
            ->target($target)
167 14
            ->find()
168 14
            ->toArray();
169
    }
170
171 14
    protected function load(string $path): array
172
    {
173 14
        $this->log('Loading an array:', $path);
174
175 14
        return $this->manager()->load($path);
176
    }
177
178 14
    protected function store(string $path, array $content): void
179
    {
180 14
        $this->log('Saving an array to a file:', $path);
181
182 14
        $this->manager()->store($path, $content);
183 14
    }
184
185 14
    protected function manager(): Manager
186
    {
187 14
        $this->log('Getting a comparison object...');
188
189 14
        return $this->container(Manager::class);
190
    }
191
192 7
    protected function doesntExists(): bool
193
    {
194 7
        $this->log('Checking for the existence of a file:', $this->target_path);
195
196 7
        return ! File::exists($this->target_path);
197
    }
198
}
199