Completed
Push — master ( 047943...ada7af )
by Lee
05:25
created

GetElement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 3
c 3
b 0
f 0
lcom 1
cbo 5
dl 26
loc 26
ccs 14
cts 14
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A execute() 22 22 3

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
 * This file is part of the Drest package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author Lee Davis
9
 * @copyright Copyright (c) Lee Davis <@leedavis81>
10
 * @link https://github.com/leedavis81/drest/blob/master/LICENSE
11
 * @license http://opensource.org/licenses/MIT The MIT X License (MIT)
12
 */
13
namespace Drest\Service\Action;
14
15
use Doctrine\ORM;
16
use DrestCommon\Response\Response;
17
18 View Code Duplication
class GetElement extends AbstractAction
19
{
20
21 3
    public function execute()
22
    {
23 3
        $classMetaData = $this->getMatchedRoute()->getClassMetaData();
24 3
        $elementName = $classMetaData->getEntityAlias();
25
26 3
        $em = $this->getEntityManager();
27 3
        $qb = $em->createQueryBuilder()->from($classMetaData->getClassName(), $elementName);
28 3
        $this->registerExposeFromMetaData($qb, $em->getClassMetadata($classMetaData->getClassName()));
29
30 3
        foreach ($this->getMatchedRoute()->getRouteParams() as $key => $value) {
31 2
            $qb->andWhere($elementName . '.' . $key . ' = :' . $key);
32 2
            $qb->setParameter($key, $value);
33 3
        }
34
35
        try {
36 3
            $resultSet = $this->createResultSet($qb->getQuery()->getSingleResult(ORM\Query::HYDRATE_ARRAY));
37 3
        } catch (\Exception $e) {
38 2
            return $this->handleError($e, Response::STATUS_CODE_404);
39
        }
40
41 1
        return $resultSet;
42
    }
43
}
44