1414namespace Sylius \Bundle \CoreBundle \Fixture \Factory ;
1515
1616use Sylius \Component \Core \Model \AdminUserInterface ;
17+ use Sylius \Component \Core \Model \AvatarImage ;
18+ use Sylius \Component \Core \Uploader \ImageUploaderInterface ;
1719use Sylius \Component \Resource \Factory \FactoryInterface ;
20+ use Symfony \Component \Config \FileLocatorInterface ;
21+ use Symfony \Component \HttpFoundation \File \UploadedFile ;
1822use Symfony \Component \OptionsResolver \Options ;
1923use 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}
0 commit comments