Completed
Push — master ( c1f454...99d563 )
by Rai
02:41
created

BaseRepository   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 171
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

17 Methods

Rating   Name   Duplication   Size   Complexity  
A preSave() 4 4 1
A postSave() 4 4 1
getMessageNotFound() 1 1 ?
A validate() 20 20 3
A getClassMetadata() 4 4 1
A getEntityName() 4 4 1
A createEntity() 4 4 1
A createQueryWorker() 4 4 1
A query() 4 4 1
A findAll() 4 4 1
A findOneBy() 10 10 3
A find() 4 4 2
C findOrCreate() 26 26 7
A remove() 8 8 1
A save() 6 6 1
A flush() 6 6 1
A em() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bludata\Doctrine\ODM\MongoDB\Repositories;
4
5
use Bludata\Doctrine\Common\Interfaces\BaseEntityInterface;
6
use Bludata\Doctrine\Common\Interfaces\BaseRepositoryInterface;
7
use Bludata\Doctrine\ORM\Repositories\QueryWorker;
8
use Doctrine\ODM\MongoDB\DocumentRepository;
9
use Symfony\Component\Validator\ValidatorBuilder;
10
11 View Code Duplication
abstract class BaseRepository extends DocumentRepository implements BaseRepositoryInterface
0 ignored issues
show
Duplication introduced by
This class 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...
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...
12
{
13
    /**
14
     * Método executado nos eventos ORM\PrePersist e ORM\PreUpdate
15
     */
16
    public function preSave(BaseEntityInterface $entity)
17
    {
18
        return $this;
19
    }
20
21
    /**
22
     * Método executado nos eventos ORM\PostPersist e ORM\PostUpdate
23
     */
24
    public function postSave(BaseEntityInterface $entity)
25
    {
26
        return $this;
27
    }
28
29
    abstract public function getMessageNotFound();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
30
31
    public function validate(BaseEntityInterface $entity)
32
    {
33
        $validator = (new ValidatorBuilder())
34
                    ->enableAnnotationMapping()
35
                    ->getValidator();
36
37
        $violations = $validator->validate($entity);
38
39
        $errors = [];
40
41
        if (count($violations)) {
42
            foreach ($violations as $violation) {
43
                $errors[] = $violation->getMessage();
44
            }
45
46
            abort(400, json_encode($errors));
47
        }
48
49
        return true;
50
    }
51
52
    public function getClassMetadata()
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 parent::getClassMetadata();
55
    }
56
57
    public function getEntityName()
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...
58
    {
59
        return parent::getDocumentName();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDocumentName() instead of getEntityName()). Are you sure this is correct? If so, you might want to change this to $this->getDocumentName().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
60
    }
61
62
    public function createEntity()
63
    {
64
        return app($this->getEntityName());
65
    }
66
67
    public function createQueryWorker()
68
    {
69
        return new QueryWorker($this);
70
    }
71
72
    public function query()
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...
73
    {
74
        return $this->createQueryBuilder('t');
75
    }
76
77
    /**
78
     * @return QueryWorker
79
     */
80
    public function findAll()
81
    {
82
        return $this->createQueryWorker();
83
    }
84
85
    public function findOneBy(array $filters, $abort = true)
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...
86
    {
87
        $entity = parent::findOneBy($filters);
88
89
        if (!$entity && $abort) {
90
            abort(404, $this->getMessageNotFound());
91
        }
92
93
        return $entity;
94
    }
95
96
    public function find($id, $abort = true)
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...
97
    {
98
        return is_object($id) ? $id : $this->findOneBy(['id' => $id], $abort);
99
    }
100
101
    /**
102
     * Inserir ou atualizar um registro.
103
     *
104
     * @param null | string | int | array
105
     *
106
     * @throws InvalidArgumentException Se $input não for null | string | int | array é lançada a exceção
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...
107
     */
108
    public function findOrCreate($input)
109
    {
110
        if (is_null($input)) {
111
            return $input;
112
        }
113
114
        if (is_string($input)) {
115
            if ($decoded = json_decode($input, true)) {
116
                $input = $decoded;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $input. This often makes code more readable.
Loading history...
117
            }
118
        }
119
120
        if (is_array($input)) {
121
            if (array_key_exists('id', $input) && $input['id']) {
122
                $object = $this->find($input['id']);
123
            } else {
124
                $object = $this->createEntity();
125
            }
126
127
            $object->setPropertiesEntity($input);
128
129
            return $object;
130
        }
131
132
        return $this->find($input);
133
    }
134
135
    /**
136
     * Marcar um registro como deletado.
137
     *
138
     * @param object | int $target
139
     *
140
     * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Se $target não for encontrado
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...
141
     *
142
     * @return Bludata\Entities\BaseEntityInterface
143
     */
144
    public function remove($target)
145
    {
146
        $entity = $this->find($target);
147
148
        $this->em()->remove($entity);
149
150
        return $entity;
151
    }
152
153
    /**
154
     * @param Bludata\Entities\BaseEntityInterface $entity
0 ignored issues
show
Documentation introduced by
Should the type for parameter $entity not be BaseEntityInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
155
     *
156
     * @return Bludata\Repositories\QueryWorker
157
     */
158
    public function save(BaseEntityInterface $entity)
159
    {
160
        $this->em()->persist($entity);
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param Bludata\Entities\BaseEntityInterface $entity
0 ignored issues
show
Documentation introduced by
Should the type for parameter $entity not be null|BaseEntityInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
167
     *
168
     * @return Bludata\Repositories\QueryWorker
169
     */
170
    public function flush(BaseEntityInterface $entity = null)
171
    {
172
        $this->em()->flush($entity);
173
174
        return $this;
175
    }
176
177
    public function em()
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...
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
178
    {
179
        return parent::getDocumentManager();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getDocumentManager() instead of em()). Are you sure this is correct? If so, you might want to change this to $this->getDocumentManager().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
180
    }
181
}
182