Passed
Pull Request — main (#84)
by Andrey
59:33 queued 44:34
created

Comparator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 93
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A source() 0 5 1
A getFallbackValue() 0 5 2
A sort() 0 5 1
A ranExcludes() 0 6 2
A split() 0 8 2
A handle() 0 5 1
A toArray() 0 10 1
A extractExcludes() 0 8 1
A target() 0 5 1
A prepareExcludes() 0 5 1
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Comparators;
4
5
use Helldar\LaravelLangPublisher\Concerns\Logger;
6
use Helldar\LaravelLangPublisher\Contracts\Comparator as Contract;
7
use Helldar\Support\Concerns\Makeable;
8
use Helldar\Support\Facades\Helpers\Arr;
9
10
abstract class Comparator implements Contract
11
{
12
    use Logger;
13
    use Makeable;
14
15
    protected $source;
16
17
    protected $target;
18
19
    protected $excludes = [];
20
21
    public function handle(): array
22
    {
23
        $this->log('Merging source and target arrays...');
24
25
        return array_merge($this->source, $this->target);
26
    }
27
28
    public function source(array $array): Contract
29
    {
30
        $this->source = $array;
31
32
        return $this;
33
    }
34
35
    public function target(array $array): Contract
36
    {
37
        $this->target = $array;
38
39
        return $this;
40
    }
41
42
    public function toArray(): array
43
    {
44
        $this->split();
45
46
        $array    = $this->sort($this->handle());
47
        $excludes = $this->sort($this->excludes);
48
49
        $this->log('Merging the main array with excluded data...');
50
51
        return array_merge($array, $excludes);
52
    }
53
54
    protected function split(): void
55
    {
56
        $this->log('Splitting main arrays into excludes...');
57
58
        if (! empty($this->excludes)) {
59
            $this->prepareExcludes();
60
            $this->ranExcludes();
61
            $this->extractExcludes();
62
        }
63
    }
64
65
    protected function ranExcludes(): void
66
    {
67
        $this->log('Retrieving values from arrays...');
68
69
        foreach ($this->excludes as $key => &$value) {
70
            $value = $this->getFallbackValue($this->source, $this->target, $key);
71
        }
72
    }
73
74
    protected function extractExcludes(): void
75
    {
76
        $this->log('Extracting extended data from source and target arrays ...');
77
78
        $keys = array_keys($this->excludes);
79
80
        $this->source = Arr::except($this->source, $keys);
81
        $this->target = Arr::except($this->target, $keys);
82
    }
83
84
    protected function getFallbackValue(array $source, array $target, string $key): array
85
    {
86
        $this->log('Retrieving values from arrays by the "' . $key . '" key...');
87
88
        return Arr::get($target, $key) ?: Arr::get($source, $key, []);
89
    }
90
91
    protected function sort(array $array): array
92
    {
93
        $this->log('Sorting array...');
94
95
        return Arr::ksort($array);
96
    }
97
98
    protected function prepareExcludes(): void
99
    {
100
        $this->log('Exchanges all keys with their associated values in an array...');
101
102
        $this->excludes = array_flip($this->excludes);
103
    }
104
}
105