Completed
Push — master ( 936cb2...f0f25e )
by Iakov
12:25 queued 06:29
created

FetchEntityByIdStep::getRequiredArtifacts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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