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