Passed
Pull Request — master (#2)
by David
03:49
created

AbstractBeanPropertyDescriptor   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 137
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A useAlternativeName() 0 4 1
getClassName() 0 1 ?
getParamAnnotation() 0 1 ?
A getVariableName() 0 4 1
A getSetterName() 0 4 1
A getGetterName() 0 4 1
A getConstructorAssignCode() 0 6 1
isCompulsory() 0 1 ?
hasDefault() 0 1 ?
assignToDefaultCode() 0 1 ?
isPrimaryKey() 0 1 ?
A getTable() 0 4 1
getGetterSetterCode() 0 1 ?
getJsonSerializeCode() 0 1 ?
A isAlternativeName() 0 4 1
1
<?php
2
3
namespace TheCodingMachine\TDBM\Utils;
4
5
use Doctrine\DBAL\Schema\Table;
6
7
/**
8
 * This class represent a property in a bean (a property has a getter, a setter, etc...).
9
 */
10
abstract class AbstractBeanPropertyDescriptor
11
{
12
    /**
13
     * @var Table
14
     */
15
    protected $table;
16
17
    /**
18
     * Whether to use the more complex name in case of conflict.
19
     *
20
     * @var bool
21
     */
22
    protected $alternativeName = false;
23
    /**
24
     * @var NamingStrategyInterface
25
     */
26
    protected $namingStrategy;
27
28
    /**
29
     * @param Table $table
30
     * @param NamingStrategyInterface $namingStrategy
31
     */
32
    public function __construct(Table $table, NamingStrategyInterface $namingStrategy)
33
    {
34
        $this->table = $table;
35
        $this->namingStrategy = $namingStrategy;
36
    }
37
38
    /**
39
     * Use the more complex name in case of conflict.
40
     */
41
    public function useAlternativeName()
42
    {
43
        $this->alternativeName = true;
44
    }
45
46
    /**
47
     * Returns the name of the class linked to this property or null if this is not a foreign key.
48
     *
49
     * @return null|string
50
     */
51
    abstract public function getClassName();
52
53
    /**
54
     * Returns the param annotation for this property (useful for constructor).
55
     *
56
     * @return string
57
     */
58
    abstract public function getParamAnnotation();
59
60
    public function getVariableName()
61
    {
62
        return $this->namingStrategy->getVariableName($this);
63
    }
64
65
    public function getSetterName()
66
    {
67
        return $this->namingStrategy->getSetterName($this);
68
    }
69
70
    public function getGetterName()
71
    {
72
        return $this->namingStrategy->getGetterName($this);
73
    }
74
75
    /**
76
     * Returns the PHP code used in the ben constructor for this property.
77
     *
78
     * @return string
79
     */
80
    public function getConstructorAssignCode()
81
    {
82
        $str = '        $this->%s(%s);';
83
84
        return sprintf($str, $this->getSetterName(), $this->getVariableName());
85
    }
86
87
    /**
88
     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
89
     *
90
     * @return bool
91
     */
92
    abstract public function isCompulsory();
93
94
    /**
95
     * Returns true if the property has a default value.
96
     *
97
     * @return bool
98
     */
99
    abstract public function hasDefault();
100
101
    /**
102
     * Returns the code that assigns a value to its default value.
103
     *
104
     * @return string
105
     *
106
     * @throws \TDBMException
107
     */
108
    abstract public function assignToDefaultCode();
109
110
    /**
111
     * Returns true if the property is the primary key.
112
     *
113
     * @return bool
114
     */
115
    abstract public function isPrimaryKey();
116
117
    /**
118
     * @return Table
119
     */
120
    public function getTable()
121
    {
122
        return $this->table;
123
    }
124
125
    /**
126
     * Returns the PHP code for getters and setters.
127
     *
128
     * @return string
129
     */
130
    abstract public function getGetterSetterCode();
131
132
    /**
133
     * Returns the part of code useful when doing json serialization.
134
     *
135
     * @return string
136
     */
137
    abstract public function getJsonSerializeCode();
138
139
    /**
140
     * @return bool
141
     */
142
    public function isAlternativeName(): bool
143
    {
144
        return $this->alternativeName;
145
    }
146
}
147