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

Localization   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
eloc 15
c 4
b 0
f 0
dl 0
loc 67
ccs 16
cts 17
cp 0.9412
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 6 1
A delete() 0 5 2
A makeProcess() 0 7 2
A publish() 0 5 2
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