Passed
Pull Request — main (#104)
by Andrey
84:07 queued 69:01
created

Processor::extension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
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 11
    public function package(string $package): Contract
36
    {
37 11
        $this->package = $package;
38
39 11
        return $this;
40
    }
41
42 11
    public function locale(string $locale): Contract
43
    {
44 11
        $this->locale = $locale;
45
46 11
        return $this;
47
    }
48
49 11
    public function filename(string $filename, bool $is_inline = true): Contract
50
    {
51 11
        $this->setSourcePath($filename, $is_inline);
52 11
        $this->setTargetPath($filename);
53
54 11
        return $this;
55
    }
56
57 11
    public function force(bool $force = false): Contract
58
    {
59 11
        $this->force = $force;
60
61 11
        return $this;
62
    }
63
64 2
    public function full(bool $full = false): Contract
65
    {
66 2
        $this->full = $full;
67
68 2
        return $this;
69
    }
70
71 11
    public function whenPackage(?string $package): Contract
72
    {
73 11
        if ($package) {
74
            $this->package($package);
75 11
        }
76 11
77
        return $this;
78 11
    }
79 11
80
    public function whenLocale(?string $locale): Contract
81 11
    {
82 11
        if ($locale) {
83 11
            $this->locale($locale);
84
        }
85
86 11
        return $this;
87 11
    }
88
89 11
    public function whenFilename(?string $filename, bool $is_inline = true): Contract
90
    {
91 11
        if ($filename) {
92
            $this->filename($filename, $is_inline);
93 11
        }
94
95 11
        return $this;
96 11
    }
97
98 11
    protected function setSourcePath(string $filename, bool $is_inline): void
99
    {
100 11
        $this->log('Setting the path to the source file:', $filename);
101
102 11
        if ($this->isValidation($filename) && $is_inline) {
103 11
            $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline validator messages. Processing in progress...');
104 11
105 11
            $name      = $this->pathFilename($filename);
106 11
            $extension = $this->pathExtension($filename);
107 11
108 11
            $filename = $name . '-inline.' . $extension;
109
        } elseif ($this->isJson($filename)) {
110
            $filename = $this->locale . '.json';
111 11
        }
112
113 11
        $this->source_path = $this->pathSource($this->package, $this->locale) . '/' . $filename;
114
    }
115 11
116
    protected function setTargetPath(string $filename): void
117
    {
118 11
        $this->log('Setting the path to the target file:', $filename);
119
120 11
        $is_json = $this->isJson($filename);
121
122 11
        $this->target_path = $this->pathTargetFull($this->locale, $filename, $is_json);
123 11
    }
124
125 11
    protected function compare(array $source, array $target): array
126
    {
127 11
        $this->log('Find an object and perform object comparison.');
128
129 11
        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 4
    {
147
        $this->log('Saving an array to a file:', $path);
148 4
149
        $this->manager()->store($path, $content);
150 4
    }
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