BindingsGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 31
dl 0
loc 117
rs 10
c 3
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 9 1
A getRootNamespace() 0 3 1
A getEloquentRepository() 0 12 1
A getPathConfigNode() 0 3 1
A getBasePath() 0 3 1
A getRepository() 0 12 1
A getPath() 0 3 1
A getReplacements() 0 7 1
1
<?php
2
namespace Salah3id\Domains\Repository\Generators;
3
4
/**
5
 * Class BindingsGenerator
6
 * @package Salah3id\Domains\Repository\Generators
7
 * @author Anderson Andrade <[email protected]>
8
 */
9
class BindingsGenerator extends Generator
10
{
11
12
    /**
13
     * The placeholder for repository bindings
14
     *
15
     * @var string
16
     */
17
    public $bindPlaceholder = '//:end-bindings:';
18
    /**
19
     * Get stub name.
20
     *
21
     * @var string
22
     */
23
    protected $stub = 'bindings/bindings';
24
25
    public function run()
26
    {
27
28
29
        // Add entity repository binding to the repository service provider
30
        $provider = \File::get($this->getPath());
31
        $repositoryInterface = '\\' . $this->getRepository() . "::class";
32
        $repositoryEloquent = '\\' . $this->getEloquentRepository() . "::class";                                                                      
33
        \File::put($this->getPath(), str_replace($this->bindPlaceholder, "$repositoryInterface => $repositoryEloquent," . PHP_EOL . '        ' . $this->bindPlaceholder, $provider));
34
    }
35
36
    /**
37
     * Get destination path for generated file.
38
     *
39
     * @return string
40
     */
41
    public function getPath()
42
    {
43
        return $this->getBasePath() . '/Providers/' . parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . '.php';
44
    }
45
46
    /**
47
     * Get base path of destination file.
48
     *
49
     * @return string
50
     */
51
    public function getBasePath()
52
    {
53
        return $this->domainPath;
54
    }
55
56
    /**
57
     * Get generator path config node.
58
     *
59
     * @return string
60
     */
61
    public function getPathConfigNode()
62
    {
63
        return 'provider';
64
    }
65
66
    /**
67
     * Gets repository full class name
68
     *
69
     * @return string
70
     */
71
    public function getRepository()
72
    {
73
        $repositoryGenerator = new RepositoryInterfaceGenerator([
74
            'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Salah3id\Domains\Reposit...ators\BindingsGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
75
        ],$this->domain,$this->domainPath);
76
77
        $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName();
78
79
        return str_replace([
80
            "\\",
81
            '/'
82
        ], '\\', $repository) . 'Repository';
83
    }
84
85
    /**
86
     * Gets eloquent repository full class name
87
     *
88
     * @return string
89
     */
90
    public function getEloquentRepository()
91
    {
92
        $repositoryGenerator = new RepositoryEloquentGenerator([
93
            'name' => $this->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on Salah3id\Domains\Reposit...ators\BindingsGenerator. Since you implemented __get, consider adding a @property annotation.
Loading history...
94
        ],$this->domain,$this->domainPath);
95
96
        $repository = $repositoryGenerator->getRootNamespace() . '\\' . $repositoryGenerator->getName();
97
98
        return str_replace([
99
            "\\",
100
            '/'
101
        ], '\\', $repository) . 'RepositoryEloquent';
102
    }
103
104
    /**
105
     * Get root namespace.
106
     *
107
     * @return string
108
     */
109
    public function getRootNamespace()
110
    {
111
        return parent::getRootNamespace() . parent::getConfigGeneratorClassPath($this->getPathConfigNode());
112
    }
113
114
    /**
115
     * Get array replacements.
116
     *
117
     * @return array
118
     */
119
    public function getReplacements()
120
    {
121
122
        return array_merge(parent::getReplacements(), [
123
            'repository' => $this->getRepository(),
124
            'eloquent' => $this->getEloquentRepository(),
125
            'placeholder' => $this->bindPlaceholder,
126
        ]);
127
    }
128
}
129