diff --git a/src/Read/ChainSegmentFactory.php b/src/Read/ChainSegmentFactory.php index e740f18..6246222 100644 --- a/src/Read/ChainSegmentFactory.php +++ b/src/Read/ChainSegmentFactory.php @@ -6,7 +6,7 @@ use Pheature\Core\Toggle\Exception\InvalidSegmentTypeGiven; -final class ChainSegmentFactory +final class ChainSegmentFactory implements SegmentFactory { /** @var SegmentFactory[] */ private array $segmentFactories; @@ -19,17 +19,24 @@ public function __construct(SegmentFactory ...$segmentFactories) /** * @param string $segmentId * @param string $segmentType - * @param array $payload + * @param array $criteria * @return Segment */ - public function create(string $segmentId, string $segmentType, array $payload): Segment + public function create(string $segmentId, string $segmentType, array $criteria): Segment { foreach ($this->segmentFactories as $segmentFactory) { if (in_array($segmentType, $segmentFactory->types(), true)) { - return $segmentFactory->create($segmentId, $segmentType, $payload); + return $segmentFactory->create($segmentId, $segmentType, $criteria); } } throw InvalidSegmentTypeGiven::withType($segmentType); } + + public function types(): array + { + return array_merge( + ...array_map(static fn(SegmentFactory $segmentFactory) => $segmentFactory->types(), $this->segmentFactories) + ); + } } diff --git a/src/Read/ChainToggleStrategyFactory.php b/src/Read/ChainToggleStrategyFactory.php index 402c986..e7c9aa7 100644 --- a/src/Read/ChainToggleStrategyFactory.php +++ b/src/Read/ChainToggleStrategyFactory.php @@ -19,7 +19,7 @@ public function __construct(SegmentFactory $segmentFactory, ToggleStrategyFactor } /** - * @param array>> $strategy + * @param array $strategy * @return ToggleStrategy */ public function createFromArray(array $strategy): ToggleStrategy @@ -61,10 +61,10 @@ private function makeStrategy( $segmentId = $segment['segment_id']; /** @var string $segmentType */ $segmentType = $segment['segment_type']; - /** @var array $payload */ - $payload = $segment['payload']; + /** @var array $criteria */ + $criteria = $segment['criteria']; - return $this->segmentFactory->create($segmentId, $segmentType, $payload); + return $this->segmentFactory->create($segmentId, $segmentType, $criteria); }, $segments)) ); } diff --git a/src/Read/SegmentFactory.php b/src/Read/SegmentFactory.php index 8a54247..97ab701 100644 --- a/src/Read/SegmentFactory.php +++ b/src/Read/SegmentFactory.php @@ -9,8 +9,8 @@ interface SegmentFactory extends WithProcessableFixedTypes /** * @param string $segmentId * @param string $segmentType - * @param array $payload + * @param array $criteria * @return Segment */ - public function create(string $segmentId, string $segmentType, array $payload): Segment; + public function create(string $segmentId, string $segmentType, array $criteria): Segment; } diff --git a/test/Read/ChainToggleStrategyFactoryTest.php b/test/Read/ChainToggleStrategyFactoryTest.php index bf0e11d..b10ffa8 100644 --- a/test/Read/ChainToggleStrategyFactoryTest.php +++ b/test/Read/ChainToggleStrategyFactoryTest.php @@ -20,7 +20,7 @@ final class ChainToggleStrategyFactoryTest extends TestCase [ 'segment_id' => 'some_segment_id', 'segment_type' => 'some_segment_type', - 'payload' => [ + 'criteria' => [ 'some' => 'data' ], ], @@ -71,7 +71,7 @@ public function testItShouldBeCreatedWithAtLeastOneToggleStrategyFactoryInstance $segmentFactory = $this->createMock(SegmentFactory::class); $segmentFactory->expects(self::once()) ->method('create') - ->with(self::SEGMENTS[0]['segment_id'], self::SEGMENTS[0]['segment_type'],self::SEGMENTS[0]['payload']) + ->with(self::SEGMENTS[0]['segment_id'], self::SEGMENTS[0]['segment_type'],self::SEGMENTS[0]['criteria']) ->willReturn($this->createMock(Segment::class)); $toggleStrategyFactory = $this->createMock(ToggleStrategyFactory::class); $toggleStrategyFactory->expects(self::once())