|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* (c) 2019, Wesley O. Nichols |
|
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
|
|
|
namespace Wesnick\WorkflowBundle\Metadata; |
|
13
|
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
15
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
|
16
|
|
|
use Wesnick\WorkflowBundle\Controller\DefaultTransitionController; |
|
17
|
|
|
use Wesnick\WorkflowBundle\Model\PotentialActionInterface; |
|
18
|
|
|
use Wesnick\WorkflowBundle\Model\WorkflowDTO; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Ensure psuedo-property potentialActions appears on supported resources and add transition operations to the resource. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Wesley O. Nichols <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class WorkflowActionsResourceMetadataFactory implements ResourceMetadataFactoryInterface |
|
26
|
|
|
{ |
|
27
|
|
|
private $decorated; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->decorated = $decorated; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function create(string $resourceClass): ResourceMetadata |
|
38
|
|
|
{ |
|
39
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
|
40
|
|
|
if (!is_a($resourceClass, PotentialActionInterface::class, true)) { |
|
41
|
|
|
return $resourceMetadata; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Set the pseudo-group for potentialAction to appear in name collection metadata factories |
|
45
|
|
|
$attributes = $resourceMetadata->getAttributes(); |
|
46
|
|
|
$groups = $attributes['denormalization_context']['groups'] ?? []; |
|
47
|
|
|
if (!in_array('workflowAction:output', $groups, true)) { |
|
48
|
|
|
$attributes['denormalization_context']['groups'][] = 'workflowAction:output'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$operations = $resourceMetadata->getItemOperations(); |
|
52
|
|
|
$operations['patch'] = [ |
|
53
|
|
|
'method' => 'PATCH', |
|
54
|
|
|
'controller' => DefaultTransitionController::class, |
|
55
|
|
|
'input' => ['class' => WorkflowDTO::class, 'name' => 'WorkflowDTO'], |
|
56
|
|
|
]; |
|
57
|
|
|
|
|
58
|
|
|
return $resourceMetadata |
|
59
|
|
|
->withAttributes($attributes) |
|
|
|
|
|
|
60
|
|
|
->withItemOperations($operations) |
|
|
|
|
|
|
61
|
|
|
; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|