Completed
Push — master ( ada7af...87ae84 )
by Lee
07:59
created

GetElement::execute()   B

Complexity

Conditions 4
Paths 18

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.1106

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 30
ccs 17
cts 21
cp 0.8095
rs 8.5806
cc 4
eloc 19
nc 18
nop 0
crap 4.1106
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
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
            $resultArray = $qb->getQuery()->getSingleResult(ORM\Query::HYDRATE_ARRAY);
37 1
            if ($this->getMatchedRoute()->hasHandleCall())
38 1
            {
39
                $className = $this->getMatchedRoute()->getClassMetaData()->getClassName();
40
                $handleMethod = $this->getMatchedRoute()->getHandleCall();
41
                $resultArray = $className::$handleMethod($this->getRepresentation()->toArray(false), $this->getRequest());
42
            }
43
44 1
            $resultSet = $this->createResultSet($resultArray);
45 3
        } catch (\Exception $e) {
46 2
            return $this->handleError($e, Response::STATUS_CODE_404);
47
        }
48
49 1
        return $resultSet;
50
    }
51
}
52