Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| ResourcePolicyProvider | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPolicyFor | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Policies; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\ContextConstants; |
| 6 | use Apie\Core\Lists\ItemHashmap; |
| 7 | |
| 8 | class ResourcePolicyProvider implements PolicyProviderInterface |
| 9 | { |
| 10 | public function __construct( |
| 11 | private readonly ItemHashmap $policyProviders, |
| 12 | private readonly FallbackPolicy $fallbackPolicy |
| 13 | ) { |
| 14 | } |
| 15 | |
| 16 | public function getPolicyFor(ApieContext $apieContext, string $action): object |
| 17 | { |
| 18 | $resourceName = $apieContext->getContext(ContextConstants::RESOURCE_NAME, false); |
| 19 | if ($resourceName === null) { |
| 20 | return $this->fallbackPolicy; |
| 21 | } |
| 22 | if (class_exists($resourceName)) { |
| 23 | $resourceName = (new \ReflectionClass($resourceName))->getShortName(); |
| 24 | } |
| 25 | |
| 26 | return $this->policyProviders[$resourceName] ?? $this->fallbackPolicy; |
| 27 | } |
| 28 | } |