Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
57.14% |
24 / 42 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ActionFinder | |
57.14% |
24 / 42 |
|
60.00% |
3 / 5 |
32.71 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| find | |
50.00% |
17 / 34 |
|
0.00% |
0 / 1 |
16.00 | |||
| getConstructorParameters | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getServiceId | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| isAutoWiredController | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA; * |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\routing\Service; |
| 22 | |
| 23 | use oat\tao\model\routing\Contract\ActionFinderInterface; |
| 24 | use oat\tao\model\routing\Contract\ActionInterface; |
| 25 | use Psr\Container\ContainerInterface; |
| 26 | use Psr\Log\LoggerInterface; |
| 27 | use ReflectionClass; |
| 28 | use ReflectionNamedType; |
| 29 | |
| 30 | class ActionFinder implements ActionFinderInterface |
| 31 | { |
| 32 | /** @var ContainerInterface */ |
| 33 | private $container; |
| 34 | |
| 35 | /** @var LoggerInterface */ |
| 36 | private $logger; |
| 37 | |
| 38 | public function __construct(ContainerInterface $container, LoggerInterface $logger) |
| 39 | { |
| 40 | $this->container = $container; |
| 41 | $this->logger = $logger; |
| 42 | } |
| 43 | |
| 44 | public function find(string $className): ?object |
| 45 | { |
| 46 | $serviceId = $this->getServiceId($className); |
| 47 | |
| 48 | if ($this->container->has($serviceId)) { |
| 49 | return $this->container->get($serviceId); |
| 50 | } |
| 51 | |
| 52 | if (!$this->isAutoWiredController($className)) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | $reflectionClass = new ReflectionClass($className); |
| 57 | $constructorParameters = $this->getConstructorParameters($reflectionClass); |
| 58 | |
| 59 | if (empty($constructorParameters)) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | $params = []; |
| 64 | |
| 65 | foreach ($constructorParameters as $parameter) { |
| 66 | $paramClassType = $parameter->getType(); |
| 67 | $paramClass = $paramClassType instanceof ReflectionNamedType ? $paramClassType->getName() : null; |
| 68 | |
| 69 | if ($paramClass === null) { |
| 70 | $this->logger->debug( |
| 71 | sprintf( |
| 72 | 'Non-object parameters are not supported for action "%s" constructor: %s', |
| 73 | $className, |
| 74 | $parameter->getName() |
| 75 | ) |
| 76 | ); |
| 77 | |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | $paramServiceId = $this->getServiceId($paramClass); |
| 82 | |
| 83 | if (!$this->container->has($paramServiceId)) { |
| 84 | $this->logger->warning( |
| 85 | sprintf( |
| 86 | 'Service "%s" does not exist for action "%s"', |
| 87 | $paramServiceId, |
| 88 | $className |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | return null; |
| 93 | } |
| 94 | |
| 95 | $params[] = $this->container->get($paramServiceId); |
| 96 | } |
| 97 | |
| 98 | return $reflectionClass->newInstanceArgs($params); |
| 99 | } |
| 100 | |
| 101 | private function getConstructorParameters(ReflectionClass $reflectionClass): array |
| 102 | { |
| 103 | $constructor = $reflectionClass->getConstructor(); |
| 104 | |
| 105 | return $constructor ? $constructor->getParameters() : []; |
| 106 | } |
| 107 | |
| 108 | private function getServiceId(string $className): string |
| 109 | { |
| 110 | return defined("$className::SERVICE_ID") |
| 111 | ? $className::SERVICE_ID |
| 112 | : $className; |
| 113 | } |
| 114 | |
| 115 | private function isAutoWiredController(string $className): bool |
| 116 | { |
| 117 | return class_exists($className) && is_subclass_of($className, ActionInterface::class); |
| 118 | } |
| 119 | } |