Completed
Push — master ( d23403...374141 )
by Rai
03:12
created

BaseEntity::preFlush()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
rs 9.4285
1
<?php
2
3
namespace Bludata\Doctrine\ODM\MongoDB\Entities;
4
5
use Bludata\Doctrine\Common\Interfaces\BaseEntityInterface;
6
use Bludata\Doctrine\Common\Interfaces\EntityManagerInterface;
7
use Bludata\Doctrine\Common\Interfaces\EntityTimestampInterface;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
10
use Doctrine\ODM\MongoDB\PersistentCollection;
11
12
/**
13
 * @ODM\MappedSuperclass
14
 * @ODM\HasLifecycleCallbacks
15
 */
16
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...
17
{
18
    /**
19
     * @ODM\Id
20
     */
21
    protected $id;
22
23
    /**
24
     * @var \DateTime
25
     *
26
     * @ODM\Field(type="timestamp", name="createdAt")
27
     */
28
    protected $createdAt;
29
30
    /**
31
     * @var \DateTime
32
     *
33
     * @ODM\Field(type="timestamp", name="updatedAt")
34
     */
35
    protected $updatedAt;
36
37
    /**
38
     * @ODM\Field(type="timestamp", nullable=true, name="deletedAt")
39
     */
40
    protected $deletedAt;
41
42
    public function getCreatedAt()
43
    {
44
        return $this->createdAt;
45
    }
46
47
    public function getUpdatedAt()
48
    {
49
        return $this->updatedAt;
50
    }
51
52
    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...
53
    {
54
        return $this->deletedAt;
55
    }
56
57
    public function setDeletedAt($deletedAt)
58
    {
59
        $this->deletedAt = $deletedAt;
60
    }
61
62
    /**
63
     * Altera o campo updatedAt para forçar o persist da entity.
64
     */
65
    public function forcePersist()
66
    {
67
        $this->updatedAt = new \DateTime();
68
69
        return $this;
70
    }
71
72
    /**
73
     * @ODM\PrePersist
74
     */
75 View Code Duplication
    public function prePersist()
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...
76
    {
77
        $repository = $this->getRepository();
78
79
        if (method_exists($repository, 'preSave')) {
80
            $repository->preSave($this);
81
        }
82
83
        if (method_exists($repository, 'preSave')) {
84
            $repository->validate($this);
85
        }
86
    }
87
88
    /**
89
     * @ODM\PostPersist
90
     */
91 View Code Duplication
    public function postPersist()
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...
92
    {
93
        $repository = $this->getRepository();
94
95
        if (method_exists($repository, 'postSave')) {
96
            $repository->postSave($this);
97
        }
98
99
        if (method_exists($repository, 'postSave')) {
100
            $repository->validate($this);
101
        }
102
    }
103
104
    /**
105
     * @ODM\PreUpdate
106
     */
107 View Code Duplication
    public function preUpdate()
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...
108
    {
109
        $repository = $this->getRepository();
110
111
        if (method_exists($repository, 'preSave')) {
112
            $repository->preSave($this);
113
        }
114
115
        if (method_exists($repository, 'preSave')) {
116
            $repository->validate($this);
117
        }
118
    }
119
120
    /**
121
     * @ODM\PostUpdate
122
     */
123 View Code Duplication
    public function postUpdate()
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...
124
    {
125
        $repository = $this->getRepository();
126
127
        if (method_exists($repository, 'postSave')) {
128
            $repository->postSave($this);
129
        }
130
131
        if (method_exists($repository, 'postSave')) {
132
            $repository->validate($this);
133
        }
134
    }
135
136
    /**
137
     * @ORM\PreFlush
138
     */
139
    public function preFlush()
140
    {
141
        $repository = $this->getRepository();
142
143
        if (method_exists($repository, 'preFlush')) {
144
            $repository->preFlush($this);
145
        }
146
    }
147
148
    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...
149
    {
150
        return app(EntityManagerInterface::class)->getRepository(get_class($this));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 83 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...
151
    }
152
153
    public function save($flush = false)
154
    {
155
        $this->getRepository()->save($this);
156
157
        return $this;
158
    }
159
160
    public function remove()
161
    {
162
        $this->getRepository()->remove($this);
163
164
        return $this;
165
    }
166
167
    public function flush($all = true)
168
    {
169
        $this->getRepository()->flush($all ? null : $this);
170
171
        return $this;
172
    }
173
174
    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...
175
    {
176
        return $this->id;
177
    }
178
179
    protected function getFillable()
180
    {
181
        return ['id', 'createdAt', 'updatedAt', 'deletedAt'];
182
    }
183
184
    /**
185
     * 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...
186
     * É usado principalmente em $this->setPropertiesEntity e nos Controllers.
187
     * 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...
188
     *
189
     * @return array
190
     */
191
    abstract public function getOnlyStore();
192
193
    /**
194
     * 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...
195
     * Por padrão retorna os mesmos valores de $this->getOnlyStore().
196
     * Este método pode ser sobrescrito nas classes filhas.
197
     * É usado principalmente em $this->setPropertiesEntity e nos Controllers.
198
     * 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...
199
     *
200
     * @return array
201
     */
202
    public function getOnlyUpdate()
203
    {
204
        return $this->getOnlyStore();
205
    }
206
207
    public function setPropertiesEntity(array $data)
208
    {
209
        foreach ($data as $key => $value) {
210
            $set = true;
211
212 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...
213
                ((!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...
214
                ||
215
                (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...
216
            ) {
217
                $set = false;
218
            }
219
220
            $method = 'set'.ucfirst($key);
221
222
            if (method_exists($this, $method) && $set) {
223
                $this->$method($value);
224
            }
225
        }
226
227
        return $this;
228
    }
229
230 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...
231
    {
232
        if (
233
            $options
234
            &&
235
            (
236
                (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...
237
                ||
238
                (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...
239
            )
240
        ) {
241
            return false;
242
        }
243
244
        return true;
245
    }
246
247
    public function toArray(array $options = null)
248
    {
249
        $classMetadata = $this->getRepository()->getClassMetadata();
250
        $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...
251
252
        foreach ($this->getFillable() as $key) {
253
            if ($this->checkOnyExceptInArray($key, $options)) {
254
                if (is_object($this->$key) && $this->$key instanceof \DateTime || $this->$key instanceof \MongoTimestamp) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 123 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...
255
                    $metaDataKey = $classMetadata->hasField($key) ? $classMetadata->getFieldMapping($key) : null;
0 ignored issues
show
Unused Code introduced by
$metaDataKey is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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...
256
257
                    if ($this->$key) {
258
                        $dateFormat = 'Y-m-d H:i:s';
259
260
                        if ($this->$key instanceof \MongoTimestamp) {
261
                            $timestamp = new \DateTime();
262
                            $timestamp->setTimestamp($this->$key->sec);
263
                            $this->$key = $timestamp;
264
                        }
265
266
                        $array[$key] = $this->$key->format($dateFormat);
267
                    }
268 View Code Duplication
                } elseif (is_object($this->$key) && $this->$key instanceof ArrayCollection || $this->$key instanceof PersistentCollection) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ODM\MongoDB\PersistentCollection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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...
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 140 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...
269
                    $ids = [];
270
                    foreach ($this->$key->getValues() as $item) {
271
                        $ids[] = $item->getId();
272
                    }
273
                    $array[$key] = $ids;
274
                } elseif (method_exists($this->$key, 'getId')) {
275
                    $array[$key] = $this->$key->getId();
276
                } else {
277
                    $array[$key] = $this->$key;
278
                }
279
            }
280
        }
281
282
        return $array;
283
    }
284
}
285