Passed
Pull Request — main (#84)
by Andrey
196:26 queued 181:26
created

Processor::locale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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\Contracts\Processor as Contract;
9
use Helldar\LaravelLangPublisher\Facades\Path;
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\Arr;
14
use Helldar\Support\Facades\Helpers\Filesystem\File;
15
16
abstract class Processor implements Contract
17
{
18
    use Containable;
19
    use Contains;
20
    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...
21
    use Makeable;
22
23
    protected $locale;
24
25
    protected $source_path;
26
27
    protected $target_path;
28
29
    protected $force;
30
31
    protected $full = false;
32
33
    public function locale(string $locale): Contract
34
    {
35
        $this->locale = $locale;
36
37
        return $this;
38
    }
39
40
    public function filename(string $filename, bool $is_inline): Contract
41
    {
42
        $this->setSourcePath($filename, $is_inline);
43
        $this->setTargetPath($filename);
44
45
        return $this;
46
    }
47
48
    public function force(bool $force = false): Contract
49
    {
50
        $this->force = $force;
51
52
        return $this;
53
    }
54
55
    public function full(bool $full = false): Contract
56
    {
57
        $this->full = $full;
58
59
        return $this;
60
    }
61
62
    protected function setSourcePath(string $filename, bool $is_inline): void
63
    {
64
        $this->log('Setting the path to the source file: ' . $filename);
65
66
        if ($this->isValidation($filename) && $is_inline) {
67
            $this->log('The "' . $filename . '" file is a collection of inline validator messages. Processing in progress...');
68
69
            $name      = Path::filename($filename);
70
            $extension = Path::extension($filename);
71
72
            $filename = $name . '-inline.' . $extension;
73
        } elseif ($this->isJson($filename)) {
74
            $filename = $this->locale . '.json';
75
        }
76
77
        $this->source_path = Path::source($this->locale) . '/' . $filename;
78
    }
79
80
    protected function setTargetPath(string $filename): void
81
    {
82
        $this->log('Setting the path to the target file: ' . $filename);
83
84
        $is_json = $this->isJson($filename);
85
86
        $this->target_path = Path::targetFull($this->locale, $filename, $is_json);
87
    }
88
89
    protected function compare(array $source, array $target): array
90
    {
91
        $this->log('Find an object and perform object comparison.');
92
93
        return Manage::make()
94
            ->filename($this->source_path)
95
            ->full($this->full)
96
            ->source($source)
97
            ->target($target)
98
            ->find()
99
            ->toArray();
100
    }
101
102
    protected function sort(array &$array): void
103
    {
104
        $this->log('Sorting an array.');
105
106
        $array = Arr::ksort($array);
107
    }
108
109
    protected function load(string $path): array
110
    {
111
        $this->log('Loading an array: ' . $path);
112
113
        return $this->manager()->load($path);
114
    }
115
116
    protected function store(string $path, array $content): void
117
    {
118
        $this->log('Saving an array to a file: ' . $path);
119
120
        $this->manager()->store($path, $content);
121
    }
122
123
    protected function manager(): Manager
124
    {
125
        $this->log('Getting a comparison object...');
126
127
        return $this->container(Manager::class);
128
    }
129
130
    protected function directory(string $path): string
131
    {
132
        $this->log('Getting the directory name for a path: ' . $path);
133
134
        return Path::directory($path);
135
    }
136
137
    protected function extension(string $path): string
138
    {
139
        $this->log('Getting the file extension for a path: ' . $path);
140
141
        return Path::extension($path);
142
    }
143
144
    protected function exists(): bool
145
    {
146
        $this->log('Checking for the existence of a file: ' . $this->target_path);
147
148
        return File::exists($this->target_path);
149
    }
150
}
151