1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\Fixture\Factory; |
13
|
|
|
|
14
|
|
|
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption; |
15
|
|
|
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; |
16
|
|
|
use Sylius\Component\Core\Factory\PaymentMethodFactoryInterface; |
17
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
18
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
19
|
|
|
use Sylius\Component\Locale\Model\LocaleInterface; |
20
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
21
|
|
|
use Sylius\Component\Resource\Repository\RepositoryInterface; |
22
|
|
|
use Symfony\Component\OptionsResolver\Options; |
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Kamil Kokot <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class PaymentMethodExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
29
|
|
|
{ |
30
|
|
|
const DEFAULT_LOCALE = 'en_US'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var PaymentMethodFactoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $paymentMethodFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RepositoryInterface |
39
|
|
|
*/ |
40
|
|
|
private $localeRepository; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ChannelRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
private $channelRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Faker\Generator |
49
|
|
|
*/ |
50
|
|
|
private $faker; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var OptionsResolver |
54
|
|
|
*/ |
55
|
|
|
private $optionsResolver; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param PaymentMethodFactoryInterface $paymentMethodFactory |
59
|
|
|
* @param RepositoryInterface $localeRepository |
60
|
|
|
* @param ChannelRepositoryInterface $channelRepository |
61
|
|
|
*/ |
62
|
|
|
public function __construct( |
63
|
|
|
PaymentMethodFactoryInterface $paymentMethodFactory, |
64
|
|
|
RepositoryInterface $localeRepository, |
65
|
|
|
ChannelRepositoryInterface $channelRepository |
66
|
|
|
) { |
67
|
|
|
$this->paymentMethodFactory = $paymentMethodFactory; |
68
|
|
|
$this->localeRepository = $localeRepository; |
69
|
|
|
$this->channelRepository = $channelRepository; |
70
|
|
|
|
71
|
|
|
$this->faker = \Faker\Factory::create(); |
72
|
|
|
$this->optionsResolver = new OptionsResolver(); |
73
|
|
|
|
74
|
|
|
$this->configureOptions($this->optionsResolver); |
75
|
|
|
} |
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
|
|
public function create(array $options = []) |
80
|
|
|
{ |
81
|
|
|
$options = $this->optionsResolver->resolve($options); |
82
|
|
|
|
83
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
84
|
|
|
$paymentMethod = $this->paymentMethodFactory->createWithGateway($options['gatewayFactory']); |
85
|
|
|
$paymentMethod->getGatewayConfig()->setGatewayName($options['gatewayName']); |
86
|
|
|
$paymentMethod->getGatewayConfig()->setConfig($options['gatewayConfig']); |
87
|
|
|
|
88
|
|
|
$paymentMethod->setCode($options['code']); |
89
|
|
|
$paymentMethod->setEnabled($options['enabled']); |
90
|
|
|
|
91
|
|
|
foreach ($this->getLocales() as $localeCode) { |
92
|
|
|
$paymentMethod->setCurrentLocale($localeCode); |
93
|
|
|
$paymentMethod->setFallbackLocale($localeCode); |
94
|
|
|
|
95
|
|
|
$paymentMethod->setName($options['name']); |
96
|
|
|
$paymentMethod->setDescription($options['description']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($options['channels'] as $channel) { |
100
|
|
|
$paymentMethod->addChannel($channel); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $paymentMethod; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
protected function configureOptions(OptionsResolver $resolver) |
110
|
|
|
{ |
111
|
|
|
$resolver |
112
|
|
|
->setDefault('name', function (Options $options) { |
|
|
|
|
113
|
|
|
return $this->faker->words(3, true); |
114
|
|
|
}) |
115
|
|
|
->setDefault('code', function (Options $options) { |
116
|
|
|
return StringInflector::nameToCode($options['name']); |
117
|
|
|
}) |
118
|
|
|
->setDefault('description', function (Options $options) { |
|
|
|
|
119
|
|
|
return $this->faker->sentence(); |
120
|
|
|
}) |
121
|
|
|
->setDefault('gatewayName', 'Offline') |
122
|
|
|
->setDefault('gatewayFactory', 'offline') |
123
|
|
|
->setDefault('gatewayConfig', []) |
124
|
|
|
->setDefault('enabled', function (Options $options) { |
|
|
|
|
125
|
|
|
return $this->faker->boolean(90); |
126
|
|
|
}) |
127
|
|
|
->setDefault('channels', LazyOption::all($this->channelRepository)) |
128
|
|
|
->setAllowedTypes('channels', 'array') |
129
|
|
|
->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) |
130
|
|
|
->setAllowedTypes('enabled', 'bool') |
131
|
|
|
; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
|
|
|
|
136
|
|
|
*/ |
137
|
|
|
private function getLocales() |
138
|
|
|
{ |
139
|
|
|
/** @var LocaleInterface[] $locales */ |
140
|
|
|
$locales = $this->localeRepository->findAll(); |
141
|
|
|
if (empty($locales)) { |
142
|
|
|
yield self::DEFAULT_LOCALE; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
foreach ($locales as $locale) { |
146
|
|
|
yield $locale->getCode(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.