Passed
Push — master ( 3cb518...ba9ea6 )
by Thomas
40s
created

LanguageService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 61
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 10 1
A getName() 0 4 1
A writeFile() 0 13 2
A getFilePath() 0 4 1
A getTranslationFileContent() 0 10 1
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Services;
4
5
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
6
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
8
9
class LanguageService extends ServiceAbstract implements ServiceInterface
10
{
11
    private $languageFile;
12
13
    private $translation = [];
14
15
    private $currentLanguage;
16
17
    public function call()
18
    {
19
        $this->currentLanguage = \Lang::locale();
20
        $this->relativeToBasePath = 'resources/lang/' . $this->currentLanguage;
21
        $this->languageFile = $this->getFilePath();
22
23
        $this->writeFile($this->getName($this->command->entity));
24
25
        $this->addLatestFileToIdeStack();
26
    }
27
28
    public function getName(string $entity): string
29
    {
30
        return snake_case($entity);
31
    }
32
33
    /**
34
     * Build the class with the given name.
35
     *
36
     * @param string $name
37
     *
38
     * @return string
39
     */
40
    private function writeFile($name)
41
    {
42
        if ($this->filesystem->exists($this->languageFile)) {
43
            $this->translation = include $this->languageFile;
44
        }
45
46
        $this->translation = array_add($this->translation, $name, [
47
            'singular' => ucfirst($name),
48
            'plural'   => ucfirst(str_plural($name)),
49
        ]);
50
51
        return $this->filesystem->put($this->languageFile, $this->getTranslationFileContent());
52
    }
53
54
    private function getFilePath()
55
    {
56
        return resource_path('lang/' . $this->currentLanguage) . '/models.php';
57
    }
58
59
    private function getTranslationFileContent()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
60
    {
61
        $content = ShortSyntaxArray::parse($this->translation);
62
63
        return <<<FILE
64
<?php
65
        
66
return {$content};
67
FILE;
68
    }
69
}
70