Completed
Push — master ( 259875...8de2a5 )
by Rai
10:00
created

BaseEntity::getDiscr()   A

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 Bludata\Doctrine\ORM\Entities;
4
5
use Bludata\Doctrine\Common\Interfaces\BaseEntityInterface;
6
use Bludata\Doctrine\Common\Interfaces\EntityTimestampInterface;
7
use DateTime;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ORM\Mapping as ORM;
10
use Doctrine\ORM\PersistentCollection;
11
use EntityManager;
12
use Gedmo\Mapping\Annotation as Gedmo;
13
14
/**
15
 * @ORM\MappedSuperclass
16
 * @ORM\HasLifecycleCallbacks
17
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
18
 */
19
abstract class BaseEntity implements BaseEntityInterface, EntityTimestampInterface
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 82 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
20
{
21
    /**
22
     * @ORM\Id
23
     * @ORM\Column(type="integer", name="id")
24
     * @ORM\GeneratedValue
25
     */
26
    protected $id;
27
28
    /**
29
     * @var \DateTime
30
     *
31
     * @Gedmo\Timestampable(on="create")
32
     * @ORM\Column(type="datetime", name="createdAt")
33
     */
34
    private $createdAt;
35
36
    /**
37
     * @var \DateTime
38
     *
39
     * @Gedmo\Timestampable(on="update")
40
     * @ORM\Column(type="datetime", name="updatedAt")
41
     */
42
    private $updatedAt;
43
44
    /**
45
     * @ORM\Column(type="datetime", nullable=true, name="deletedAt")
46
     */
47
    private $deletedAt;
48
49
    public function getCreatedAt()
50
    {
51
        return $this->createdAt;
52
    }
53
54
    public function getUpdatedAt()
55
    {
56
        return $this->updatedAt;
57
    }
58
59
    public function getDeletedAt()
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
        return $this->deletedAt;
62
    }
63
64
    public function setDeletedAt($deletedAt)
65
    {
66
        $this->deletedAt = $deletedAt;
67
    }
68
69
    public function getDiscr()
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...
70
    {
71
        return $this->getRepository()->getClassMetadata()->discriminatorValue;
72
    }
73
74
    public function getDiscrName()
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...
75
    {
76
        return $this->getRepository()->getClassMetadata()->discriminatorColumn['name'];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 87 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
77
    }
78
79
    /**
80
     * Altera o campo updatedAt para forçar o persist da entity.
81
     */
82
    public function forcePersist()
83
    {
84
        $this->updatedAt = new DateTime();
85
86
        return $this;
87
    }
88
89
    /**
90
     * @ORM\PrePersist
91
     */
92
    public function prePersist()
93
    {
94
        $this->getRepository()
95
             ->preSave($this)
96
             ->validate($this);
97
    }
98
99
    /**
100
     * @ORM\PreUpdate
101
     */
102
    public function preUpdate()
103
    {
104
        $this->getRepository()
105
             ->preSave($this)
106
             ->validate($this);
107
    }
108
109
    public function getRepository()
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...
110
    {
111
        return EntityManager::getRepository(get_class($this));
112
    }
113
114
    public function remove()
115
    {
116
        $this->getRepository()->remove($this);
117
118
        return $this;
119
    }
120
121
    public function save($flush = false)
122
    {
123
        $this->getRepository()->save($this);
124
125
        return $this;
126
    }
127
128
    public function flush($all = true)
129
    {
130
        $this->getRepository()->flush($all ? null : $this);
131
132
        return $this;
133
    }
134
135
    public function getId()
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...
136
    {
137
        return $this->id;
138
    }
139
140
    protected function getFillable()
141
    {
142
        return ['id', 'createdAt', 'updatedAt', 'deletedAt'];
143
    }
144
145
    /**
146
     * Retona um array com o nome das propriedade que o cliente pode setar para realizar o store
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 96 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
147
     * É usado principalmente em $this->setPropertiesEntity e nos Controllers.
148
     * Este método não evita que uma propriedade seja alterada caso tenha seu método set().
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 91 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
149
     *
150
     * @return array
151
     */
152
    abstract public function getOnlyStore();
153
154
    /**
155
     * Retona um array com o nome das propriedade que o cliente pode setar para realizar o update.
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 98 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
156
     * Por padrão retorna os mesmos valores de $this->getOnlyStore().
157
     * Este método pode ser sobrescrito nas classes filhas.
158
     * É usado principalmente em $this->setPropertiesEntity e nos Controllers.
159
     * Este método não evita que uma propriedade seja alterada caso tenha seu método set().
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 91 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
160
     *
161
     * @return array
162
     */
163
    public function getOnlyUpdate()
164
    {
165
        return $this->getOnlyStore();
166
    }
167
168
    public function setPropertiesEntity(array $data)
169
    {
170
        foreach ($data as $key => $value) {
171
            $set = true;
172
173 View Code Duplication
            if (
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
174
                ((!isset($data['id']) || !is_numeric($data['id'])) && !in_array($key, $this->getOnlyStore()))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 109 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
175
                ||
176
                (isset($data['id']) && is_numeric($data['id']) && !in_array($key, $this->getOnlyUpdate()))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 106 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
177
            ) {
178
                $set = false;
179
            }
180
181
            $method = 'set'.ucfirst($key);
182
183
            if (method_exists($this, $method) && $set) {
184
                $this->$method(is_string($value) && strlen($value) <= 0 ? null : $value);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 89 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
185
            }
186
        }
187
188
        return $this;
189
    }
190
191 View Code Duplication
    final protected function checkOnyExceptInArray($key, array $options = null)
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...
192
    {
193
        if (
194
            $options
195
            &&
196
            (
197
                (isset($options['only']) && is_array($options['only']) && !in_array($key, $options['only']))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 108 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
198
                ||
199
                (isset($options['except']) && is_array($options['except']) && in_array($key, $options['except']))
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
200
            )
201
        ) {
202
            return false;
203
        }
204
205
        return true;
206
    }
207
208
    public function toArray(array $options = null)
209
    {
210
        $classMetadata = $this->getRepository()->getClassMetadata();
211
        $array = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
212
213
        foreach ($this->getFillable() as $key) {
214
            $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 105 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
215
216
            if ($this->checkOnyExceptInArray($key, $options)) {
217
                if (is_object($this->$key)) {
218
                    if ($this->$key instanceof DateTime) {
219
                        if ($this->$key) {
220
                            $dateFormat = 'Y-m-d';
221
222
                            if ($metaDataKey) {
223
                                switch ($metaDataKey['type']) {
224
                                    case 'datetime':
225
                                        $dateFormat = 'Y-m-d H:i:s';
226
                                        break;
227
228
                                    case 'time':
229
                                        $dateFormat = 'H:i:s';
230
                                        break;
231
232
                                    default:
233
                                        break;
234
                                }
235
                            }
236
                            $array[$key] = $this->$key->format($dateFormat);
237
                        }
238
                    } elseif ($this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 118 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
239
                        $ids = [];
240
                        foreach ($this->$key->getValues() as $item) {
241
                            $ids[] = $item->getId();
242
                        }
243
                        $array[$key] = $ids;
244
                    } else {
245
                        $array[$key] = $this->$key->getId();
246
                    }
247
                } else {
248
                    if ($metaDataKey['type'] == 'decimal') {
249
                        $array[$key] = (float) $this->$key;
250
                    } else {
251
                        $array[$key] = $this->$key;
252
                    }
253
                }
254
            }
255
        }
256
257
        return $array;
258
    }
259
}
260