Passed
Pull Request — master (#1703)
by Tarmo
08:51
created

RestResourceConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Request/ParamConverter/RestResourceConverter.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Request\ParamConverter;
10
11
use App\Resource\ResourceCollection;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
13
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Throwable;
16
17
/**
18
 * Class RestResourceConverter
19
 *
20
 * Purpose of this param converter is to use exactly same methods and workflow
21
 * as in basic REST API requests.
22
 *
23
 * @package App\Request\ParamConverter
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class RestResourceConverter implements ParamConverterInterface
27
{
28 340
    public function __construct(
29
        private ResourceCollection $collection,
30
    ) {
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @throws Throwable
37
     */
38 67
    public function apply(Request $request, ParamConverter $configuration): bool
39
    {
40 67
        $name = $configuration->getName();
41 67
        $identifier = (string)$request->attributes->get($name, '');
42 67
        $resource = $this->collection->get((string)$configuration->getClass());
43
44 67
        if ($identifier !== '') {
45
            // Reminder make throw to exist on options
46 67
            $request->attributes->set($name, $resource->findOne($identifier, true));
47
        }
48
49 66
        return true;
50
    }
51
52 65
    public function supports(ParamConverter $configuration): bool
53
    {
54 65
        return $this->collection->has($configuration->getClass());
55
    }
56
}
57