Passed
Push — main ( f4d82a...ca2db5 )
by Andrey
03:23 queued 12s
created

Php::setAlignment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
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 62
    public function load(string $path): array
31
    {
32 62
        $path = $this->resolveAlignedPath($path);
33
34 62
        if ($this->doesntExists($path)) {
35 62
            return [];
36
        }
37
38 62
        $items = Pretty::make()->load($path);
39
40 62
        return $this->correct($items);
41
    }
42
43 62
    public function store(string $path, $content): string
44
    {
45 62
        $service = $this->formatter();
46
47 62
        Pretty::make($service->raw($content))->store($path);
48
49 62
        return $path;
50
    }
51
52 4
    public function delete(string $path): void
53
    {
54 4
        Directory::ensureDelete($path);
55 4
    }
56
57 62
    protected function resolveAlignedPath(string $path): string
58
    {
59 62
        if (! $this->hasInline()) {
60 30
            return $path;
61
        }
62
63 32
        $directory = $this->directory($path);
64 32
        $filename  = $this->filename($path);
65 32
        $extension = $this->extension($path);
66
67 32
        $inline_path = $this->path($directory, $filename . '-inline.' . $extension);
68
69 32
        return File::exists($inline_path) ? $inline_path : $path;
70
    }
71
72 62
    protected function formatter(): Formatter
73
    {
74 62
        $formatter = Formatter::make();
75
76 62
        $this->setCase($formatter);
77 62
        $this->setAlignment($formatter);
78 62
        $this->setKeyToString($formatter);
79
80 62
        return $formatter;
81
    }
82
83 62
    protected function setCase(Formatter $formatter): void
84
    {
85 62
        $formatter->setCase(
86 62
            $this->getCase()
87
        );
88 62
    }
89
90 62
    protected function setAlignment(Formatter $formatter): void
91
    {
92 62
        if ($this->hasAlignment()) {
93 62
            $formatter->setEqualsAlign();
94
        }
95 62
    }
96
97 62
    protected function setKeyToString(Formatter $formatter): void
98
    {
99 62
        $formatter->setKeyAsString();
100 62
    }
101
102 62
    protected function getCase(): int
103
    {
104 62
        return Config::case();
105
    }
106
}
107