AppUserExampleFactory::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 1
nc 1
nop 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\AppUserInterface;
15
use App\Entity\Customer\CustomerInterface;
16
use Sylius\Component\Resource\Factory\FactoryInterface;
17
use Symfony\Component\OptionsResolver\Options;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
20
class AppUserExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
21
{
22
    /**
23
     * @var FactoryInterface
24
     */
25
    private $appUserFactory;
26
27
    /**
28
     * @var FactoryInterface
29
     */
30
    private $customerFactory;
31
32
    /**
33
     * @var \Faker\Generator
34
     */
35
    private $faker;
36
37
    /**
38
     * @var OptionsResolver
39
     */
40
    private $optionsResolver;
41
42
    /**
43
     * AppUserExampleFactory constructor.
44
     *
45
     * @param FactoryInterface $appUserFactory
46
     * @param FactoryInterface $customerFactory
47
     */
48
    public function __construct(FactoryInterface $appUserFactory, FactoryInterface $customerFactory)
49
    {
50
        $this->appUserFactory = $appUserFactory;
51
        $this->customerFactory = $customerFactory;
52
53
        $this->faker = \Faker\Factory::create();
54
        $this->optionsResolver = new OptionsResolver();
55
56
        $this->configureOptions($this->optionsResolver);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function create(array $options = [])
63
    {
64
        $options = $this->optionsResolver->resolve($options);
65
66
        /** @var CustomerInterface $customer */
67
        $customer = $this->customerFactory->createNew();
68
        $customer->setEmail($options['email']);
69
        $customer->setFirstName($options['first_name']);
70
        $customer->setLastName($options['last_name']);
71
72
        /** @var AppUserInterface $user */
73
        $user = $this->appUserFactory->createNew();
74
        $user->setUsername($options['username']);
75
        $user->setPlainPassword($options['password']);
76
        $user->setEnabled($options['enabled']);
77
        $user->addRole('ROLE_USER');
78
79
        foreach ($options['roles'] as $role) {
80
            $user->addRole($role);
81
        }
82
83
        $user->setCustomer($customer);
84
85
        return $user;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function configureOptions(OptionsResolver $resolver)
92
    {
93
        $resolver
94
            ->setDefault('username', 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...
95
                return $this->faker->userName;
96
            })
97
98
            ->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...
99
                return $this->faker->email;
100
            })
101
102
            ->setDefault('first_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...
103
                return $this->faker->firstName;
104
            })
105
106
            ->setDefault('last_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...
107
                return $this->faker->lastName;
108
            })
109
110
            ->setDefault('enabled', true)
111
            ->setAllowedTypes('enabled', 'bool')
112
113
            ->setDefault('password', 'password123')
114
115
            ->setDefault('roles', [])
116
            ->setAllowedTypes('roles', 'array')
117
        ;
118
    }
119
}
120