Passed
Pull Request — main (#84)
by Andrey
49:37 queued 34:36
created

Processor::full()   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;
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
        $filename = $is_json ? $this->locale . '.json' : $filename;
87
88
        $this->target_path = Path::target($this->locale, $is_json) . '/' . $filename;
89
    }
90
91
    protected function compare(array $source, array $target): array
92
    {
93
        $this->log('Find an object and perform object comparison.');
94
95
        return Manage::make()
96
            ->filename($this->source_path)
97
            ->full($this->full)
98
            ->source($source)
99
            ->target($target)
100
            ->find()
101
            ->toArray();
102
    }
103
104
    protected function sort(array &$array): void
105
    {
106
        $this->log('Sorting an array.');
107
108
        $array = Arr::ksort($array);
109
    }
110
111
    protected function load(string $path): array
112
    {
113
        $this->log('Loading an array: ' . $path);
114
115
        return $this->manager()->load($path);
116
    }
117
118
    protected function store(string $path, array $content): void
119
    {
120
        $this->log('Saving an array to a file: ' . $path);
121
122
        $this->manager()->store($path, $content);
123
    }
124
125
    protected function manager(): Manager
126
    {
127
        $this->log('Getting a comparison object...');
128
129
        return $this->container(Manager::class);
130
    }
131
132
    protected function directory(string $path): string
133
    {
134
        $this->log('Getting the directory name for a path: ' . $path);
135
136
        return Path::directory($path);
137
    }
138
139
    protected function extension(string $path): string
140
    {
141
        $this->log('Getting the file extension for a path: ' . $path);
142
143
        return Path::extension($path);
144
    }
145
146
    protected function exists(): bool
147
    {
148
        $this->log('Checking for the existence of a file: ' . $this->target_path);
149
150
        return File::exists($this->target_path);
151
    }
152
}
153