Passed
Push — 9.x ( c610c7...55b0ab )
by Andrey
11:17
created

Processor::package()   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
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
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\Concerns\Pathable;
9
use Helldar\LaravelLangPublisher\Contracts\Processor as Contract;
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
    use Pathable;
22
23
    protected $package;
24
25
    protected $locale;
26
27
    protected $source_path;
28
29
    protected $target_path;
30
31
    protected $force;
32
33
    protected $full = false;
34
35
    public function package(string $package): Contract
36
    {
37
        $this->package = $package;
38
39
        return $this;
40
    }
41
42
    public function locale(string $locale): Contract
43
    {
44
        $this->locale = $locale;
45
46
        return $this;
47
    }
48
49
    public function filename(string $filename, bool $is_inline): Contract
50
    {
51
        $this->setSourcePath($filename, $is_inline);
52
        $this->setTargetPath($filename);
53
54
        return $this;
55
    }
56
57
    public function force(bool $force = false): Contract
58
    {
59
        $this->force = $force;
60
61
        return $this;
62
    }
63
64
    public function full(bool $full = false): Contract
65
    {
66
        $this->full = $full;
67
68
        return $this;
69
    }
70
71
    protected function setSourcePath(string $filename, bool $is_inline): void
72
    {
73
        $this->log('Setting the path to the source file: ' . $filename);
74
75
        if ($this->isValidation($filename) && $is_inline) {
76
            $this->log('The "' . $filename . '" file is a collection of inline validator messages. Processing in progress...');
77
78
            $name      = $this->pathFilename($filename);
79
            $extension = $this->pathExtension($filename);
80
81
            $filename = $name . '-inline.' . $extension;
82
        } elseif ($this->isJson($filename)) {
83
            $filename = $this->locale . '.json';
84
        }
85
86
        $this->source_path = $this->pathSource($this->package, $this->locale) . '/' . $filename;
87
    }
88
89
    protected function setTargetPath(string $filename): void
90
    {
91
        $this->log('Setting the path to the target file: ' . $filename);
92
93
        $is_json = $this->isJson($filename);
94
95
        $this->target_path = $this->pathTargetFull($this->locale, $filename, $is_json);
96
    }
97
98
    protected function compare(array $source, array $target): array
99
    {
100
        $this->log('Find an object and perform object comparison.');
101
102
        return Manage::make()
103
            ->filename($this->source_path)
104
            ->full($this->full)
105
            ->source($source)
106
            ->target($target)
107
            ->find()
108
            ->toArray();
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 $this->pathDirectory($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 $this->pathExtension($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