Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
9 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
VerifyOtpInputComponentProvider
33.33% covered (danger)
33.33%
9 / 27
0.00% covered (danger)
0.00%
0 / 2
16.67
0.00% covered (danger)
0.00%
0 / 1
 supports
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
 createComponentFor
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\HtmlBuilders\Factories\Concrete;
3
4use Apie\Core\Attributes\CmsSingleInput;
5use Apie\Core\ContextConstants;
6use Apie\Core\Dto\CmsInputOption;
7use Apie\Core\Utils\ConverterUtils;
8use Apie\HtmlBuilders\Components\Forms\SingleInput;
9use Apie\HtmlBuilders\FormBuildContext;
10use Apie\HtmlBuilders\Interfaces\ComponentInterface;
11use Apie\HtmlBuilders\Interfaces\FormComponentProviderInterface;
12use Apie\OtpValueObjects\VerifyOTP;
13use ReflectionProperty;
14use ReflectionType;
15
16class VerifyOtpInputComponentProvider implements FormComponentProviderInterface
17{
18    public function supports(ReflectionType $type, FormBuildContext $context): bool
19    {
20        if (!$context->getApieContext()->hasContext(ContextConstants::RESOURCE)) {
21            return false;
22        }
23        $class = ConverterUtils::toReflectionClass($type);
24        if (!$class) {
25            return false;
26        }
27        do {
28            if ($class->name === VerifyOTP::class) {
29                return true;
30            }
31            $class = $class->getParentClass();
32        } while ($class);
33        return false;
34    }
35
36    public function createComponentFor(ReflectionType $type, FormBuildContext $context): ComponentInterface
37    {
38        $value = $context->getFilledInValue($type->allowsNull() ? null : false, true);
39        $class = ConverterUtils::toReflectionClass($type);
40        /** @var ReflectionProperty $property */
41        $property = $class->getMethod('getOtpReference')->invoke(null);
42        $resource = $context->getApieContext()->getContext(ContextConstants::RESOURCE);
43        /** @var string $label */
44        $label = $class->getMethod('getOtpLabel')->invoke(null, $resource);
45        $otpSecret = $property->getValue($resource);
46
47        return new SingleInput(
48            $context->getFormName(),
49            $value,
50            $context->createTranslationLabel(),
51            $type->allowsNull(),
52            $type,
53            new CmsSingleInput(
54                ['otp', 'text'],
55                new CmsInputOption(imageUrl: $otpSecret->getUrl($label))
56            )
57        );
58    }
59}