Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.70% |
85 / 108 |
|
76.92% |
10 / 13 |
CRAP | |
0.00% |
0 / 1 |
| ApieContext | |
78.70% |
85 / 108 |
|
76.92% |
10 / 13 |
101.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| withContext | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| hasContext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getContext | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| registerOrMarkAmbiguous | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| registerInstance | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| getApplicableGetters | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
9 | |||
| getApplicableSetters | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
90 | |||
| getApplicableMethods | |
68.75% |
11 / 16 |
|
0.00% |
0 / 1 |
8.50 | |||
| appliesToContext | |
70.37% |
19 / 27 |
|
0.00% |
0 / 1 |
22.66 | |||
| checkAuthorization | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| isAuthorized | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| dispatchEvent | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Context; |
| 3 | |
| 4 | use Apie\Core\Attributes\ApieContextAttribute; |
| 5 | use Apie\Core\Attributes\Internal; |
| 6 | use Apie\Core\Attributes\RuntimeCheck; |
| 7 | use Apie\Core\Attributes\StaticCheck; |
| 8 | use Apie\Core\ContextConstants; |
| 9 | use Apie\Core\Entities\EntityWithStatesInterface; |
| 10 | use Apie\Core\Exceptions\ActionNotAllowedException; |
| 11 | use Apie\Core\Exceptions\IndexNotFoundException; |
| 12 | use Apie\Core\Metadata\Concerns\UseContextKey; |
| 13 | use Apie\Core\Utils\EntityUtils; |
| 14 | use LogicException; |
| 15 | use Psr\EventDispatcher\EventDispatcherInterface; |
| 16 | use ReflectionClass; |
| 17 | use ReflectionEnumUnitCase; |
| 18 | use ReflectionMethod; |
| 19 | use ReflectionProperty; |
| 20 | use ReflectionType; |
| 21 | use Throwable; |
| 22 | |
| 23 | /** |
| 24 | * ApieContext is used as builder/mediator and passed though many Apie functions. It can be used to filter (for example |
| 25 | * only show property when authenticated) or can be used to provide extra functionality to other methods. |
| 26 | */ |
| 27 | final class ApieContext |
| 28 | { |
| 29 | use UseContextKey; |
| 30 | |
| 31 | /** @var array<int, class-string<ApieContextAttribute>> */ |
| 32 | private const ATTRIBUTES = [ |
| 33 | StaticCheck::class |
| 34 | ]; |
| 35 | /** @var array<string, \Closure> */ |
| 36 | private array $predefined; |
| 37 | |
| 38 | /** |
| 39 | * @param array<string, mixed> $context |
| 40 | */ |
| 41 | public function __construct(private array $context = []) |
| 42 | { |
| 43 | $this->predefined = [ |
| 44 | ApieContext::class => function (ApieContext $context) { |
| 45 | return $context; |
| 46 | } |
| 47 | ]; |
| 48 | } |
| 49 | |
| 50 | public function withContext(string $key, mixed $value): self |
| 51 | { |
| 52 | $instance = clone $this; |
| 53 | $instance->context[$key] = $value; |
| 54 | return $instance; |
| 55 | } |
| 56 | |
| 57 | public function hasContext(string $key): bool |
| 58 | { |
| 59 | return array_key_exists($key, $this->context) || isset($this->predefined[$key]); |
| 60 | } |
| 61 | |
| 62 | public function getContext(string $key, bool $throwError = true): mixed |
| 63 | { |
| 64 | if (isset($this->predefined[$key])) { |
| 65 | return $this->predefined[$key]($this); |
| 66 | } |
| 67 | if (!array_key_exists($key, $this->context)) { |
| 68 | if (!$throwError) { |
| 69 | return null; |
| 70 | } |
| 71 | throw new IndexNotFoundException($key); |
| 72 | } |
| 73 | |
| 74 | return $this->context[$key]; |
| 75 | } |
| 76 | |
| 77 | private function registerOrMarkAmbiguous(string $offset, object $instance): void |
| 78 | { |
| 79 | if (!isset($this->context[$offset])) { |
| 80 | $this->context[$offset] = $instance; |
| 81 | return; |
| 82 | } |
| 83 | if ($this->context[$offset] instanceof AmbiguousCall) { |
| 84 | $this->context[$offset] = $this->context[$offset]->withAddedName(get_class($instance)); |
| 85 | } else { |
| 86 | $this->context[$offset] = new AmbiguousCall($offset, get_class($this->context[$offset]), get_class($instance)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public function registerInstance(object $object): self |
| 91 | { |
| 92 | $refl = new ReflectionClass($object); |
| 93 | |
| 94 | $instance = $this->withContext($refl->name, $object); |
| 95 | foreach ($refl->getInterfaceNames() as $interface) { |
| 96 | $instance->registerOrMarkAmbiguous($interface, $object); |
| 97 | } |
| 98 | $refl = $refl->getParentClass(); |
| 99 | while ($refl) { |
| 100 | $instance->registerOrMarkAmbiguous($refl->name, $object); |
| 101 | $refl = $refl->getParentClass(); |
| 102 | } |
| 103 | |
| 104 | return $instance; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param ReflectionClass<object> $class |
| 109 | */ |
| 110 | public function getApplicableGetters(ReflectionClass $class, bool $runtimeChecks = true): ReflectionHashmap |
| 111 | { |
| 112 | $list = []; |
| 113 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
| 114 | if ($this->appliesToContext($property, $runtimeChecks)) { |
| 115 | $list[$property->getName()] = $property; |
| 116 | } |
| 117 | } |
| 118 | foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
| 119 | if (preg_match('/^(get|has|is).+$/i', $method->name) && $this->appliesToContext($method, $runtimeChecks) && !$method->isStatic() && !$method->isAbstract()) { |
| 120 | if (strpos($method->name, 'is') === 0) { |
| 121 | $list[lcfirst(substr($method->name, 2))] = $method; |
| 122 | } else { |
| 123 | $list[lcfirst(substr($method->name, 3))] = $method; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return new ReflectionHashmap($list); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param ReflectionClass<object> $class |
| 132 | */ |
| 133 | public function getApplicableSetters(ReflectionClass $class, bool $runtimeChecks = true): ReflectionHashmap |
| 134 | { |
| 135 | $list = []; |
| 136 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
| 137 | if ($property->isReadOnly()) { |
| 138 | continue; |
| 139 | } |
| 140 | if ($this->appliesToContext($property, $runtimeChecks)) { |
| 141 | $list[$property->getName()] = $property; |
| 142 | } |
| 143 | } |
| 144 | foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
| 145 | if (preg_match('/^(set).+$/i', $method->name) && $this->appliesToContext($method, $runtimeChecks) && !$method->isStatic() && !$method->isAbstract()) { |
| 146 | $list[lcfirst(substr($method->name, 3))] = $method; |
| 147 | } |
| 148 | } |
| 149 | return new ReflectionHashmap($list); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param ReflectionClass<object> $class |
| 154 | */ |
| 155 | public function getApplicableMethods(ReflectionClass $class, bool $runtimeChecks = true): ReflectionHashmap |
| 156 | { |
| 157 | $list = []; |
| 158 | $filter = function (ReflectionMethod $method) { |
| 159 | return !preg_match('/^(__|create|set|get|has|is).+$/i', $method->name); |
| 160 | }; |
| 161 | if ($runtimeChecks && $this->hasContext(ContextConstants::RESOURCE)) { |
| 162 | $resource = $this->getContext(ContextConstants::RESOURCE); |
| 163 | if ($resource instanceof EntityWithStatesInterface) { |
| 164 | $allowedMethods = $resource->provideAllowedMethods()->toArray(); |
| 165 | $allowedMethodsMap = array_combine($allowedMethods, $allowedMethods); |
| 166 | $filter = function (ReflectionMethod $method) use (&$allowedMethodsMap) { |
| 167 | return isset($allowedMethodsMap[$method->name]); |
| 168 | }; |
| 169 | } |
| 170 | } |
| 171 | foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
| 172 | if ($this->appliesToContext($method, $runtimeChecks) && $filter($method)) { |
| 173 | $list[$method->name] = $method; |
| 174 | } |
| 175 | } |
| 176 | return new ReflectionHashmap($list); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * @param ReflectionClass<object>|ReflectionMethod|ReflectionProperty|ReflectionType|ReflectionEnumUnitCase $method |
| 181 | */ |
| 182 | public function appliesToContext(ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionType|ReflectionEnumUnitCase $method, bool $runtimeChecks = true, ?Throwable $errorToThrow = null): bool |
| 183 | { |
| 184 | if ($method->getAttributes(Internal::class)) { |
| 185 | return false; |
| 186 | } |
| 187 | $attributesToCheck = $runtimeChecks ? [RuntimeCheck::class, ...self::ATTRIBUTES] : self::ATTRIBUTES; |
| 188 | foreach ($attributesToCheck as $attribute) { |
| 189 | foreach ($method->getAttributes($attribute) as $reflAttribute) { |
| 190 | if (!$reflAttribute->newInstance()->applies($this)) { |
| 191 | if ($errorToThrow) { |
| 192 | throw $errorToThrow; |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | if ($method instanceof ReflectionMethod && $runtimeChecks) { |
| 199 | foreach (EntityUtils::getContextParameters($method) as $parameter) { |
| 200 | if ($parameter->isDefaultValueAvailable()) { |
| 201 | continue; |
| 202 | } |
| 203 | $key = $this->getContextKey($this, $parameter); |
| 204 | if ($key === null) { |
| 205 | if ($errorToThrow) { |
| 206 | throw new LogicException( |
| 207 | 'Parameter ' . $parameter->name . ' has an invalid context key', |
| 208 | 0, |
| 209 | $errorToThrow |
| 210 | ); |
| 211 | } |
| 212 | return false; |
| 213 | } |
| 214 | if (!isset($this->context[$key]) && !isset($this->predefined[$key])) { |
| 215 | if ($errorToThrow) { |
| 216 | throw new IndexNotFoundException($key); |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | public function checkAuthorization(): void |
| 226 | { |
| 227 | try { |
| 228 | if (!$this->isAuthorized(runtimeChecks: true, throwError: true)) { |
| 229 | throw new ActionNotAllowedException(); |
| 230 | } |
| 231 | } catch (ActionNotAllowedException) { |
| 232 | throw new ActionNotAllowedException(); |
| 233 | } catch (Throwable $error) { |
| 234 | throw new ActionNotAllowedException($error); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | public function isAuthorized(bool $runtimeChecks, bool $throwError = false): bool |
| 239 | { |
| 240 | $actionClass = $this->getContext(ContextConstants::APIE_ACTION, $throwError); |
| 241 | if (!$actionClass) { |
| 242 | return true; |
| 243 | } |
| 244 | return $actionClass::isAuthorized($this, $runtimeChecks, $throwError); |
| 245 | } |
| 246 | |
| 247 | public function dispatchEvent(object $event): object |
| 248 | { |
| 249 | $dispatcher = $this->context[EventDispatcherInterface::class] ?? null; |
| 250 | if ($dispatcher instanceof EventDispatcherInterface) { |
| 251 | return $dispatcher->dispatch($event); |
| 252 | } |
| 253 | return $event; |
| 254 | } |
| 255 | } |