WorkflowActionsResourceMetadataFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 24
rs 9.7666
c 0
b 0
f 0
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)
0 ignored issues
show
Bug introduced by
It seems like $attributes can also be of type null; however, parameter $attributes of ApiPlatform\Core\Metadat...adata::withAttributes() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
            ->withAttributes(/** @scrutinizer ignore-type */ $attributes)
Loading history...
60
            ->withItemOperations($operations)
0 ignored issues
show
Bug introduced by
It seems like $operations can also be of type null; however, parameter $itemOperations of ApiPlatform\Core\Metadat...a::withItemOperations() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
            ->withItemOperations(/** @scrutinizer ignore-type */ $operations)
Loading history...
61
        ;
62
    }
63
}
64