|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\CoreBundle\Fixture\Factory; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Component\Core\Model\AdminUserInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\AvatarImage; |
|
18
|
|
|
use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
|
19
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
|
20
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
22
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
23
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
24
|
|
|
|
|
25
|
|
|
class AdminUserExampleFactory extends AbstractExampleFactory implements ExampleFactoryInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var FactoryInterface */ |
|
28
|
|
|
private $userFactory; |
|
29
|
|
|
|
|
30
|
|
|
/** @var \Faker\Generator */ |
|
31
|
|
|
private $faker; |
|
32
|
|
|
|
|
33
|
|
|
/** @var OptionsResolver */ |
|
34
|
|
|
private $optionsResolver; |
|
35
|
|
|
|
|
36
|
|
|
/** @var string */ |
|
37
|
|
|
private $localeCode; |
|
38
|
|
|
|
|
39
|
|
|
/** @var FileLocatorInterface */ |
|
40
|
|
|
private $fileLocator; |
|
41
|
|
|
|
|
42
|
|
|
/** @var ImageUploaderInterface */ |
|
43
|
|
|
private $imageUploader; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct( |
|
46
|
|
|
FactoryInterface $userFactory, |
|
47
|
|
|
string $localeCode, |
|
48
|
|
|
?FileLocatorInterface $fileLocator = null, |
|
49
|
|
|
?ImageUploaderInterface $imageUploader = null |
|
50
|
|
|
) { |
|
51
|
|
|
$this->userFactory = $userFactory; |
|
52
|
|
|
$this->localeCode = $localeCode; |
|
53
|
|
|
|
|
54
|
|
|
$this->faker = \Faker\Factory::create(); |
|
55
|
|
|
$this->optionsResolver = new OptionsResolver(); |
|
56
|
|
|
|
|
57
|
|
|
$this->configureOptions($this->optionsResolver); |
|
58
|
|
|
|
|
59
|
|
|
$this->fileLocator = $fileLocator; |
|
60
|
|
|
$this->imageUploader = $imageUploader; |
|
61
|
|
|
|
|
62
|
|
|
if ($this->fileLocator === null || $this->imageUploader === null) { |
|
63
|
|
|
@trigger_error(sprintf('Not passing a $fileLocator or/and $imageUploader to %s constructor is deprecated since Sylius 1.6 and will be removed in Sylius 2.0.', self::class), \E_USER_DEPRECATED); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function create(array $options = []): AdminUserInterface |
|
71
|
|
|
{ |
|
72
|
|
|
$options = $this->optionsResolver->resolve($options); |
|
73
|
|
|
|
|
74
|
|
|
/** @var AdminUserInterface $user */ |
|
75
|
|
|
$user = $this->userFactory->createNew(); |
|
76
|
|
|
$user->setEmail($options['email']); |
|
77
|
|
|
$user->setUsername($options['username']); |
|
78
|
|
|
$user->setPlainPassword($options['password']); |
|
79
|
|
|
$user->setEnabled($options['enabled']); |
|
80
|
|
|
$user->addRole('ROLE_ADMINISTRATION_ACCESS'); |
|
81
|
|
|
$user->setLocaleCode($options['locale_code']); |
|
82
|
|
|
|
|
83
|
|
|
if (isset($options['first_name'])) { |
|
84
|
|
|
$user->setFirstName($options['first_name']); |
|
85
|
|
|
} |
|
86
|
|
|
if (isset($options['last_name'])) { |
|
87
|
|
|
$user->setLastName($options['last_name']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($options['api']) { |
|
91
|
|
|
$user->addRole('ROLE_API_ACCESS'); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!($options['avatar'] === '')) { |
|
95
|
|
|
$this->createAvatar($user, $options); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $user; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function configureOptions(OptionsResolver $resolver): void |
|
105
|
|
|
{ |
|
106
|
|
|
$resolver |
|
107
|
|
|
->setDefault('email', function (Options $options): string { |
|
|
|
|
|
|
108
|
|
|
return $this->faker->email; |
|
109
|
|
|
}) |
|
110
|
|
|
->setDefault('username', function (Options $options): string { |
|
|
|
|
|
|
111
|
|
|
return $this->faker->firstName . ' ' . $this->faker->lastName; |
|
112
|
|
|
}) |
|
113
|
|
|
->setDefault('enabled', true) |
|
114
|
|
|
->setAllowedTypes('enabled', 'bool') |
|
115
|
|
|
->setDefault('password', 'password123') |
|
116
|
|
|
->setDefault('locale_code', $this->localeCode) |
|
117
|
|
|
->setDefault('api', false) |
|
118
|
|
|
->setDefined('first_name') |
|
119
|
|
|
->setDefined('last_name') |
|
120
|
|
|
->setDefault('avatar', '') |
|
121
|
|
|
->setAllowedTypes('avatar', 'string') |
|
122
|
|
|
; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
private function createAvatar(AdminUserInterface $adminUser, array $options): void |
|
126
|
|
|
{ |
|
127
|
|
|
if ($this->fileLocator === null || $this->imageUploader === null) { |
|
128
|
|
|
throw new \RuntimeException('You must configure a $fileLocator or/and $imageUploader'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$imagePath = $options['avatar']; |
|
132
|
|
|
|
|
133
|
|
|
$imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath); |
|
134
|
|
|
$uploadedImage = new UploadedFile($imagePath, basename($imagePath)); |
|
135
|
|
|
|
|
136
|
|
|
$avatarImage = new AvatarImage(); |
|
137
|
|
|
$avatarImage->setFile($uploadedImage); |
|
138
|
|
|
|
|
139
|
|
|
$this->imageUploader->upload($avatarImage); |
|
140
|
|
|
|
|
141
|
|
|
$adminUser->setAvatar($avatarImage); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: