AdminUserExampleFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A create() 0 20 3
A configureOptions() 0 17 1
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
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
namespace App\Fixture\Factory;
13
14
use App\Entity\User\AdminUserInterface;
15
use Sylius\Component\Resource\Factory\FactoryInterface;
16
use Symfony\Component\OptionsResolver\Options;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
class AdminUserExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
20
{
21
    /**
22
     * @var FactoryInterface
23
     */
24
    private $adminUserFactory;
25
26
    /**
27
     * @var \Faker\Generator
28
     */
29
    private $faker;
30
31
    /**
32
     * @var OptionsResolver
33
     */
34
    private $optionsResolver;
35
36
    /**
37
     * AdminUserExampleFactory constructor.
38
     *
39
     * @param FactoryInterface $adminUserFactory
40
     */
41
    public function __construct(FactoryInterface $adminUserFactory)
42
    {
43
        $this->adminUserFactory = $adminUserFactory;
44
45
        $this->faker = \Faker\Factory::create();
46
        $this->optionsResolver = new OptionsResolver();
47
48
        $this->configureOptions($this->optionsResolver);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function create(array $options = [])
55
    {
56
        $options = $this->optionsResolver->resolve($options);
57
58
        /** @var AdminUserInterface $user */
59
        $user = $this->adminUserFactory->createNew();
60
        $user->setEmail($options['email']);
61
        $user->setUsername($options['username']);
62
        $user->setPlainPassword($options['password']);
63
        $user->setEnabled($options['enabled']);
64
65
        if (isset($options['first_name'])) {
66
            $user->setFirstName($options['first_name']);
67
        }
68
        if (isset($options['last_name'])) {
69
            $user->setLastName($options['last_name']);
70
        }
71
72
        return $user;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function configureOptions(OptionsResolver $resolver)
79
    {
80
        $resolver
81
            ->setDefault('email', 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...
82
                return $this->faker->email;
83
            })
84
            ->setDefault('username', function (Options $options): string {
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...
85
                return $this->faker->firstName.' '.$this->faker->lastName;
86
            })
87
            ->setDefault('enabled', true)
88
            ->setAllowedTypes('enabled', 'bool')
89
            ->setDefault('password', 'password')
90
            ->setDefault('api', false)
91
            ->setDefined('first_name')
92
            ->setDefined('last_name')
93
        ;
94
    }
95
}
96