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

LanguageService::writeFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 6
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