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

FetchEntityByIdStep   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 11 2
A requiresBefore() 0 3 1
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
}