Completed
Pull Request — master (#33)
by Iakov
06:48
created

FetchEntityByIdStep   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 30
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

3 Methods

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