|
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
|
|
|
|