Skip to content

Commit 8d85c3e

Browse files
committed
Simplify DI
1 parent 67a4913 commit 8d85c3e

File tree

3 files changed

+38
-60
lines changed

3 files changed

+38
-60
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=5.4",
14-
"trejjam/thepay-lib": "dev-namespaces",
14+
"trejjam/thepay-lib": "dev-namespaces#a0040b6",
15+
"trejjam/base-extension": "~0.5.0",
1516
"nette/di": "~2.3",
1617
"nette/application": "~2.3"
1718
},

src/DI/ThePayExtension.php

+35-58
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
namespace Trejjam\ThePay\DI;
1010

1111
use Nette,
12-
Tp;
12+
Tp,
13+
Trejjam;
1314

14-
class ThePayExtension extends Nette\DI\CompilerExtension
15+
class ThePayExtension extends Trejjam\BaseExtension\DI\BaseExtension
1516
{
16-
protected $defaults = [
17+
protected $default = [
1718
'demo' => TRUE,
1819
'merchant' => [
1920
'gateUrl' => 'https://www.thepay.cz/gate/',
@@ -36,74 +37,50 @@ class ThePayExtension extends Nette\DI\CompilerExtension
3637
'dataWebServicesWsdl' => 'https://www.thepay.cz/demo-gate/api/data-demo.wsdl',
3738
];
3839

39-
public function loadConfiguration()
40-
{
41-
parent::loadConfiguration();
42-
43-
$builder = $this->getContainerBuilder();
44-
$config = $this->createConfig();
45-
46-
$classesDefinition = [
47-
'merchantConfig' => 'Tp\MerchantConfig',
48-
'helper.dataApi' => 'Trejjam\ThePay\Helper\DataApi',
49-
];
50-
51-
$factoriesDefinition = [
52-
'paymentFactory' => 'Trejjam\ThePay\IPayment',
53-
'permanentPaymentFactory' => 'Trejjam\ThePay\IPermanentPayment',
54-
'returnedPaymentFactory' => 'Trejjam\ThePay\IReturnedPayment',
55-
'helper.radioMerchantFactory' => 'Trejjam\ThePay\Helper\IRadioMerchant',
56-
];
57-
58-
/** @var Nette\DI\ServiceDefinition[] $classes */
59-
$classes = [];
60-
61-
foreach ($classesDefinition as $k => $v) {
62-
$classes[$k] = $builder->addDefinition($this->prefix($k))
63-
->setClass($v);
64-
}
65-
66-
/** @var Nette\DI\ServiceDefinition[] $factories */
67-
$factories = [];
40+
protected $classesDefinition = [
41+
'merchantConfig' => 'Tp\MerchantConfig',
42+
'helper.dataApi' => 'Trejjam\ThePay\Helper\DataApi',
43+
];
6844

69-
foreach ($factoriesDefinition as $k => $v) {
70-
$factories[$k] = $builder->addDefinition($this->prefix($k))
71-
->setImplement($v);
72-
}
73-
}
45+
protected $factoriesDefinition = [
46+
'paymentFactory' => 'Trejjam\ThePay\IPayment',
47+
'permanentPaymentFactory' => 'Trejjam\ThePay\IPermanentPayment',
48+
'returnedPaymentFactory' => 'Trejjam\ThePay\IReturnedPayment',
49+
'helper.radioMerchantFactory' => 'Trejjam\ThePay\Helper\IRadioMerchant',
50+
];
7451

7552
public function beforeCompile()
7653
{
7754
parent::beforeCompile();
7855

79-
$builder = $this->getContainerBuilder();
8056
$config = $this->createConfig();
8157
$merchantConfig = $config['merchant'];
8258

83-
$builder->getDefinition($this->prefix('merchantConfig'))
84-
->addSetup(
85-
'$service->gateUrl = ?;' . "\n" .
86-
'$service->merchantId = ?;' . "\n" .
87-
'$service->accountId = ?;' . "\n" .
88-
'$service->password = ?;' . "\n" .
89-
'$service->dataApiPassword = ?;' . "\n" .
90-
'$service->webServicesWsdl = ?;' . "\n" .
91-
'$service->dataWebServicesWsdl = ?',
92-
[
93-
$merchantConfig['gateUrl'],
94-
$merchantConfig['merchantId'],
95-
$merchantConfig['accountId'],
96-
$merchantConfig['password'],
97-
$merchantConfig['dataApiPassword'],
98-
$merchantConfig['webServicesWsdl'],
99-
$merchantConfig['dataWebServicesWsdl'],
100-
]);
59+
$classes = $this->getClasses();
60+
61+
$classes['merchantConfig']
62+
->addSetup(
63+
'$service->gateUrl = ?;' . "\n" .
64+
'$service->merchantId = ?;' . "\n" .
65+
'$service->accountId = ?;' . "\n" .
66+
'$service->password = ?;' . "\n" .
67+
'$service->dataApiPassword = ?;' . "\n" .
68+
'$service->webServicesWsdl = ?;' . "\n" .
69+
'$service->dataWebServicesWsdl = ?',
70+
[
71+
$merchantConfig['gateUrl'],
72+
$merchantConfig['merchantId'],
73+
$merchantConfig['accountId'],
74+
$merchantConfig['password'],
75+
$merchantConfig['dataApiPassword'],
76+
$merchantConfig['webServicesWsdl'],
77+
$merchantConfig['dataWebServicesWsdl'],
78+
]);
10179
}
10280

10381
protected function createConfig()
10482
{
105-
$config = $this->getConfig($this->defaults);
106-
Nette\Utils\Validators::assert($config, 'array');
83+
$config = parent::createConfig();
10784
if ($config['demo']) {
10885
$config['merchant'] = $this->merchantDemo;
10986
}

src/Helper/DataApi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function __construct(Tp\MerchantConfig $config)
2121
{
2222
$this->config = $config;
2323
}
24-
24+
2525
/**
2626
* @param bool $onlyActive
2727
* @return Tp\DataApi\GetPaymentMethodsResponse

0 commit comments

Comments
 (0)