Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5814e1

Browse files
committed
Admin avatar - fixtures
1 parent 77723e5 commit a5814e1

6 files changed

Lines changed: 46 additions & 2 deletions

File tree

src/Sylius/Bundle/CoreBundle/Fixture/AdminUserFixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi
4040
->scalarNode('locale_code')->cannotBeEmpty()->end()
4141
->scalarNode('first_name')->cannotBeEmpty()->end()
4242
->scalarNode('last_name')->cannotBeEmpty()->end()
43+
->scalarNode('avatar')->cannotBeEmpty()->end()
4344
;
4445
}
4546
}

src/Sylius/Bundle/CoreBundle/Fixture/Factory/AdminUserExampleFactory.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
namespace Sylius\Bundle\CoreBundle\Fixture\Factory;
1515

1616
use Sylius\Component\Core\Model\AdminUserInterface;
17+
use Sylius\Component\Core\Model\AvatarImage;
18+
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
1719
use Sylius\Component\Resource\Factory\FactoryInterface;
20+
use Symfony\Component\Config\FileLocatorInterface;
21+
use Symfony\Component\HttpFoundation\File\UploadedFile;
1822
use Symfony\Component\OptionsResolver\Options;
1923
use Symfony\Component\OptionsResolver\OptionsResolver;
2024

@@ -32,15 +36,28 @@ class AdminUserExampleFactory extends AbstractExampleFactory implements ExampleF
3236
/** @var string */
3337
private $localeCode;
3438

35-
public function __construct(FactoryInterface $userFactory, string $localeCode)
36-
{
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,
49+
ImageUploaderInterface $imageUploader
50+
) {
3751
$this->userFactory = $userFactory;
3852
$this->localeCode = $localeCode;
3953

4054
$this->faker = \Faker\Factory::create();
4155
$this->optionsResolver = new OptionsResolver();
4256

4357
$this->configureOptions($this->optionsResolver);
58+
59+
$this->fileLocator = $fileLocator;
60+
$this->imageUploader = $imageUploader;
4461
}
4562

4663
/**
@@ -70,6 +87,8 @@ public function create(array $options = []): AdminUserInterface
7087
$user->addRole('ROLE_API_ACCESS');
7188
}
7289

90+
$this->createAvatar($user, $options);
91+
7392
return $user;
7493
}
7594

@@ -92,6 +111,26 @@ protected function configureOptions(OptionsResolver $resolver): void
92111
->setDefault('api', false)
93112
->setDefined('first_name')
94113
->setDefined('last_name')
114+
->setDefault('avatar', '')
115+
->setAllowedTypes('avatar', 'string')
95116
;
96117
}
118+
119+
private function createAvatar(AdminUserInterface $adminUser, array $options): void
120+
{
121+
if ($options['avatar'] === null) {
122+
return;
123+
}
124+
$imagePath = $options['avatar'];
125+
126+
$imagePath = $this->fileLocator === null ? $imagePath : $this->fileLocator->locate($imagePath);
127+
$uploadedImage = new UploadedFile($imagePath, basename($imagePath));
128+
129+
$avatarImage = new AvatarImage();
130+
$avatarImage->setFile($uploadedImage);
131+
132+
$this->imageUploader->upload($avatarImage);
133+
134+
$adminUser->setAvatar($avatarImage);
135+
}
97136
}

src/Sylius/Bundle/CoreBundle/Resources/config/app/fixtures/shop_configuration.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ sylius_fixtures:
111111
locale_code: '%locale%'
112112
first_name: 'John'
113113
last_name: 'Doe'
114+
avatar: '@SyliusCoreBundle/Resources/fixtures/adminAvatars/john.jpg'
114115

115116
- email: '[email protected]'
116117
username: 'api'
@@ -120,6 +121,7 @@ sylius_fixtures:
120121
first_name: 'Luke'
121122
last_name: 'Brushwood'
122123
api: true
124+
avatar: '@SyliusCoreBundle/Resources/fixtures/adminAvatars/luke.jpg'
123125

124126
tax_category:
125127
options:

src/Sylius/Bundle/CoreBundle/Resources/config/services/fixtures_factories.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
<service id="sylius.fixture.example_factory.admin_user" class="Sylius\Bundle\CoreBundle\Fixture\Factory\AdminUserExampleFactory">
5555
<argument type="service" id="sylius.factory.admin_user" />
5656
<argument>%locale%</argument>
57+
<argument type="service" id="file_locator" />
58+
<argument type="service" id="sylius.image_uploader" />
5759
</service>
5860

5961
<service id="sylius.fixture.example_factory.promotion" class="Sylius\Bundle\CoreBundle\Fixture\Factory\PromotionExampleFactory">
100 KB
Loading
213 KB
Loading

0 commit comments

Comments
 (0)