Completed
Push — master ( 8cc523...26132b )
by Kamil
18:11
created

PaymentMethodExampleFactory::configureOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 15
nc 1
nop 1
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\Formatter\StringInflector;
17
use Sylius\Component\Core\Model\PaymentMethodInterface;
18
use Sylius\Component\Locale\Model\LocaleInterface;
19
use Sylius\Component\Resource\Factory\FactoryInterface;
20
use Sylius\Component\Resource\Repository\RepositoryInterface;
21
use Symfony\Component\OptionsResolver\Options;
22
use Symfony\Component\OptionsResolver\OptionsResolver;
23
24
/**
25
 * @author Kamil Kokot <[email protected]>
26
 */
27
class PaymentMethodExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
28
{
29
    /**
30
     * @var FactoryInterface
31
     */
32
    private $paymentMethodFactory;
33
34
    /**
35
     * @var RepositoryInterface
36
     */
37
    private $localeRepository;
38
39
    /**
40
     * @var ChannelRepositoryInterface
41
     */
42
    private $channelRepository;
43
44
    /**
45
     * @var \Faker\Generator
46
     */
47
    private $faker;
48
49
    /**
50
     * @var OptionsResolver
51
     */
52
    private $optionsResolver;
53
54
    /**
55
     * @param FactoryInterface $paymentMethodFactory
56
     * @param RepositoryInterface $localeRepository
57
     * @param ChannelRepositoryInterface $channelRepository
58
     */
59
    public function __construct(
60
        FactoryInterface $paymentMethodFactory,
61
        RepositoryInterface $localeRepository,
62
        ChannelRepositoryInterface $channelRepository
63
    ) {
64
        $this->paymentMethodFactory = $paymentMethodFactory;
65
        $this->localeRepository = $localeRepository;
66
        $this->channelRepository = $channelRepository;
67
68
        $this->faker = \Faker\Factory::create();
69
        $this->optionsResolver = new OptionsResolver();
70
71
        $this->configureOptions($this->optionsResolver);
72
    }
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function create(array $options = [])
77
    {
78
        $options = $this->optionsResolver->resolve($options);
79
80
        /** @var PaymentMethodInterface $paymentMethod */
81
        $paymentMethod = $this->paymentMethodFactory->createNew();
82
83
        $paymentMethod->setCode($options['code']);
84
        $paymentMethod->setGateway($options['gateway']);
85
        $paymentMethod->setEnabled($options['enabled']);
86
87
        foreach ($this->getLocales() as $localeCode) {
88
            $paymentMethod->setCurrentLocale($localeCode);
89
            $paymentMethod->setFallbackLocale($localeCode);
90
91
            $paymentMethod->setName($options['name']);
92
            $paymentMethod->setDescription($options['description']);
93
        }
94
95
        foreach ($options['channels'] as $channel) {
96
            $paymentMethod->addChannel($channel);
97
        }
98
99
        return $paymentMethod;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    protected function configureOptions(OptionsResolver $resolver)
106
    {
107
        $resolver
108
            ->setDefault('name', function (Options $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
109
                return $this->faker->words(3, true);
110
            })
111
            ->setDefault('code', function (Options $options) {
112
                return StringInflector::nameToCode($options['name']);
113
            })
114
            ->setDefault('description', function (Options $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
115
                return $this->faker->sentence();
116
            })
117
            ->setDefault('gateway', 'offline')
118
            ->setDefault('enabled', function (Options $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
119
                return $this->faker->boolean(90);
120
            })
121
            ->setDefault('channels', LazyOption::all($this->channelRepository))
122
            ->setAllowedTypes('channels', 'array')
123
            ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code'))
124
            ->setAllowedTypes('enabled', 'bool')
125
        ;
126
    }
127
128
    /**
129
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
130
     */
131
    private function getLocales()
132
    {
133
        /** @var LocaleInterface[] $locales */
134
        $locales = $this->localeRepository->findAll();
135
        foreach ($locales as $locale) {
136
            yield $locale->getCode();
137
        }
138
    }
139
}
140