Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.56% covered (success)
95.56%
43 / 45
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
BaseDropdownOptionProvider
95.56% covered (success)
95.56%
43 / 45
0.00% covered (danger)
0.00%
0 / 2
15
0.00% covered (danger)
0.00%
0 / 1
 supportsField
n/a
0 / 0
n/a
0 / 0
0
 createDropdownList
n/a
0 / 0
n/a
0 / 0
0
 supports
96.00% covered (success)
96.00%
24 / 25
0.00% covered (danger)
0.00%
0 / 1
10
 getList
95.00% covered (success)
95.00%
19 / 20
0.00% covered (danger)
0.00%
0 / 1
5
1<?php
2namespace Apie\CmsApiDropdownOption\DropdownOptionProvider;
3
4use Apie\CmsApiDropdownOption\Lists\DropdownOptionList;
5use Apie\Core\Context\ApieContext;
6use Apie\Core\ContextConstants;
7use Apie\Core\Metadata\Fields\ConstructorParameter;
8use Apie\Core\Metadata\Fields\FieldInterface;
9use Apie\Core\PropertyToFieldMetadataUtil;
10use Apie\Core\ValueObjects\Utils;
11use ReflectionClass;
12use ReflectionMethod;
13
14abstract class BaseDropdownOptionProvider implements DropdownOptionProviderInterface
15{
16    abstract protected function supportsField(FieldInterface $fieldMetadata, ApieContext $apieContext): bool;
17
18    abstract protected function createDropdownList(
19        string $property,
20        FieldInterface $fieldMetadata,
21        string $searchTerm,
22        ApieContext $apieContext
23    ): DropdownOptionList;
24
25    final public function supports(ApieContext $apieContext): bool
26    {
27        if (!$apieContext->hasContext('property')) {
28            return false;
29        }
30        $property = Utils::toString($apieContext->getContext('property'));
31        if ($apieContext->hasContext(ContextConstants::RESOURCE_NAME)) {
32            $resourceName = $apieContext->getContext(ContextConstants::RESOURCE_NAME);
33            if (!class_exists($resourceName) || $property === 'id') {
34                return false;
35            }
36            $refl = new ReflectionClass($resourceName);
37            $fieldMetadata = PropertyToFieldMetadataUtil::fromPropertyStringToFieldMetadata(
38                $refl,
39                $apieContext,
40                $property
41            );
42            if ($fieldMetadata instanceof FieldInterface) {
43                return $this->supportsField($fieldMetadata, $apieContext);
44            }
45        }
46        if ($apieContext->hasContext(ContextConstants::SERVICE_CLASS) && $apieContext->hasContext(ContextConstants::METHOD_NAME)) {
47            $refl = new ReflectionMethod(
48                $apieContext->getContext(ContextConstants::SERVICE_CLASS),
49                $apieContext->getContext(ContextConstants::METHOD_NAME)
50            );
51            foreach ($refl->getParameters() as $parameter) {
52                if ($parameter->getName() === $property) {
53                    $fieldMetadata = new ConstructorParameter($parameter);
54                    return $this->supportsField($fieldMetadata, $apieContext);
55                }
56            }
57        }
58        return false;
59    }
60
61    final public function getList(ApieContext $apieContext, string $searchTerm): DropdownOptionList
62    {
63        $property = Utils::toString($apieContext->getContext('property'));
64        if ($apieContext->hasContext(ContextConstants::RESOURCE_NAME)) {
65            $resourceName = $apieContext->getContext(ContextConstants::RESOURCE_NAME);
66            $refl = new ReflectionClass($resourceName);
67            $fieldMetadata = PropertyToFieldMetadataUtil::fromPropertyStringToFieldMetadata(
68                $refl,
69                $apieContext,
70                $property
71            );
72        } else {
73            $refl = new ReflectionMethod(
74                $apieContext->getContext(ContextConstants::SERVICE_CLASS),
75                $apieContext->getContext(ContextConstants::METHOD_NAME)
76            );
77            foreach ($refl->getParameters() as $parameter) {
78                if ($parameter->getName() === $property) {
79                    $fieldMetadata = new ConstructorParameter($parameter);
80                    break;
81                }
82            }
83            if (!isset($fieldMetadata)) {
84                return new DropdownOptionList();
85            }
86        }
87
88        return $this->createDropdownList($property, $fieldMetadata, $searchTerm, $apieContext);
89    }
90}