Passed
Push — master ( c97482...68f9e3 )
by Andrey
07:00 queued 04:30
created

Localization::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services;
4
5
use Helldar\LaravelLangPublisher\Contracts\Process;
6
use Helldar\LaravelLangPublisher\Exceptions\NoProcessInstanceException;
7
use Helldar\LaravelLangPublisher\Services\Processing\DeleteJson;
8
use Helldar\LaravelLangPublisher\Services\Processing\DeletePhp;
9
use Helldar\LaravelLangPublisher\Services\Processing\PublishJson;
10
use Helldar\LaravelLangPublisher\Services\Processing\PublishPhp;
11
12
final class Localization
13
{
14
    /** @var array */
15
    protected $result = [];
16
17
    /**
18
     * @param  string  $locale
19
     * @param  bool  $force
20
     * @param  bool  $json
21
     *
22
     * @throws \Helldar\LaravelLangPublisher\Exceptions\NoProcessInstanceException
23
     *
24
     * @return array
25
     */
26 54
    public function publish(string $locale, bool $force = false, bool $json = false): array
27
    {
28 54
        return $json
29 30
            ? $this->process(PublishJson::class, $locale, $force)
30 42
            : $this->process(PublishPhp::class, $locale, $force);
31
    }
32
33
    /**
34
     * @param  string  $locale
35
     * @param  bool  $json
36
     *
37
     * @throws \Helldar\LaravelLangPublisher\Exceptions\NoProcessInstanceException
38
     *
39
     * @return array
40
     */
41 24
    public function delete(string $locale, bool $json = false): array
42
    {
43 24
        return $json
44 12
            ? $this->process(DeleteJson::class, $locale)
45 24
            : $this->process(DeletePhp::class, $locale);
46
    }
47
48
    /**
49
     * @param  string  $classname
50
     * @param  string  $locale
51
     * @param  bool  $force
52
     *
53
     * @throws \Helldar\LaravelLangPublisher\Exceptions\NoProcessInstanceException
54
     *
55
     * @return mixed
56
     */
57 78
    protected function process(string $classname, string $locale, bool $force = false)
58
    {
59 78
        return $this->makeProcess($classname)
60 78
            ->locale($locale)
61 78
            ->force($force)
62 78
            ->run();
63
    }
64
65
    /**
66
     * @param  string  $classname
67
     *
68
     * @throws \Helldar\LaravelLangPublisher\Exceptions\NoProcessInstanceException
69
     *
70
     * @return Process
71
     */
72 78
    protected function makeProcess(string $classname): Process
73
    {
74 78
        if (! is_subclass_of($classname, Process::class)) {
75
            throw new NoProcessInstanceException($classname);
76
        }
77
78 78
        return app($classname);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app($classname) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return Helldar\LaravelLangPublisher\Contracts\Process. Consider adding an additional type-check to rule them out.
Loading history...
79
    }
80
}
81