Completed
Pull Request — master (#9)
by Iakov
06:26
created

FetchEntityByIdStep::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace Kami\ApiCoreBundle\RequestProcessor\Step\Common;
5
6
use Doctrine\Bundle\DoctrineBundle\Registry;
7
use Kami\ApiCoreBundle\RequestProcessor\Step\AbstractStep;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
11
class FetchEntityByIdStep extends AbstractStep
12
{
13
    /**
14
     * @var Registry
15
     */
16
    protected $doctrine;
17
18
    public function __construct(Registry $doctrine)
19
    {
20
        $this->doctrine = $doctrine;
21
    }
22
23
    public function execute()
24
    {
25
        /** @var \ReflectionClass $reflection */
26
        $reflection = $this->getFromResponse('reflection');
27
        $entity = $this->doctrine->getRepository($reflection->getName())->find($this->request->get('id', 0));
28
29
        if (!$entity) {
30
            throw new NotFoundHttpException('Resource not found');
31
        }
32
33
        return $this->createResponse(['entity' => $entity]);
34
    }
35
36
    public function requiresBefore()
37
    {
38
        return [GetReflectionFromRequestStep::class];
39
    }
40
41
}