Passed
Push — 9.x ( 28b741...b7dd27 )
by Andrey
39:34 queued 26:05
created

Processor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 51
c 3
b 0
f 0
dl 0
loc 126
rs 10
wmc 16

13 Methods

Rating   Name   Duplication   Size   Complexity  
A full() 0 5 1
A force() 0 5 1
A locale() 0 5 1
A filename() 0 6 1
A setSourcePath() 0 16 4
A directory() 0 5 1
A compare() 0 11 1
A store() 0 5 1
A extension() 0 5 1
A manager() 0 5 1
A setTargetPath() 0 7 1
A exists() 0 5 1
A load() 0 5 1
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
    {
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
        } elseif ($this->isJson($filename)) {
73
            $filename = $this->locale . '.json';
74
        }
75
76
        $this->source_path = Path::source($this->locale) . '/' . $filename;
77
    }
78
79
    protected function setTargetPath(string $filename): void
80
    {
81
        $this->log('Setting the path to the target file: ' . $filename);
82
83
        $is_json = $this->isJson($filename);
84
85
        $this->target_path = Path::targetFull($this->locale, $filename, $is_json);
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
            ->full($this->full)
95
            ->source($source)
96
            ->target($target)
97
            ->find()
98
            ->toArray();
99
    }
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
    {
110
        $this->log('Saving an array to a file: ' . $path);
111
112
        $this->manager()->store($path, $content);
113
    }
114
115
    protected function manager(): Manager
116
    {
117
        $this->log('Getting a comparison object...');
118
119
        return $this->container(Manager::class);
120
    }
121
122
    protected function directory(string $path): string
123
    {
124
        $this->log('Getting the directory name for a path: ' . $path);
125
126
        return Path::directory($path);
127
    }
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