1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
18
|
|
|
use ApiPlatform\Core\Operation\PathSegmentNameGeneratorInterface; |
19
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This will add an endpoint for component resources to find out usage totals. |
23
|
|
|
* |
24
|
|
|
* @author Daniel West <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class ComponentResourceMetadataFactory implements ResourceMetadataFactoryInterface |
27
|
|
|
{ |
28
|
|
|
private ResourceMetadataFactoryInterface $decorated; |
29
|
|
|
private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator; |
30
|
|
|
|
31
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator) |
32
|
|
|
{ |
33
|
|
|
$this->decorated = $decorated; |
34
|
|
|
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function create(string $resourceClass): ResourceMetadata |
38
|
|
|
{ |
39
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
40
|
|
|
$interfaces = class_implements($resourceClass); |
41
|
|
|
if (!\in_array(ComponentInterface::class, $interfaces, true)) { |
42
|
|
|
return $resourceMetadata; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$resourceShortName = $resourceMetadata->getShortName(); |
46
|
|
|
$pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName($resourceShortName); |
|
|
|
|
47
|
|
|
|
48
|
|
|
$usagePath = sprintf('/%s/{id}/usage', $pathSegmentName); |
49
|
|
|
|
50
|
|
|
$itemOperations = $resourceMetadata->getItemOperations() ?? []; |
51
|
|
|
$itemOperations['get_usage'] = [ |
52
|
|
|
'method' => 'GET', |
53
|
|
|
'stateless' => null, |
54
|
|
|
'path' => $usagePath, |
55
|
|
|
'serialize' => true, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
return $resourceMetadata->withItemOperations($itemOperations); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|