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

GetElement::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 14
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 22
loc 22
ccs 14
cts 14
cp 1
rs 9.2
cc 3
eloc 14
nc 4
nop 0
crap 3
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