-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAbstractStrategyHandler.php
executable file
·57 lines (45 loc) · 1.56 KB
/
AbstractStrategyHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Unleash\Client\Strategy;
use Override;
use Unleash\Client\Configuration\Context;
use Unleash\Client\DTO\Constraint;
use Unleash\Client\DTO\Strategy;
use Unleash\Client\Helper\ConstraintValidatorTrait;
abstract class AbstractStrategyHandler implements StrategyHandler
{
use ConstraintValidatorTrait;
public function supports(Strategy $strategy): bool
{
return $strategy->getName() === $this->getStrategyName();
}
protected function findParameter(string $parameter, Strategy $strategy): ?string
{
$parameters = $strategy->getParameters();
return $parameters[$parameter] ?? null;
}
protected function validateConstraints(Strategy $strategy, Context $context): bool
{
if (method_exists($strategy, 'hasNonexistentSegments') && $strategy->hasNonexistentSegments()) {
return false;
}
$validator = $this->getValidator();
$constraints = $this->getConstraintsForStrategy($strategy);
foreach ($constraints as $constraint) {
if (!$validator->validateConstraint($constraint, $context)) {
return false;
}
}
return true;
}
/**
* @return iterable<Constraint>
*/
private function getConstraintsForStrategy(Strategy $strategy): iterable
{
yield from $strategy->getConstraints();
$segments = method_exists($strategy, 'getSegments') ? $strategy->getSegments() : [];
foreach ($segments as $segment) {
yield from $segment->getConstraints();
}
}
}