Completed
Pull Request — master (#1)
by Kamil
07:23
created

RouteExampleFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Lakion\SyliusCmsBundle\Fixture\Factory;
4
5
use Lakion\SyliusCmsBundle\Document\Route;
6
use Lakion\SyliusCmsBundle\Document\StaticContent;
7
use Sylius\Bundle\CoreBundle\Fixture\Factory\ExampleFactoryInterface;
8
use Sylius\Bundle\CoreBundle\Fixture\OptionsResolver\LazyOption;
9
use Sylius\Component\Core\Formatter\StringInflector;
10
use Sylius\Component\Resource\Factory\FactoryInterface;
11
use Sylius\Component\Resource\Repository\RepositoryInterface;
12
use Symfony\Component\OptionsResolver\Options;
13
use Symfony\Component\OptionsResolver\OptionsResolver;
14
15
final class RouteExampleFactory implements ExampleFactoryInterface
16
{
17
    /**
18
     * @var FactoryInterface
19
     */
20
    private $routeFactory;
21
22
    /**
23
     * @var \Faker\Generator
24
     */
25
    private $faker;
26
27
    /**
28
     * @var OptionsResolver
29
     */
30
    private $optionsResolver;
31
32
    /**
33
     * @param FactoryInterface $routeFactory
34
     * @param RepositoryInterface $staticContentRepository
35
     */
36
    public function __construct(FactoryInterface $routeFactory, RepositoryInterface $staticContentRepository)
37
    {
38
        $this->routeFactory = $routeFactory;
39
40
        $this->faker = \Faker\Factory::create();
41
        $this->optionsResolver =
42
            (new OptionsResolver())
43
                ->setDefault('name', 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...
44
                    return StringInflector::nameToCode($this->faker->words(3, true));
0 ignored issues
show
Bug introduced by
It seems like $this->faker->words(3, true) targeting Faker\Generator::words() can also be of type array; however, Sylius\Component\Core\Fo...Inflector::nameToCode() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
45
                })
46
                ->setDefault('content', LazyOption::randomOne($staticContentRepository))
47
                ->setAllowedTypes('content', ['string', StaticContent::class])
48
                ->setNormalizer('content', LazyOption::findOneBy($staticContentRepository, 'name'))
49
        ;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function create(array $options = [])
56
    {
57
        $options = $this->optionsResolver->resolve($options);
58
59
        /** @var Route $route */
60
        $route = $this->routeFactory->createNew();
61
        $route->setName($options['name']);
62
        $route->setContent($options['content']);
63
64
        return $route;
65
    }
66
}
67