Passed
Pull Request — main (#94)
by Andrey
53:33 queued 39:20
created

Processor::load()   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 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 2
cp 0
crap 2
rs 10
c 2
b 0
f 0
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\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
22
    protected $locale;
23
24
    protected $source_path;
25
26
    protected $target_path;
27
28
    protected $force;
29
30
    protected $full = false;
31
32
    public function locale(string $locale): Contract
33 11
    {
34
        $this->locale = $locale;
35 11
36
        return $this;
37 11
    }
38
39
    public function filename(string $filename, bool $is_inline): Contract
40 11
    {
41
        $this->setSourcePath($filename, $is_inline);
42 11
        $this->setTargetPath($filename);
43 11
44
        return $this;
45 11
    }
46
47
    public function force(bool $force = false): Contract
48 11
    {
49
        $this->force = $force;
50 11
51
        return $this;
52 11
    }
53
54
    public function full(bool $full = false): Contract
55 2
    {
56
        $this->full = $full;
57 2
58
        return $this;
59 2
    }
60
61
    protected function setSourcePath(string $filename, bool $is_inline): void
62 11
    {
63
        $this->log('Setting the path to the source file: ' . $filename);
64 11
65
        if ($this->isValidation($filename) && $is_inline) {
66 11
            $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
        } elseif ($this->isJson($filename)) {
73 11
            $filename = $this->locale . '.json';
74 11
        }
75
76
        $this->source_path = Path::source($this->locale) . '/' . $filename;
77 11
    }
78 11
79
    protected function setTargetPath(string $filename): void
80 11
    {
81
        $this->log('Setting the path to the target file: ' . $filename);
82 11
83
        $is_json = $this->isJson($filename);
84 11
85
        $this->target_path = Path::targetFull($this->locale, $filename, $is_json);
86 11
    }
87 11
88
    protected function compare(array $source, array $target): array
89 11
    {
90
        $this->log('Find an object and perform object comparison.');
91 11
92
        return Manage::make()
93 11
            ->filename($this->source_path)
94 11
            ->full($this->full)
95 11
            ->source($source)
96 11
            ->target($target)
97 11
            ->find()
98 11
            ->toArray();
99 11
    }
100
101
    protected function load(string $path): array
102
    {
103
        $this->log('Loading an array: ' . $path);
104
105
        return $this->manager()->load($path);
106
    }
107
108
    protected function store(string $path, array $content): void
109 11
    {
110
        $this->log('Saving an array to a file: ' . $path);
111 11
112
        $this->manager()->store($path, $content);
113 11
    }
114
115
    protected function manager(): Manager
116 11
    {
117
        $this->log('Getting a comparison object...');
118 11
119
        return $this->container(Manager::class);
120 11
    }
121 11
122
    protected function directory(string $path): string
123 11
    {
124
        $this->log('Getting the directory name for a path: ' . $path);
125 11
126
        return Path::directory($path);
127 11
    }
128
129
    protected function extension(string $path): string
130
    {
131
        $this->log('Getting the file extension for a path: ' . $path);
132
133
        return Path::extension($path);
134
    }
135
136
    protected function exists(): bool
137
    {
138
        $this->log('Checking for the existence of a file: ' . $this->target_path);
139
140
        return File::exists($this->target_path);
141
    }
142
}
143