Completed
Pull Request — demo (#202)
by Loïc
02:17
created

ArticleExampleFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of monofony.
5
 *
6
 * (c) Mobizel
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace App\Fixture\Factory;
15
16
use App\Entity\Article;
17
use Sylius\Component\Resource\Factory\FactoryInterface;
18
use Symfony\Component\OptionsResolver\Options;
19
use Symfony\Component\OptionsResolver\OptionsResolver;
20
21
class ArticleExampleFactory extends AbstractExampleFactory
22
{
23
    /**
24
     * @var FactoryInterface
25
     */
26
    private $articleFactory;
27
28
    /**
29
     * @var \Faker\Generator
30
     */
31
    private $faker;
32
33
    /**
34
     * @var OptionsResolver
35
     */
36
    private $optionsResolver;
37
38
    /**
39
     * @param FactoryInterface $articleFactory
40
     */
41
    public function __construct(FactoryInterface $articleFactory)
42
    {
43
        $this->articleFactory = $articleFactory;
44
45
        $this->faker = \Faker\Factory::create();
46
        $this->optionsResolver = new OptionsResolver();
47
        $this->configureOptions($this->optionsResolver);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function configureOptions(OptionsResolver $resolver)
54
    {
55
        $resolver
56
            ->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...
57
                return ucfirst($this->faker->words(3, true));
58
            });
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function create(array $options = [])
65
    {
66
        $options = $this->optionsResolver->resolve($options);
67
68
        /** @var Article $article */
69
        $article = $this->articleFactory->createNew();
70
        $article->setTitle($options['title']);
71
72
        return $article;
73
    }
74
}
75