Completed
Push — symfony3 ( 405d0c...88ded0 )
by Kamil
32:03 queued 12:32
created

PaymentMethodExampleFactory::getLocales()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
final class PaymentMethodExampleFactory 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 =
70
            (new OptionsResolver())
71
                ->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...
72
                    return $this->faker->words(3, true);
73
                })
74
                ->setDefault('code', function (Options $options) {
75
                    return StringInflector::nameToCode($options['name']);
76
                })
77
                ->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...
78
                    return $this->faker->sentence();
79
                })
80
                ->setDefault('gateway', 'offline')
81
                ->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...
82
                    return $this->faker->boolean(90);
83
                })
84
                ->setDefault('channels', LazyOption::all($channelRepository))
85
                ->setAllowedTypes('channels', 'array')
86
                ->setNormalizer('channels', LazyOption::findBy($channelRepository, 'code'))
87
                ->setAllowedTypes('enabled', 'bool')
88
        ;
89
    }
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function create(array $options = [])
94
    {
95
        $options = $this->optionsResolver->resolve($options);
96
        
97
        /** @var PaymentMethodInterface $paymentMethod */
98
        $paymentMethod = $this->paymentMethodFactory->createNew();
99
100
        $paymentMethod->setCode($options['code']);
101
        $paymentMethod->setGateway($options['gateway']);
102
        $paymentMethod->setEnabled($options['enabled']);
103
104
        foreach ($this->getLocales() as $localeCode) {
105
            $paymentMethod->setCurrentLocale($localeCode);
106
            $paymentMethod->setFallbackLocale($localeCode);
107
108
            $paymentMethod->setName($options['name']);
109
            $paymentMethod->setDescription($options['description']);
110
        }
111
112
        foreach ($options['channels'] as $channel) {
113
            $paymentMethod->addChannel($channel);
114
        }
115
116
        return $paymentMethod;
117
    }
118
119
    /**
120
     * @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...
121
     */
122
    private function getLocales()
123
    {
124
        /** @var LocaleInterface[] $locales */
125
        $locales = $this->localeRepository->findAll();
126
        foreach ($locales as $locale) {
127
            yield $locale->getCode();
128
        }
129
    }
130
}
131