Completed
Push — master ( 8cc523...26132b )
by Kamil
18:11
created

AdminUserExampleFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B create() 0 26 4
A configureOptions() 0 18 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\CoreBundle\Fixture\Factory;
13
14
use Sylius\Component\Core\Model\AdminUserInterface;
15
use Sylius\Component\Resource\Factory\FactoryInterface;
16
use Symfony\Component\OptionsResolver\Options;
17
use Symfony\Component\OptionsResolver\OptionsResolver;
18
19
/**
20
 * @author Arkadiusz Krakowiak <[email protected]>
21
 */
22
class AdminUserExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface
23
{
24
    /**
25
     * @var FactoryInterface
26
     */
27
    private $userFactory;
28
29
    /**
30
     * @var \Faker\Generator
31
     */
32
    private $faker;
33
34
    /**
35
     * @var OptionsResolver
36
     */
37
    private $optionsResolver;
38
39
    /**
40
     * @var string
41
     */
42
    private $localeCode;
43
44
    /**
45
     * @param FactoryInterface $userFactory
46
     * @param string $localeCode
47
     */
48
    public function __construct(FactoryInterface $userFactory, $localeCode)
49
    {
50
        $this->userFactory = $userFactory;
51
        $this->localeCode = $localeCode;
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 AdminUserInterface $user */
67
        $user = $this->userFactory->createNew();
68
        $user->setEmail($options['email']);
69
        $user->setUsername($options['username']);
70
        $user->setPlainPassword($options['password']);
71
        $user->setEnabled($options['enabled']);
72
        $user->addRole('ROLE_ADMINISTRATION_ACCESS');
73
        $user->setLocaleCode($options['locale_code']);
74
75
        if (isset($options['first_name'])) {
76
            $user->setFirstName($options['first_name']);
77
        }
78
        if (isset($options['last_name'])) {
79
            $user->setLastName($options['last_name']);
80
        }
81
82
        if ($options['api']) {
83
            $user->addRole('ROLE_API_ACCESS');
84
        }
85
86
        return $user;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function configureOptions(OptionsResolver $resolver)
93
    {
94
        $resolver
95
            ->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...
96
                return $this->faker->email;
97
            })
98
            ->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...
99
                return $this->faker->firstName.' '.$this->faker->lastName;
100
            })
101
            ->setDefault('enabled', true)
102
            ->setAllowedTypes('enabled', 'bool')
103
            ->setDefault('password', 'password123')
104
            ->setDefault('locale_code', $this->localeCode)
105
            ->setDefault('api', false)
106
            ->setDefined('first_name')
107
            ->setDefined('last_name')
108
        ;
109
    }
110
}
111