Completed
Pull Request — master (#8)
by David
20:39
created

PivotTableMethodsDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6
use Doctrine\DBAL\Schema\Table;
7
8
class PivotTableMethodsDescriptor implements MethodDescriptorInterface
9
{
10
    /**
11
     * @var Table
12
     */
13
    private $pivotTable;
14
15
    private $useAlternateName = false;
16
17
    /**
18
     * @var ForeignKeyConstraint
19
     */
20
    private $localFk;
21
22
    /**
23
     * @var ForeignKeyConstraint
24
     */
25
    private $remoteFk;
26
    /**
27
     * @var NamingStrategyInterface
28
     */
29
    private $namingStrategy;
30
31
    /**
32
     * @param Table $pivotTable The pivot table
33
     * @param ForeignKeyConstraint $localFk
34
     * @param ForeignKeyConstraint $remoteFk
35
     * @param NamingStrategyInterface $namingStrategy
36
     */
37
    public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy)
38
    {
39
        $this->pivotTable = $pivotTable;
40
        $this->localFk = $localFk;
41
        $this->remoteFk = $remoteFk;
42
        $this->namingStrategy = $namingStrategy;
43
    }
44
45
    /**
46
     * Requests the use of an alternative name for this method.
47
     */
48
    public function useAlternativeName()
49
    {
50
        $this->useAlternateName = true;
51
    }
52
53
    /**
54
     * Returns the name of the method to be generated.
55
     *
56
     * @return string
57
     */
58
    public function getName() : string
59
    {
60
        if (!$this->useAlternateName) {
61
            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
62
        } else {
63
            return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
64
        }
65
    }
66
67
    /**
68
     * Returns the name of the class that will be returned by the getter (short name).
69
     *
70
     * @return string
71
     */
72
    public function getBeanClassName(): string
73
    {
74
        return $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName());
75
    }
76
77
    /**
78
     * Returns the plural name.
79
     *
80
     * @return string
81
     */
82 View Code Duplication
    private function getPluralName() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (!$this->useAlternateName) {
85
            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName());
86
        } else {
87
            return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
88
        }
89
    }
90
91
    /**
92
     * Returns the singular name.
93
     *
94
     * @return string
95
     */
96 View Code Duplication
    private function getSingularName() : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if (!$this->useAlternateName) {
99
            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName()));
100
        } else {
101
            return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName());
102
        }
103
    }
104
105
    /**
106
     * Returns the code of the method.
107
     *
108
     * @return string
109
     */
110
    public function getCode() : string
111
    {
112
        $singularName = $this->getSingularName();
113
        $pluralName = $this->getPluralName();
114
        $remoteBeanName = $this->getBeanClassName();
115
        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
116
        $pluralVariableName = $variableName.'s';
117
118
        $str = '    /**
119
     * Returns the list of %s associated to this bean via the %s pivot table.
120
     *
121
     * @return %s[]
122
     */
123
    public function get%s() : array
124
    {
125
        return $this->_getRelationships(%s);
126
    }
127
';
128
129
        $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true));
130
131
        $str = '    /**
132
     * Adds a relationship with %s associated to this bean via the %s pivot table.
133
     *
134
     * @param %s %s
135
     */
136
    public function add%s(%s %s) : void
137
    {
138
        $this->addRelationship(%s, %s);
139
    }
140
';
141
142
        $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
143
144
        $str = '    /**
145
     * Deletes the relationship with %s associated to this bean via the %s pivot table.
146
     *
147
     * @param %s %s
148
     */
149
    public function remove%s(%s %s) : void
150
    {
151
        $this->_removeRelationship(%s, %s);
152
    }
153
';
154
155
        $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
156
157
        $str = '    /**
158
     * Returns whether this bean is associated with %s via the %s pivot table.
159
     *
160
     * @param %s %s
161
     * @return bool
162
     */
163
    public function has%s(%s %s) : bool
164
    {
165
        return $this->hasRelationship(%s, %s);
166
    }
167
';
168
169
        $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName);
170
171
        $str = '    /**
172
     * Sets all relationships with %s associated to this bean via the %s pivot table.
173
     * Exiting relationships will be removed and replaced by the provided relationships.
174
     *
175
     * @param %s[] %s
176
     */
177
    public function set%s(array %s) : void
178
    {
179
        $this->setRelationships(%s, %s);
180
    }
181
';
182
183
        $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName);
184
185
        $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode;
186
187
        return $code;
188
    }
189
190
    /**
191
     * Returns an array of classes that needs a "use" for this method.
192
     *
193
     * @return string[]
194
     */
195
    public function getUsedClasses() : array
196
    {
197
        return [$this->getBeanClassName()];
198
    }
199
200
    /**
201
     * Returns the code to past in jsonSerialize.
202
     *
203
     * @return string
204
     */
205
    public function getJsonSerializeCode() : string
206
    {
207
        $remoteBeanName = $this->getBeanClassName();
208
        $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName);
209
210
        return '        if (!$stopRecursion) {
211
            $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') {
212
                return '.$variableName.'->jsonSerialize(true);
213
            }, $this->'.$this->getName().'());
214
        }
215
';
216
    }
217
}
218