Completed
Push — master ( d3ded3...063c30 )
by Paweł
02:40
created

CustomBlockExampleFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 66
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 28 28 1
A create() 13 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Lakion\SyliusCmsBundle\Fixture\Factory;
4
5
use Lakion\SyliusCmsBundle\Document\CustomBlock;
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 View Code Duplication
final class CustomBlockExampleFactory 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...
13
{
14
    /**
15
     * @var FactoryInterface
16
     */
17
    private $factory;
18
19
    /**
20
     * @var \Faker\Generator
21
     */
22
    private $faker;
23
24
    /**
25
     * @var OptionsResolver
26
     */
27
    private $optionsResolver;
28
29
    /**
30
     * @param FactoryInterface $factory
31
     */
32
    public function __construct(FactoryInterface $factory)
33
    {
34
        $this->factory = $factory;
35
36
        $this->faker = \Faker\Factory::create();
37
        $this->optionsResolver =
38
            (new OptionsResolver())
39
                ->setDefault('name', function (Options $options) {
40
                    return StringInflector::nameToCode($options['title']);
41
                })
42
                ->setAllowedTypes('name', 'string')
43
44
                ->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...
45
                    return $this->faker->words(3, true);
46
                })
47
                ->setAllowedTypes('title', '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->sentence();
51
                })
52
                ->setAllowedTypes('body', 'string')
53
54
                ->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...
55
                    return $this->faker->url;
56
                })
57
                ->setAllowedTypes('link', 'string')
58
        ;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function create(array $options = [])
65
    {
66
        $options = $this->optionsResolver->resolve($options);
67
68
        /** @var CustomBlock $stringBlock */
69
        $stringBlock = $this->factory->createNew();
70
        $stringBlock->setName($options['name']);
71
        $stringBlock->setTitle($options['name']);
72
        $stringBlock->setBody($options['body']);
73
        $stringBlock->setLinkUrl($options['link']);
74
75
        return $stringBlock;
76
    }
77
}
78