forked from shlinkio/shlink-php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request shlinkio#13 from acelaya-forks/feature/client-builder
Feature/client builder
- Loading branch information
Showing
8 changed files
with
344 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\SDK\Builder; | ||
|
||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfigInterface; | ||
use Shlinkio\Shlink\SDK\Domains\DomainsClient; | ||
use Shlinkio\Shlink\SDK\Domains\DomainsClientInterface; | ||
use Shlinkio\Shlink\SDK\Http\Debug\HttpDebuggerInterface; | ||
use Shlinkio\Shlink\SDK\Http\HttpClient; | ||
use Shlinkio\Shlink\SDK\Http\HttpClientInterface; | ||
use Shlinkio\Shlink\SDK\ShortUrls\ShortUrlsClient; | ||
use Shlinkio\Shlink\SDK\ShortUrls\ShortUrlsClientInterface; | ||
use Shlinkio\Shlink\SDK\Tags\TagsClient; | ||
use Shlinkio\Shlink\SDK\Tags\TagsClientInterface; | ||
use Shlinkio\Shlink\SDK\Visits\VisitsClient; | ||
use Shlinkio\Shlink\SDK\Visits\VisitsClientInterface; | ||
|
||
class ShlinkClientBuilder implements ShlinkClientBuilderInterface | ||
{ | ||
public function __construct( | ||
private ClientInterface $client, | ||
private RequestFactoryInterface $requestFactory, | ||
private StreamFactoryInterface $streamFactory, | ||
private ?HttpDebuggerInterface $debugger = null, | ||
) { | ||
} | ||
|
||
public function buildShortUrlsClient(ShlinkConfigInterface $config): ShortUrlsClientInterface | ||
{ | ||
return new ShortUrlsClient($this->createHttpClient($config)); | ||
} | ||
|
||
public function buildVisitsClient(ShlinkConfigInterface $config): VisitsClientInterface | ||
{ | ||
return new VisitsClient($this->createHttpClient($config)); | ||
} | ||
|
||
public function buildTagsClient(ShlinkConfigInterface $config): TagsClientInterface | ||
{ | ||
return new TagsClient($this->createHttpClient($config)); | ||
} | ||
|
||
public function buildDomainsClient(ShlinkConfigInterface $config): DomainsClientInterface | ||
{ | ||
return new DomainsClient($this->createHttpClient($config)); | ||
} | ||
|
||
private function createHttpClient(ShlinkConfigInterface $config): HttpClientInterface | ||
{ | ||
return new HttpClient($this->client, $this->requestFactory, $this->streamFactory, $config, $this->debugger); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\SDK\Builder; | ||
|
||
use Shlinkio\Shlink\SDK\Config\ShlinkConfigInterface; | ||
use Shlinkio\Shlink\SDK\Domains\DomainsClientInterface; | ||
use Shlinkio\Shlink\SDK\ShortUrls\ShortUrlsClientInterface; | ||
use Shlinkio\Shlink\SDK\Tags\TagsClientInterface; | ||
use Shlinkio\Shlink\SDK\Visits\VisitsClientInterface; | ||
|
||
interface ShlinkClientBuilderInterface | ||
{ | ||
public function buildShortUrlsClient(ShlinkConfigInterface $config): ShortUrlsClientInterface; | ||
|
||
public function buildVisitsClient(ShlinkConfigInterface $config): VisitsClientInterface; | ||
|
||
public function buildTagsClient(ShlinkConfigInterface $config): TagsClientInterface; | ||
|
||
public function buildDomainsClient(ShlinkConfigInterface $config): DomainsClientInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Shlinkio\Shlink\SDK\Builder; | ||
|
||
use Shlinkio\Shlink\SDK\Config\ShlinkConfigInterface; | ||
use Shlinkio\Shlink\SDK\Domains\DomainsClientInterface; | ||
use Shlinkio\Shlink\SDK\ShortUrls\ShortUrlsClientInterface; | ||
use Shlinkio\Shlink\SDK\Tags\TagsClientInterface; | ||
use Shlinkio\Shlink\SDK\Visits\VisitsClientInterface; | ||
|
||
use function sprintf; | ||
|
||
class SingletonShlinkClientBuilder implements ShlinkClientBuilderInterface | ||
{ | ||
private array $instances = []; | ||
|
||
public function __construct(private ShlinkClientBuilderInterface $wrapped) | ||
{ | ||
} | ||
|
||
public function buildShortUrlsClient(ShlinkConfigInterface $config): ShortUrlsClientInterface | ||
{ | ||
$key = $this->configToKey($config); | ||
return $this->instances[ShortUrlsClientInterface::class][$key] ?? ( | ||
$this->instances[ShortUrlsClientInterface::class][$key] = $this->wrapped->buildShortUrlsClient($config) | ||
); | ||
} | ||
|
||
public function buildVisitsClient(ShlinkConfigInterface $config): VisitsClientInterface | ||
{ | ||
$key = $this->configToKey($config); | ||
return $this->instances[VisitsClientInterface::class][$key] ?? ( | ||
$this->instances[VisitsClientInterface::class][$key] = $this->wrapped->buildVisitsClient($config) | ||
); | ||
} | ||
|
||
public function buildTagsClient(ShlinkConfigInterface $config): TagsClientInterface | ||
{ | ||
$key = $this->configToKey($config); | ||
return $this->instances[TagsClientInterface::class][$key] ?? ( | ||
$this->instances[TagsClientInterface::class][$key] = $this->wrapped->buildTagsClient($config) | ||
); | ||
} | ||
|
||
public function buildDomainsClient(ShlinkConfigInterface $config): DomainsClientInterface | ||
{ | ||
$key = $this->configToKey($config); | ||
return $this->instances[DomainsClientInterface::class][$key] ?? ( | ||
$this->instances[DomainsClientInterface::class][$key] = $this->wrapped->buildDomainsClient($config) | ||
); | ||
} | ||
|
||
private function configToKey(ShlinkConfigInterface $config): string | ||
{ | ||
return sprintf('%s_%s', $config->baseUrl(), $config->apiKey()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\Builder; | ||
|
||
use ReflectionClass; | ||
use Shlinkio\Shlink\SDK\Builder\ShlinkClientBuilderInterface; | ||
|
||
trait ClientBuilderMethodsProviderTrait | ||
{ | ||
public function provideMethods(): iterable | ||
{ | ||
$ref = new ReflectionClass(ShlinkClientBuilderInterface::class); | ||
|
||
foreach ($ref->getMethods() as $method) { | ||
$name = $method->getName(); | ||
yield $name => [$name]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ShlinkioTest\Shlink\SDK\Builder; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Shlinkio\Shlink\SDK\Builder\ShlinkClientBuilder; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfig; | ||
use Shlinkio\Shlink\SDK\Config\ShlinkConfigInterface; | ||
|
||
class ShlinkClientBuilderTest extends TestCase | ||
{ | ||
use ClientBuilderMethodsProviderTrait; | ||
use ProphecyTrait; | ||
|
||
private ShlinkClientBuilder $builder; | ||
private ObjectProphecy $client; | ||
private ObjectProphecy $requestFactory; | ||
private ObjectProphecy $streamFactory; | ||
private ShlinkConfigInterface $config; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = $this->prophesize(ClientInterface::class); | ||
$this->requestFactory = $this->prophesize(RequestFactoryInterface::class); | ||
$this->streamFactory = $this->prophesize(StreamFactoryInterface::class); | ||
|
||
$this->builder = new ShlinkClientBuilder( | ||
$this->client->reveal(), | ||
$this->requestFactory->reveal(), | ||
$this->streamFactory->reveal(), | ||
); | ||
$this->config = ShlinkConfig::fromBaseUrlAndApiKey('foo', 'bar'); | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider provideMethods | ||
*/ | ||
public function buildClientReturnsAlwaysNewInstances(string $method): void | ||
{ | ||
$instance1 = $this->builder->{$method}($this->config); | ||
$instance2 = $this->builder->{$method}($this->config); | ||
|
||
self::assertEquals($instance1, $instance2); | ||
self::assertNotSame($instance1, $instance2); | ||
} | ||
} |
Oops, something went wrong.