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