Passed
Push — main ( 55f929...127e84 )
by Andrey
161:41 queued 158:10
created

Processor::setSourcePath()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.024

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 6
cts 10
cp 0.6
crap 5.024
rs 9.9666
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 11
    public function locale(string $locale): Contract
34
    {
35 11
        $this->locale = $locale;
36
37 11
        return $this;
38
    }
39
40 11
    public function filename(string $filename, bool $is_inline): Contract
41
    {
42 11
        $this->setSourcePath($filename, $is_inline);
43 11
        $this->setTargetPath($filename);
44
45 11
        return $this;
46
    }
47
48 11
    public function force(bool $force = false): Contract
49
    {
50 11
        $this->force = $force;
51
52 11
        return $this;
53
    }
54
55 2
    public function full(bool $full = false): Contract
56
    {
57 2
        $this->full = $full;
58
59 2
        return $this;
60
    }
61
62 11
    protected function setSourcePath(string $filename, bool $is_inline): void
63
    {
64 11
        $this->log('Setting the path to the source file: ' . $filename);
65
66 11
        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 11
        } elseif ($this->isJson($filename)) {
74 11
            $filename = $this->locale . '.json';
75
        }
76
77 11
        $this->source_path = Path::source($this->locale) . '/' . $filename;
78 11
    }
79
80 11
    protected function setTargetPath(string $filename): void
81
    {
82 11
        $this->log('Setting the path to the target file: ' . $filename);
83
84 11
        $is_json = $this->isJson($filename);
85
86 11
        $this->target_path = Path::targetFull($this->locale, $filename, $is_json);
87 11
    }
88
89 11
    protected function compare(array $source, array $target): array
90
    {
91 11
        $this->log('Find an object and perform object comparison.');
92
93 11
        return Manage::make()
94 11
            ->filename($this->source_path)
95 11
            ->full($this->full)
96 11
            ->source($source)
97 11
            ->target($target)
98 11
            ->find()
99 11
            ->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 11
    protected function load(string $path): array
110
    {
111 11
        $this->log('Loading an array: ' . $path);
112
113 11
        return $this->manager()->load($path);
114
    }
115
116 11
    protected function store(string $path, array $content): void
117
    {
118 11
        $this->log('Saving an array to a file: ' . $path);
119
120 11
        $this->manager()->store($path, $content);
121 11
    }
122
123 11
    protected function manager(): Manager
124
    {
125 11
        $this->log('Getting a comparison object...');
126
127 11
        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 4
    protected function exists(): bool
145
    {
146 4
        $this->log('Checking for the existence of a file: ' . $this->target_path);
147
148 4
        return File::exists($this->target_path);
149
    }
150
}
151