Completed
Pull Request — master (#45)
by Jan
19:37
created

ProductBlockExampleFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace Lakion\CmsPlugin\Fixture\Factory;
4
5
use Lakion\CmsPlugin\Document\ProductBlock;
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
final class ProductBlockExampleFactory implements ExampleFactoryInterface
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 $productRepository;
35
36
    /**
37
     * @param FactoryInterface $factory
38
     * @param RepositoryInterface $productRepository
39
     */
40
    public function __construct(FactoryInterface $factory, RepositoryInterface $productRepository)
41
    {
42
        $this->factory = $factory;
43
        $this->productRepository = $productRepository;
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('product', LazyOption::randomOne($this->productRepository))
69
                ->setNormalizer('product', LazyOption::findOneBy($this->productRepository, 'code'))
70
        ;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function create(array $options = [])
77
    {
78
        $options = $this->optionsResolver->resolve($options);
79
80
        /** @var ProductBlock $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->setProduct($options['product']);
87
88
        return $stringBlock;
89
    }
90
}
91