Completed
Pull Request — master (#1)
by Kamil
02:31
created

StaticContentExampleFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 6
dl 0
loc 60
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
A create() 0 13 1
1
<?php
2
3
namespace Lakion\SyliusCmsBundle\Fixture\Factory;
4
5
use Lakion\SyliusCmsBundle\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
                ->setDefault('name', function (Options $options) {
43
                    return StringInflector::nameToCode($options['title']);
44
                })
45
                ->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...
46
                    return $this->faker->paragraphs(4, true);
47
                })
48
                ->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...
49
                    return $this->faker->boolean(90);
50
                })
51
                ->setAllowedTypes('publishable', 'bool')
52
        ;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function create(array $options = [])
59
    {
60
        $options = $this->optionsResolver->resolve($options);
61
62
        /** @var StaticContent $staticContent */
63
        $staticContent = $this->staticContentFactory->createNew();
64
        $staticContent->setTitle($options['title']);
65
        $staticContent->setName($options['name']);
66
        $staticContent->setBody($options['body']);
67
        $staticContent->setPublishable($options['publishable']);
68
69
        return $staticContent;
70
    }
71
}
72