Passed
Pull Request — main (#123)
by Andrey
42:12 queued 26:54
created

Php   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 77
rs 10
c 2
b 0
f 0
wmc 13

9 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 7 1
A setCase() 0 4 1
A setAlignment() 0 4 2
A resolveAlignedPath() 0 13 3
A load() 0 11 2
A setKeyToString() 0 3 1
A getCase() 0 3 1
A delete() 0 3 1
A formatter() 0 9 1
1
<?php
2
3
/*
4
 * This file is part of the "andrey-helldar/laravel-lang-publisher" project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author Andrey Helldar <[email protected]>
10
 *
11
 * @copyright 2021 Andrey Helldar
12
 *
13
 * @license MIT
14
 *
15
 * @see https://github.com/andrey-helldar/laravel-lang-publisher
16
 */
17
18
declare(strict_types=1);
19
20
namespace Helldar\LaravelLangPublisher\Support\Filesystem;
21
22
use Helldar\LaravelLangPublisher\Facades\Helpers\Config;
23
use Helldar\PrettyArray\Services\File as Pretty;
24
use Helldar\PrettyArray\Services\Formatter;
25
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
26
use Helldar\Support\Facades\Helpers\Filesystem\File;
27
28
class Php extends Base
29
{
30
    public function load(string $path): array
31
    {
32
        $path = $this->resolveAlignedPath($path);
33
34
        if ($this->doesntExists($path)) {
35
            return [];
36
        }
37
38
        $items = Pretty::make()->load($path);
39
40
        return $this->correct($items);
41
    }
42
43
    public function store(string $path, $content): string
44
    {
45
        $service = $this->formatter();
46
47
        Pretty::make($service->raw($content))->store($path);
48
49
        return $path;
50
    }
51
52
    public function delete(string $path): void
53
    {
54
        Directory::ensureDelete($path);
55
    }
56
57
    protected function resolveAlignedPath(string $path): string
58
    {
59
        if (! $this->hasInline()) {
60
            return $path;
61
        }
62
63
        $directory = $this->directory($path);
64
        $filename  = $this->filename($path);
65
        $extension = $this->extension($path);
66
67
        $inline_path = $this->path($directory, $filename . '-inline.' . $extension);
68
69
        return File::exists($inline_path) ? $inline_path : $path;
70
    }
71
72
    protected function formatter(): Formatter
73
    {
74
        $formatter = Formatter::make();
75
76
        $this->setCase($formatter);
77
        $this->setAlignment($formatter);
78
        $this->setKeyToString($formatter);
79
80
        return $formatter;
81
    }
82
83
    protected function setCase(Formatter $formatter): void
84
    {
85
        $formatter->setCase(
86
            $this->getCase()
87
        );
88
    }
89
90
    protected function setAlignment(Formatter $formatter): void
91
    {
92
        if ($this->hasAlignment()) {
93
            $formatter->setEqualsAlign();
94
        }
95
    }
96
97
    protected function setKeyToString(Formatter $formatter): void
98
    {
99
        $formatter->setKeyAsString();
100
    }
101
102
    protected function getCase(): int
103
    {
104
        return Config::case();
105
    }
106
}
107