Skip to content

Commit

Permalink
Added server key based host_id detector (#7)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel Agaletskiy <pavel.agaletskiy@lamoda.ru>
  • Loading branch information
omnilight and Pavel Agaletskiy authored Apr 6, 2020
1 parent 90bcc88 commit 548eb45
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Factory/FileBasedEnvResolvingStrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Lamoda\MultiEnv\Strategy\FileBasedEnvResolvingStrategy;
use Lamoda\MultiEnv\Strategy\RawEnvResolvingStrategy;

/**
* @deprecated This factory must be implemented in the client code. It will be removed in version 1.0
*/
class FileBasedEnvResolvingStrategyFactory
{
public static function createStrategy(
Expand All @@ -23,6 +26,11 @@ public static function createStrategy(
string $envFileName,
string $basePathToEnvFile
): FileBasedEnvResolvingStrategy {
@trigger_error(
sprintf('Factory %s is deprecated. It must be implemented in the client code. It will be removed in version 1.0', self::class),
E_USER_DEPRECATED
);

return new FileBasedEnvResolvingStrategy(
new FirstSuccessfulHostDetector([
new ServerHeadersBasedHostDetector($serverHeaderToSearch),
Expand Down
8 changes: 8 additions & 0 deletions src/Factory/HostBasedEnvResolvingStrategyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
use Lamoda\MultiEnv\HostDetector\ServerHeadersBasedHostDetector;
use Lamoda\MultiEnv\Strategy\HostBasedEnvResolvingStrategy;

/**
* @deprecated This factory must be implemented in the client code. It will be removed in version 1.0
*/
class HostBasedEnvResolvingStrategyFactory
{
public static function createStrategy(
string $serverHeaderToSearch,
string $cliArgToSearch,
string $delimiter
): HostBasedEnvResolvingStrategy {
@trigger_error(
sprintf('Factory %s is deprecated. It must be implemented in the client code. It will be removed in version 1.0', self::class),
E_USER_DEPRECATED
);

return new HostBasedEnvResolvingStrategy(
new FirstSuccessfulHostDetector([
new ServerHeadersBasedHostDetector($serverHeaderToSearch),
Expand Down
37 changes: 37 additions & 0 deletions src/HostDetector/HostIdMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Lamoda\MultiEnv\HostDetector;

use Lamoda\MultiEnv\Model\HostId;

final class HostIdMapper implements HostDetectorInterface
{
/**
* @var HostDetectorInterface
*/
private $inner;
/**
* @var string[]
*/
private $hostIdMap;

/**
* @param HostDetectorInterface $inner
* @param string[] $hostIdMap
*/
public function __construct(HostDetectorInterface $inner, array $hostIdMap = [])
{
$this->inner = $inner;
$this->hostIdMap = $hostIdMap;
}

public function getCurrentHost(): HostId
{
$innerHostId = $this->inner->getCurrentHost();
$value = $this->hostIdMap[(string)$innerHostId] ?? '';

return new HostId($value);
}
}
64 changes: 64 additions & 0 deletions tests/Unit/HostDetector/HostIdMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Lamoda\MultiEnvTests\Unit\HostDetector;

use Lamoda\MultiEnv\HostDetector\HostDetectorInterface;
use Lamoda\MultiEnv\HostDetector\HostIdMapper;
use Lamoda\MultiEnv\Model\HostId;
use PHPUnit\Framework\TestCase;

final class HostIdMapperTest extends TestCase
{
/**
* @var HostDetectorInterface
*/
private $inner;

/**
* @var HostIdMapper
*/
private $mapper;

protected function setUp(): void
{
$this->inner = $this->createMock(HostDetectorInterface::class);
$this->mapper = new HostIdMapper($this->inner, [
'host_from_1' => 'host_to_1',
'host_from_2' => 'host_to_2',
]);
}

/**
* @dataProvider dataGetCurrentHost
*/
public function testGetCurrentHost(HostId $innerResponse, HostId $expected): void
{
$this->inner
->method('getCurrentHost')
->willReturn($innerResponse);

$result = $this->mapper->getCurrentHost();

$this->assertEquals($expected, $result);
}

public function dataGetCurrentHost(): iterable
{
yield [
new HostId(''),
new HostId(''),
];

yield [
new HostId('host_from_1'),
new HostId('host_to_1'),
];

yield [
new HostId('unknown'),
new HostId(''),
];
}
}

0 comments on commit 548eb45

Please sign in to comment.