FormStateProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
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\DataProvider\StateProvider;
15
16
use ApiPlatform\Doctrine\Orm\State\CollectionProvider;
17
use ApiPlatform\Doctrine\Orm\State\ItemProvider;
18
use ApiPlatform\Metadata\CollectionOperationInterface;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\State\ProviderInterface;
21
use Ramsey\Uuid\Uuid;
22
use Silverback\ApiComponentsBundle\Entity\Component\Form;
23
use Silverback\ApiComponentsBundle\Form\Type\User\PasswordUpdateType;
24
25
/**
26
 * @author Daniel West <[email protected]>
27
 */
28
class FormStateProvider implements ProviderInterface
29
{
30
    public function __construct(private readonly ProviderInterface $defaultProvider)
31
    {
32
    }
33
34
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
35
    {
36
        if ($operation instanceof CollectionOperationInterface) {
37
            return $this->defaultProvider->provide($operation->withProvider(CollectionProvider::class), $uriVariables, $context);
38
        }
39
40
        $id = $uriVariables['id'];
41
        if ('password_reset' === $id) {
42
            $dummyFormComponent = new Form();
43
            $dummyFormComponent->formType = PasswordUpdateType::class;
44
            $refObject = new \ReflectionObject($dummyFormComponent);
45
            $refProperty = $refObject->getProperty('id');
46
            $refProperty->setValue($dummyFormComponent, Uuid::uuid4());
47
48
            return $dummyFormComponent;
49
        }
50
51
        return $this->defaultProvider->provide($operation->withProvider(ItemProvider::class), $uriVariables, $context);
52
    }
53
}
54