TaxonBlockExampleFactory::__construct()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 20

Duplication

Lines 32
Ratio 100 %

Importance

Changes 0
Metric Value
dl 32
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 2
1
<?php
2
3
namespace Lakion\CmsPlugin\Fixture\Factory;
4
5
use Lakion\CmsPlugin\Document\TaxonBlock;
6
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
7
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
8
use Sylius\Component\Core\Formatter\StringInflector;
9
use Sylius\Component\Resource\Factory\FactoryInterface;
10
use Sylius\Component\Resource\Repository\RepositoryInterface;
11
use Symfony\Component\OptionsResolver\Options;
12
use Symfony\Component\OptionsResolver\OptionsResolver;
13
14 View Code Duplication
final class TaxonBlockExampleFactory implements ExampleFactoryInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
{
16
    /**
17
     * @var FactoryInterface
18
     */
19
    private $factory;
20
21
    /**
22
     * @var \Faker\Generator
23
     */
24
    private $faker;
25
26
    /**
27
     * @var OptionsResolver
28
     */
29
    private $optionsResolver;
30
31
    /**
32
     * @var RepositoryInterface
33
     */
34
    private $taxonRepository;
35
36
    /**
37
     * @param FactoryInterface $factory
38
     * @param RepositoryInterface $taxonRepository
39
     */
40
    public function __construct(FactoryInterface $factory, RepositoryInterface $taxonRepository)
41
    {
42
        $this->factory = $factory;
43
        $this->taxonRepository = $taxonRepository;
44
45
        $this->faker = \Faker\Factory::create();
46
        $this->optionsResolver =
47
            (new OptionsResolver())
48
                ->setDefault('name', function (Options $options) {
49
                    return StringInflector::nameToCode($options['title']);
50
                })
51
                ->setAllowedTypes('name', 'string')
52
53
                ->setDefault('title', 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...
54
                    return $this->faker->words(3, true);
55
                })
56
                ->setAllowedTypes('title', 'string')
57
58
                ->setDefault('body', 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...
59
                    return $this->faker->sentence();
60
                })
61
                ->setAllowedTypes('body', 'string')
62
63
                ->setDefault('link', 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...
64
                    return $this->faker->url;
65
                })
66
                ->setAllowedTypes('link', 'string')
67
68
                ->setDefault('taxon', LazyOption::randomOne($this->taxonRepository))
69
                ->setNormalizer('taxon', LazyOption::findOneBy($this->taxonRepository, 'code'))
70
        ;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function create(array $options = [])
77
    {
78
        $options = $this->optionsResolver->resolve($options);
79
80
        /** @var TaxonBlock $stringBlock */
81
        $stringBlock = $this->factory->createNew();
82
        $stringBlock->setName($options['name']);
83
        $stringBlock->setTitle($options['name']);
84
        $stringBlock->setBody($options['body']);
85
        $stringBlock->setLinkUrl($options['link']);
86
        $stringBlock->setTaxon($options['taxon']);
87
88
        return $stringBlock;
89
    }
90
}
91