Code Duplication    Length = 177-179 lines in 2 locations

src/Doctrine/ODM/MongoDB/Repositories/BaseRepository.php 1 location

@@ 11-189 (lines=179) @@
8
use Doctrine\ODM\MongoDB\DocumentRepository;
9
use Symfony\Component\Validator\ValidatorBuilder;
10
11
abstract class BaseRepository extends DocumentRepository implements BaseRepositoryInterface
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
    /**
30
     * Método executado no evento ORM\PreFlush.
31
     */
32
    public function preFlush(BaseEntityInterface $entity)
33
    {
34
        return $this;
35
    }
36
37
    abstract public function getMessageNotFound();
38
39
    public function validate(BaseEntityInterface $entity)
40
    {
41
        $validator = (new ValidatorBuilder())
42
                    ->enableAnnotationMapping()
43
                    ->getValidator();
44
45
        $violations = $validator->validate($entity);
46
47
        $errors = [];
48
49
        if (count($violations)) {
50
            foreach ($violations as $violation) {
51
                $errors[] = $violation->getMessage();
52
            }
53
54
            abort(400, json_encode($errors));
55
        }
56
57
        return true;
58
    }
59
60
    public function getClassMetadata()
61
    {
62
        return parent::getClassMetadata();
63
    }
64
65
    public function getEntityName()
66
    {
67
        return parent::getDocumentName();
68
    }
69
70
    public function createEntity()
71
    {
72
        return app($this->getEntityName());
73
    }
74
75
    public function createQueryWorker()
76
    {
77
        return new QueryWorker($this);
78
    }
79
80
    public function query()
81
    {
82
        return $this->createQueryBuilder('t');
83
    }
84
85
    /**
86
     * @return QueryWorker
87
     */
88
    public function findAll()
89
    {
90
        return $this->createQueryWorker();
91
    }
92
93
    public function findOneBy(array $filters, $abort = true)
94
    {
95
        $entity = parent::findOneBy($filters);
96
97
        if (!$entity && $abort) {
98
            abort(404, $this->getMessageNotFound());
99
        }
100
101
        return $entity;
102
    }
103
104
    public function find($id, $abort = true)
105
    {
106
        return is_object($id) ? $id : $this->findOneBy(['id' => $id], $abort);
107
    }
108
109
    /**
110
     * Inserir ou atualizar um registro.
111
     *
112
     * @param null | string | int | array
113
     *
114
     * @throws InvalidArgumentException Se $input não for null | string | int | array é lançada a exceção
115
     */
116
    public function findOrCreate($input)
117
    {
118
        if (is_null($input)) {
119
            return $input;
120
        }
121
122
        if (is_string($input)) {
123
            if ($decoded = json_decode($input, true)) {
124
                $input = $decoded;
125
            }
126
        }
127
128
        if (is_array($input)) {
129
            if (array_key_exists('id', $input) && $input['id']) {
130
                $object = $this->find($input['id']);
131
            } else {
132
                $object = $this->createEntity();
133
            }
134
135
            $object->setPropertiesEntity($input);
136
137
            return $object;
138
        }
139
140
        return $this->find($input);
141
    }
142
143
    /**
144
     * Marcar um registro como deletado.
145
     *
146
     * @param object | int $target
147
     *
148
     * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Se $target não for encontrado
149
     *
150
     * @return Bludata\Entities\BaseEntityInterface
151
     */
152
    public function remove($target)
153
    {
154
        $entity = $this->find($target);
155
156
        $this->em()->remove($entity);
157
158
        return $entity;
159
    }
160
161
    /**
162
     * @param Bludata\Entities\BaseEntityInterface $entity
163
     *
164
     * @return Bludata\Repositories\QueryWorker
165
     */
166
    public function save(BaseEntityInterface $entity)
167
    {
168
        $this->em()->persist($entity);
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param Bludata\Entities\BaseEntityInterface $entity
175
     *
176
     * @return Bludata\Repositories\QueryWorker
177
     */
178
    public function flush(BaseEntityInterface $entity = null)
179
    {
180
        $this->em()->flush($entity);
181
182
        return $this;
183
    }
184
185
    public function em()
186
    {
187
        return parent::getDocumentManager();
188
    }
189
}
190

src/Doctrine/ORM/Repositories/BaseRepository.php 1 location

@@ 10-186 (lines=177) @@
7
use Doctrine\ORM\EntityRepository;
8
use Symfony\Component\Validator\ValidatorBuilder;
9
10
abstract class BaseRepository extends EntityRepository implements BaseRepositoryInterface
11
{
12
    /**
13
     * Método executado nos eventos ORM\PrePersist e ORM\PreUpdate.
14
     */
15
    public function preSave(BaseEntityInterface $entity)
16
    {
17
        return $this;
18
    }
19
20
    /**
21
     * Método executado nos eventos ORM\PostPersist e ORM\PostUpdate.
22
     */
23
    public function postSave(BaseEntityInterface $entity)
24
    {
25
        return $this;
26
    }
27
28
    /**
29
     * Método executado no evento ORM\PreFlush.
30
     */
31
    public function preFlush(BaseEntityInterface $entity)
32
    {
33
        return $this;
34
    }
35
36
    public function validate(BaseEntityInterface $entity)
37
    {
38
        $validator = (new ValidatorBuilder())
39
                    ->enableAnnotationMapping()
40
                    ->getValidator();
41
42
        $violations = $validator->validate($entity);
43
44
        $errors = [];
45
46
        if (count($violations)) {
47
            foreach ($violations as $violation) {
48
                $errors[] = $violation->getMessage();
49
            }
50
51
            abort(400, json_encode($errors));
52
        }
53
    }
54
55
    public function getClassMetadata()
56
    {
57
        return parent::getClassMetadata();
58
    }
59
60
    public function getEntityName()
61
    {
62
        return parent::getEntityName();
63
    }
64
65
    public function createEntity()
66
    {
67
        return app($this->getEntityName());
68
    }
69
70
    public function createQueryWorker()
71
    {
72
        return new QueryWorker($this);
73
    }
74
75
    public function query()
76
    {
77
        return $this->createQueryBuilder('t');
78
    }
79
80
    /**
81
     * @return QueryWorker
82
     */
83
    public function findAll()
84
    {
85
        return $this->createQueryWorker();
86
    }
87
88
    public function findOneBy(array $filters, $abort = true)
89
    {
90
        $entity = parent::findOneBy($filters);
91
92
        if (!$entity && $abort) {
93
            abort(404, $this->getMessageNotFound());
94
        }
95
96
        return $entity;
97
    }
98
99
    public function find($id, $abort = true)
100
    {
101
        return is_object($id) ? $id : $this->findOneBy(['id' => $id], $abort);
102
    }
103
104
    /**
105
     * Inserir ou atualizar um registro.
106
     *
107
     * @param null | string | int | array
108
     *
109
     * @throws InvalidArgumentException Se $input não for null | string | int | array é lançada a exceção
110
     */
111
    public function findOrCreate($input)
112
    {
113
        if (is_null($input)) {
114
            return $input;
115
        }
116
117
        if (is_string($input)) {
118
            $input = json_decode($input, true);
119
        }
120
121
        if (is_numeric($input)) {
122
            return $this->find($input);
123
        }
124
125
        if (is_array($input)) {
126
            if (array_key_exists('id', $input) && $input['id']) {
127
                $object = $this->find($input['id']);
128
            } else {
129
                $object = $this->createEntity();
130
            }
131
132
            $object->setPropertiesEntity($input);
133
134
            return $object;
135
        }
136
137
        throw new InvalidArgumentException('O parâmetro $input pode ser um null | string | int | array');
138
    }
139
140
    /**
141
     * Marcar um registro como deletado.
142
     *
143
     * @param object | int $target
144
     *
145
     * @throws Symfony\Component\HttpKernel\Exception\NotFoundHttpException Se $target não for encontrado
146
     *
147
     * Bludata\Doctrine\Common\Interfaces
148
     */
149
    public function remove($target)
150
    {
151
        $entity = $this->find($target);
152
153
        $this->em()->remove($entity);
154
155
        return $entity;
156
    }
157
158
    /**
159
     * @param Bludata\Doctrine\Common\Interfaces\BaseEntityInterface $entity
160
     *
161
     * @return Bludata\Doctrine\ORM\Repositories\QueryWorker
162
     */
163
    public function save(BaseEntityInterface $entity)
164
    {
165
        $this->em()->persist($entity);
166
167
        return $this;
168
    }
169
170
    /**
171
     * @param Bludata\Doctrine\Common\Interfaces\BaseEntityInterface $entity
172
     *
173
     * @return Bludata\Doctrine\ORM\Repositories\QueryWorker
174
     */
175
    public function flush(BaseEntityInterface $entity = null)
176
    {
177
        $this->em()->flush($entity);
178
179
        return $this;
180
    }
181
182
    public function em()
183
    {
184
        return parent::getEntityManager();
185
    }
186
}
187