Skip to content

Commit

Permalink
fix: revert idea to unify feature types
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Jul 26, 2023
1 parent 401d1f6 commit e0f8cb7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/DTO/DefaultFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
final class DefaultFeature implements Feature
{
/**
* @param iterable<Strategy> $strategies
* @param array<InternalVariant> $variants
* @param iterable<Strategy> $strategies
* @param array<Variant> $variants
*/
public function __construct(
private readonly string $name,
Expand Down Expand Up @@ -36,7 +36,7 @@ public function getStrategies(): iterable
}

/**
* @return array<InternalVariant>
* @return array<Variant>
*/
public function getVariants(): array
{
Expand Down
25 changes: 7 additions & 18 deletions src/DTO/DefaultProxyFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Unleash\Client\DTO;

final class DefaultProxyFeature implements Feature
final class DefaultProxyFeature implements ProxyFeature
{
public string $name;

public bool $enabled;

public ResolvedVariant $variant;

public bool $impressionData;
public bool $impression_data;

/**
* @param array{
Expand All @@ -24,7 +24,7 @@ final class DefaultProxyFeature implements Feature
* value: string
* }
* },
* impressionData: bool
* impression_data: bool
* } $response
*/
public function __construct(array $response)
Expand All @@ -40,7 +40,7 @@ public function __construct(array $response)

$this->name = $response['name'];
$this->enabled = $response['enabled'];
$this->impressionData = $response['impression_data'];
$this->impression_data = $response['impression_data'];

$payload = null;

Expand All @@ -61,24 +61,13 @@ public function isEnabled(): bool
return $this->enabled;
}

/**
* @return array<Variant>
*/
public function getVariants(): array
public function getVariant(): ResolvedVariant
{
return [$this->variant];
return $this->variant;
}

public function hasImpressionData(): bool
{
return $this->impressionData;
}

/**
* @return iterable<Strategy>
*/
public function getStrategies(): iterable
{
return [];
return $this->impression_data;
}
}
14 changes: 14 additions & 0 deletions src/DTO/ProxyFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Unleash\Client\DTO;

interface ProxyFeature
{
public function getName(): string;

public function isEnabled(): bool;

public function getVariant(): ResolvedVariant;

public function hasImpressionData(): bool;
}
5 changes: 3 additions & 2 deletions src/DefaultProxyUnleash.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Unleash\Client\DTO\DefaultResolvedVariant;
use Unleash\Client\DTO\DefaultVariant;
use Unleash\Client\DTO\ResolvedVariant;
use Unleash\Client\Enum\Stickiness;
use Unleash\Client\Metrics\MetricsHandler;
use Unleash\Client\Repository\ProxyRepository;

Expand Down Expand Up @@ -39,9 +40,9 @@ public function getVariant(string $featureName, ?Context $context = null, ?Resol
$response = $this->repository->findFeatureByContext($featureName, $context);

if ($response !== null) {
$variant = $response->getVariants()[0];
$variant = $response->getVariant();
}
$metricVariant = new DefaultVariant($variant->getName(), $variant->isEnabled(), 0, null, $variant->getPayload());
$metricVariant = new DefaultVariant($variant->getName(), $variant->isEnabled(), 0, Stickiness::DEFAULT, $variant->getPayload());
$this->metricsHandler->handleMetrics(new DefaultFeature($featureName, $variant->isEnabled(), []), $variant->isEnabled(), $metricVariant);

return $variant;
Expand Down
11 changes: 9 additions & 2 deletions src/Repository/DefaultUnleashProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\SimpleCache\InvalidArgumentException;
use Symfony\Component\VarExporter\Exception\LogicException;
use Unleash\Client\Configuration\Context;
use Unleash\Client\Configuration\UnleashConfiguration;
use Unleash\Client\Configuration\UnleashContext;
use Unleash\Client\DTO\DefaultProxyFeature;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\ProxyFeature;

final class DefaultUnleashProxyRepository implements UnleashRepository, ProxyRepository
{
Expand Down Expand Up @@ -40,7 +42,7 @@ public function getFeatures(): iterable
return $this->baseRepo->getFeatures();
}

public function findFeatureByContext(string $featureName, ?Context $context = null): ?Feature
public function findFeatureByContext(string $featureName, ?Context $context = null): ?ProxyFeature
{
$cacheKey = $this->getCacheKey($featureName, $context);

Expand All @@ -66,7 +68,12 @@ public function findFeatureByContext(string $featureName, ?Context $context = nu
$request = $request->withHeader($name, $value);
}

$request = $request->withHeader('Authorization', $this->configuration->getProxyKey());
$apiKey = $this->configuration->getProxyKey();
if($apiKey === null) {
throw new LogicException('No api proxy key was specified');
}

$request = $request->withHeader('Authorization', $apiKey);

$response = $this->httpClient->sendRequest($request);
$body = json_decode($response->getBody(), true);
Expand Down
4 changes: 2 additions & 2 deletions src/Repository/ProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Unleash\Client\Repository;

use Unleash\Client\Configuration\Context;
use Unleash\Client\DTO\Feature;
use Unleash\Client\DTO\ProxyFeature;

interface ProxyRepository
{
public function findFeatureByContext(string $featureName, ?Context $context = null): ?Feature;
public function findFeatureByContext(string $featureName, ?Context $context = null): ?ProxyFeature;
}
6 changes: 3 additions & 3 deletions tests/DTO/DefaultProxyFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public function testGetProperties()
self::assertIsString($instance->getName());
self::assertIsBool($instance->isEnabled());
self::assertIsBool($instance->hasImpressionData());
self::assertInstanceOf(DefaultResolvedVariant::class, $instance->getVariants()[0]);
self::assertInstanceOf(DefaultResolvedVariant::class, $instance->getVariant());

self::assertEquals('test', $instance->getName());
self::assertTrue($instance->isEnabled());
self::assertTrue($instance->hasImpressionData());
self::assertEquals('someVariant', $instance->getVariants()[0]->getName());
self::assertEquals('someVariant', $instance->getVariant()->getName());
}

public function testToJson()
{
$instance = new DefaultProxyFeature(['name' => 'test', 'enabled' => true, 'impression_data' => true, 'variant' => ['name' => 'someVariant', 'enabled' => true, 'payload' => ['type' => 'string', 'value' => 'test']]]);
$json = json_encode($instance);
self::assertEquals('{"name":"test","enabled":true,"variant":{"name":"someVariant","enabled":true,"payload":{"type":"string","value":"test"}},"impressionData":true}', $json);
self::assertEquals('{"name":"test","enabled":true,"variant":{"name":"someVariant","enabled":true,"payload":{"type":"string","value":"test"}},"impression_data":true}', $json);
}
}

0 comments on commit e0f8cb7

Please sign in to comment.