ModelGenerator::getRootNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yeelight\Generators;
4
5
use Yeelight\Generators\Migrations\SchemaParser;
6
7
/**
8
 * Class ModelGenerator
9
 *
10
 * @category Yeelight
11
 *
12
 * @package Yeelight\Generators
13
 *
14
 * @author Sheldon Lee <[email protected]>
15
 *
16
 * @license https://opensource.org/licenses/MIT MIT
17
 *
18
 * @link https://www.yeelight.com
19
 */
20
class ModelGenerator extends Generator
21
{
22
    /**
23
     * Get stub name.
24
     *
25
     * @var string
26
     */
27
    protected $stub = 'model';
28
29
    /**
30
     * Get root namespace.
31
     *
32
     * @return string
33
     */
34
    public function getRootNamespace()
35
    {
36
        return parent::getRootNamespace() .
37
            parent::getConfigGeneratorClassPath($this->getPathConfigNode());
0 ignored issues
show
Bug introduced by
Are you sure parent::getConfigGenerat...s->getPathConfigNode()) of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
            /** @scrutinizer ignore-type */ parent::getConfigGeneratorClassPath($this->getPathConfigNode());
Loading history...
38
    }
39
40
    /**
41
     * Get generator path config node.
42
     *
43
     * @return string
44
     */
45
    public function getPathConfigNode()
46
    {
47
        return 'models';
48
    }
49
50
    /**
51
     * Get destination path for generated file.
52
     *
53
     * @return string
54
     */
55
    public function getPath()
56
    {
57
        return $this->getBasePath() .
58
            '/' .
59
            parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) .
0 ignored issues
show
Bug introduced by
Are you sure parent::getConfigGenerat...PathConfigNode(), true) of type Illuminate\Config\Repository|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            /** @scrutinizer ignore-type */ parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) .
Loading history...
60
            '/' .
61
            $this->getName() .
62
            '.php';
63
    }
64
65
    /**
66
     * Get base path of destination file.
67
     *
68
     * @return string
69
     */
70
    public function getBasePath()
71
    {
72
        return config('repository.generator.basePath', app_path());
73
    }
74
75
    /**
76
     * Get array replacements.
77
     *
78
     * @return array
79
     */
80
    public function getReplacements()
81
    {
82
        return array_merge(
83
            parent::getReplacements(),
84
            [
85
                'fillable' => $this->getFillable(),
86
                'use_base_model' => 'use ' . $this->getRootNamespace() . '\BaseModel;',
87
                '_id_name' => $this->getIdName(),
88
            ]
89
        );
90
    }
91
92
    /**
93
     * Get the fillable attributes.
94
     *
95
     * @return string
96
     */
97
    public function getFillable()
98
    {
99
        if (!$this->fillable) {
0 ignored issues
show
Bug Best Practice introduced by
The property fillable does not exist on Yeelight\Generators\ModelGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
100
            return '[]';
101
        }
102
        $results = '['.PHP_EOL;
103
104
        foreach ($this->getSchemaParser()->toArray() as $column => $value) {
105
            $results .= "\t\t'{$column}',".PHP_EOL;
106
        }
107
108
        return $results."\t".']';
109
    }
110
111
    /**
112
     * Get schema parser.
113
     *
114
     * @return SchemaParser
115
     */
116
    public function getSchemaParser()
117
    {
118
        return new SchemaParser($this->fillable);
0 ignored issues
show
Bug Best Practice introduced by
The property fillable does not exist on Yeelight\Generators\ModelGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
119
    }
120
}
121