Passed
Push — main ( 545ade...3cfdef )
by Andrey
79:39 queued 77:29
created

Processor::package()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
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\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 11
    public function package(string $package): Contract
36
    {
37 11
        $this->package = $package;
38
39 11
        return $this;
40
    }
41
42 11
    public function locale(string $locale): Contract
43
    {
44 11
        $this->locale = $locale;
45
46 11
        return $this;
47
    }
48
49 11
    public function filename(string $filename, bool $is_inline): Contract
50
    {
51 11
        $this->setSourcePath($filename, $is_inline);
52 11
        $this->setTargetPath($filename);
53
54 11
        return $this;
55
    }
56
57 11
    public function force(bool $force = false): Contract
58
    {
59 11
        $this->force = $force;
60
61 11
        return $this;
62
    }
63
64 2
    public function full(bool $full = false): Contract
65
    {
66 2
        $this->full = $full;
67
68 2
        return $this;
69
    }
70
71 11
    protected function setSourcePath(string $filename, bool $is_inline): void
72
    {
73 11
        $this->log('Setting the path to the source file: ' . $filename);
74
75 11
        if ($this->isValidation($filename) && $is_inline) {
76 11
            $this->log('The "' . $filename . '" file is a collection of inline validator messages. Processing in progress...');
77
78 11
            $name      = $this->pathFilename($filename);
79 11
            $extension = $this->pathExtension($filename);
80
81 11
            $filename = $name . '-inline.' . $extension;
82 11
        } elseif ($this->isJson($filename)) {
83 11
            $filename = $this->locale . '.json';
84
        }
85
86 11
        $this->source_path = $this->pathSource($this->package, $this->locale) . '/' . $filename;
87 11
    }
88
89 11
    protected function setTargetPath(string $filename): void
90
    {
91 11
        $this->log('Setting the path to the target file: ' . $filename);
92
93 11
        $is_json = $this->isJson($filename);
94
95 11
        $this->target_path = $this->pathTargetFull($this->locale, $filename, $is_json);
96 11
    }
97
98 11
    protected function compare(array $source, array $target): array
99
    {
100 11
        $this->log('Find an object and perform object comparison.');
101
102 11
        return Manage::make()
103 11
            ->filename($this->source_path)
104 11
            ->full($this->full)
105 11
            ->source($source)
106 11
            ->target($target)
107 11
            ->find()
108 11
            ->toArray();
109
    }
110
111 11
    protected function load(string $path): array
112
    {
113 11
        $this->log('Loading an array: ' . $path);
114
115 11
        return $this->manager()->load($path);
116
    }
117
118 11
    protected function store(string $path, array $content): void
119
    {
120 11
        $this->log('Saving an array to a file: ' . $path);
121
122 11
        $this->manager()->store($path, $content);
123 11
    }
124
125 11
    protected function manager(): Manager
126
    {
127 11
        $this->log('Getting a comparison object...');
128
129 11
        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 4
    protected function exists(): bool
147
    {
148 4
        $this->log('Checking for the existence of a file: ' . $this->target_path);
149
150 4
        return File::exists($this->target_path);
151
    }
152
}
153