Completed
Push — symfony3 ( 06e2ee...17e915 )
by Kamil
46:11 queued 26:57
created

AdminUserExampleFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 74
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 1
B create() 0 26 4
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
final class AdminUserExampleFactory 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
     * @param FactoryInterface $userFactory
41
     * @param string $localeCode
42
     */
43
    public function __construct(FactoryInterface $userFactory, $localeCode)
44
    {
45
        $this->userFactory = $userFactory;
46
47
        $this->faker = \Faker\Factory::create();
48
        $this->optionsResolver =
49
            (new OptionsResolver())
50
                ->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...
51
                    return $this->faker->email;
52
                })
53
                ->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...
54
                    return $this->faker->firstName.' '.$this->faker->lastName;
55
                })
56
                ->setDefault('enabled', true)
57
                ->setAllowedTypes('enabled', 'bool')
58
                ->setDefault('password', 'password123')
59
                ->setDefault('locale_code', $localeCode)
60
                ->setDefault('api', false)
61
                ->setDefined('first_name')
62
                ->setDefined('last_name')
63
        ;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function create(array $options = [])
70
    {
71
        $options = $this->optionsResolver->resolve($options);
72
73
        /** @var AdminUserInterface $user */
74
        $user = $this->userFactory->createNew();
75
        $user->setEmail($options['email']);
76
        $user->setUsername($options['username']);
77
        $user->setPlainPassword($options['password']);
78
        $user->setEnabled($options['enabled']);
79
        $user->addRole('ROLE_ADMINISTRATION_ACCESS');
80
        $user->setLocaleCode($options['locale_code']);
81
82
        if (isset($options['first_name'])) {
83
            $user->setFirstName($options['first_name']);
84
        }
85
        if (isset($options['last_name'])) {
86
            $user->setLastName($options['last_name']);
87
        }
88
89
        if ($options['api']) {
90
            $user->addRole('ROLE_API_ACCESS');
91
        }
92
93
        return $user;
94
    }
95
}
96