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

TaxonExampleFactory   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 10
dl 0
loc 122
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
B create() 0 28 4
A configureOptions() 0 19 1
A getLocales() 0 8 2
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 Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Component\Core\Formatter\StringInflector;
16
use Sylius\Component\Core\Model\TaxonInterface;
17
use Sylius\Component\Locale\Model\LocaleInterface;
18
use Sylius\Component\Resource\Factory\FactoryInterface;
19
use Sylius\Component\Resource\Repository\RepositoryInterface;
20
use Sylius\Component\Taxonomy\Generator\TaxonSlugGeneratorInterface;
21
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface;
22
use Symfony\Component\OptionsResolver\Options;
23
use Symfony\Component\OptionsResolver\OptionsResolver;
24
25
/**
26
 * @author Kamil Kokot <[email protected]>
27
 */
28
class TaxonExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
29
{
30
    /**
31
     * @var FactoryInterface
32
     */
33
    private $taxonFactory;
34
35
    /**
36
     * @var TaxonRepositoryInterface
37
     */
38
    private $taxonRepository;
39
40
    /**
41
     * @var RepositoryInterface
42
     */
43
    private $localeRepository;
44
45
    /**
46
     * @var \Faker\Generator
47
     */
48
    private $faker;
49
50
    /**
51
     * @var TaxonSlugGeneratorInterface
52
     */
53
    private $taxonSlugGenerator;
54
55
    /**
56
     * @var OptionsResolver
57
     */
58
    private $optionsResolver;
59
60
    /**
61
     * @param FactoryInterface $taxonFactory
62
     * @param TaxonRepositoryInterface $taxonRepository
63
     * @param RepositoryInterface $localeRepository
64
     * @param TaxonSlugGeneratorInterface $taxonSlugGenerator
65
     */
66
    public function __construct(
67
        FactoryInterface $taxonFactory,
68
        TaxonRepositoryInterface $taxonRepository,
69
        RepositoryInterface $localeRepository,
70
        TaxonSlugGeneratorInterface $taxonSlugGenerator
71
    ) {
72
        $this->taxonFactory = $taxonFactory;
73
        $this->taxonRepository = $taxonRepository;
74
        $this->localeRepository = $localeRepository;
75
        $this->taxonSlugGenerator = $taxonSlugGenerator;
76
77
        $this->faker = \Faker\Factory::create();
78
        $this->optionsResolver = new OptionsResolver();
79
80
        $this->configureOptions($this->optionsResolver);
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function create(array $options = [])
87
    {
88
        $options = $this->optionsResolver->resolve($options);
89
90
        /** @var TaxonInterface $taxon */
91
        $taxon = $this->taxonRepository->findOneBy(['code' => $options['code']]);
92
93
        if (null === $taxon) {
94
            $taxon = $this->taxonFactory->createNew();
95
        }
96
97
        $taxon->setCode($options['code']);
98
99
        foreach ($this->getLocales() as $localeCode) {
100
            $taxon->setCurrentLocale($localeCode);
101
            $taxon->setFallbackLocale($localeCode);
102
103
            $taxon->setName($options['name']);
104
            $taxon->setDescription($options['description']);
105
            $taxon->setSlug($options['slug']);
106
        }
107
108
        foreach ($options['children'] as $childOptions) {
109
            $taxon->addChild($this->create($childOptions));
110
        }
111
112
        return $taxon;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    protected function configureOptions(OptionsResolver $resolver)
119
    {
120
        $resolver
121
            ->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...
122
                return $this->faker->words(3, true);
123
            })
124
            ->setDefault('code', function (Options $options) {
125
                return StringInflector::nameToCode($options['name']);
126
            })
127
            ->setDefault('slug', function (Options $options) {
128
                return $this->taxonSlugGenerator->generate($options['name']);
129
            })
130
            ->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...
131
                return $this->faker->paragraph;
132
            })
133
            ->setDefault('children', [])
134
            ->setAllowedTypes('children', ['array'])
135
        ;
136
    }
137
138
    /**
139
     * @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...
140
     */
141
    private function getLocales()
142
    {
143
        /** @var LocaleInterface[] $locales */
144
        $locales = $this->localeRepository->findAll();
145
        foreach ($locales as $locale) {
146
            yield $locale->getCode();
147
        }
148
    }
149
}
150