StaticContentExampleFactory::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 1
1
<?php
2
3
namespace Lakion\CmsPlugin\Fixture\Factory;
4
5
use Lakion\CmsPlugin\Document\StaticContent;
6
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
7
use Sylius\Component\Core\Formatter\StringInflector;
8
use Sylius\Component\Resource\Factory\FactoryInterface;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
final class StaticContentExampleFactory implements ExampleFactoryInterface
13
{
14
    /**
15
     * @var FactoryInterface
16
     */
17
    private $staticContentFactory;
18
19
    /**
20
     * @var \Faker\Generator
21
     */
22
    private $faker;
23
24
    /**
25
     * @var OptionsResolver
26
     */
27
    private $optionsResolver;
28
29
    /**
30
     * @param FactoryInterface $staticContentFactory
31
     */
32
    public function __construct(FactoryInterface $staticContentFactory)
33
    {
34
        $this->staticContentFactory = $staticContentFactory;
35
36
        $this->faker = \Faker\Factory::create();
37
        $this->optionsResolver =
38
            (new OptionsResolver())
39
                ->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...
40
                    return $this->faker->words(3, true);
41
                })
42
                ->setAllowedTypes('title', 'string')
43
44
                ->setDefault('name', function (Options $options) {
45
                    return StringInflector::nameToCode($options['title']);
46
                })
47
                ->setAllowedTypes('name', 'string')
48
49
                ->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...
50
                    return $this->faker->paragraphs(4, true);
51
                })
52
                ->setAllowedTypes('body', 'string')
53
54
                ->setDefault('meta_keywords', 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...
55
                    return $this->faker->words(10, true);
56
                })
57
                ->setAllowedTypes('meta_keywords', 'string')
58
59
                ->setDefault('meta_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...
60
                    return $this->faker->paragraphs(1, true);
61
                })
62
                ->setAllowedTypes('meta_description', 'string')
63
64
                ->setDefault('publishable', 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...
65
                    return $this->faker->boolean(90);
66
                })
67
                ->setAllowedTypes('publishable', 'bool')
68
        ;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function create(array $options = [])
75
    {
76
        $options = $this->optionsResolver->resolve($options);
77
78
        /** @var StaticContent $staticContent */
79
        $staticContent = $this->staticContentFactory->createNew();
80
        $staticContent->setTitle($options['title']);
81
        $staticContent->setName($options['name']);
82
        $staticContent->setBody($options['body']);
83
        $staticContent->setPublishable($options['publishable']);
84
        $staticContent->setMetaKeywords($options['meta_keywords']);
85
        $staticContent->setMetaDescription($options['meta_description']);
86
87
        return $staticContent;
88
    }
89
}
90