Skip to content

Commit

Permalink
Merge pull request #3 from lamoda/feature/addAtolV5Support
Browse files Browse the repository at this point in the history
Add AtolV5 support
  • Loading branch information
DmitriyMatvienko committed Sep 20, 2021
2 parents 12c850b + d4c081b commit 6e6a5f8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"minimum-stability": "stable",
"require": {
"php": "^7.2",
"lamoda/atol-client": "^1.2.0",
"lamoda/atol-client": "^1.3.0",
"symfony/dependency-injection": "^3.4 || ^4.0",
"symfony/http-kernel": "^3.4 || ^4.0",
"symfony/config": "^3.4 || ^4.0",
Expand Down
1 change: 1 addition & 0 deletions src/AtolClientBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class AtolClientBundle extends Bundle
{
public const API_CLIENT_VERSION_3 = 3;
public const API_CLIENT_VERSION_4 = 4;
public const API_CLIENT_VERSION_5 = 5;

public function getContainerExtension()
{
Expand Down
24 changes: 24 additions & 0 deletions src/DependencyInjection/AtolClientExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lamoda\AtolClient\V3\AtolApi as AtolApiV3;
use Lamoda\AtolClient\V4\AtolApi as AtolApiV4;
use Lamoda\AtolClient\V5\AtolApi as AtolApiV5;
use Lamoda\AtolClientBundle\AtolClientBundle;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -48,6 +49,11 @@ private function createAtolClients(array $config, ContainerBuilder $container):
$container->setAlias('atol_client.v4', 'atol_client.v4.default');
$container->setAlias(AtolApiV4::class, 'atol_client.v4.default');
}

if ($container->hasDefinition('atol_client.v5.default')) {
$container->setAlias('atol_client.v5', 'atol_client.v5.default');
$container->setAlias(AtolApiV5::class, 'atol_client.v5.default');
}
}

private function createAtolClient(string $name, array $clientConfig, ContainerBuilder $container): void
Expand All @@ -59,6 +65,9 @@ private function createAtolClient(string $name, array $clientConfig, ContainerBu
case AtolClientBundle::API_CLIENT_VERSION_4:
$this->createAtolClientV4($name, $clientConfig, $container);
break;
case AtolClientBundle::API_CLIENT_VERSION_5:
$this->createAtolClientV5($name, $clientConfig, $container);
break;
default:
throw new InvalidArgumentException('Wrong client version: ' . $clientConfig['version']);
}
Expand Down Expand Up @@ -94,4 +103,19 @@ private function createAtolClientV4(string $name, array $clientConfig, Container

$container->setDefinition($id, $definition);
}

private function createAtolClientV5(string $name, array $clientConfig, ContainerBuilder $container): void
{
$definition = new Definition(AtolApiV5::class, [
new Reference('atol_client.object_converter'),
new Reference($clientConfig['guzzle_client']),
$clientConfig['guzzle_client_options'],
$clientConfig['base_url'],
]);
$definition->setPublic(false);

$id = 'atol_client.v5.' . $name;

$container->setDefinition($id, $definition);
}
}
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function getConfigTreeBuilder()
->beforeNormalization()
->ifTrue(function ($v) {
return isset($v['version'])
&& $v['version'] === AtolClientBundle::API_CLIENT_VERSION_3
&& AtolClientBundle::API_CLIENT_VERSION_3 === $v['version']
&& empty($v['cash_register_group_code']);
})
->thenInvalid('cash_register_group_code is required for Atol Api Client v3')
Expand All @@ -37,6 +37,7 @@ public function getConfigTreeBuilder()
->values([
AtolClientBundle::API_CLIENT_VERSION_3,
AtolClientBundle::API_CLIENT_VERSION_4,
AtolClientBundle::API_CLIENT_VERSION_5,
])
->defaultValue(AtolClientBundle::API_CLIENT_VERSION_3)
->end()
Expand Down
11 changes: 11 additions & 0 deletions tests/App/config/config_multiple_clients.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ atol_client:
cash_register_group_code: 'test_group2'
callback_url: null

fourth:
version: 5
guzzle_client: app.guzzle.client
guzzle_client_options: { 'a': 'test' }
base_url: 'http://test_url2'
cash_register_group_code: 'test_group2'
callback_url: null

services:
test.atol_client.v3:
alias: 'atol_client.v3.default'
Expand All @@ -33,4 +41,7 @@ services:
public: true
test.atol_client.v4.third:
alias: 'atol_client.v4.third'
public: true
test.atol_client.v5.fourth:
alias: 'atol_client.v5.fourth'
public: true
12 changes: 12 additions & 0 deletions tests/App/config/config_single_client_v5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

atol_client:
version: 5
guzzle_client: app.guzzle.client
guzzle_client_options: []
base_url: 'http://test_url'
callback_url: null

services:
test.atol_client.v5:
alias: 'atol_client.v5'
public: true
8 changes: 8 additions & 0 deletions tests/AtolClientBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lamoda\AtolClient\V3\AtolApi as AtolApiV3;
use Lamoda\AtolClient\V4\AtolApi as AtolApiV4;
use Lamoda\AtolClient\V5\AtolApi as AtolApiV5;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

final class AtolClientBundleTest extends WebTestCase
Expand Down Expand Up @@ -39,12 +40,19 @@ public function dataEnv(): array
'test.atol_client.v4' => AtolApiV4::class,
],
],
[
'single_client_v5',
[
'test.atol_client.v5' => AtolApiV5::class,
],
],
[
'multiple_clients',
[
'test.atol_client.v3' => AtolApiV3::class,
'test.atol_client.v3.second' => AtolApiV3::class,
'test.atol_client.v4.third' => AtolApiV4::class,
'test.atol_client.v5.fourth' => AtolApiV5::class,
],
],
];
Expand Down

0 comments on commit 6e6a5f8

Please sign in to comment.