44
55namespace Locastic \SyliusStoreLocatorPlugin \Fixture ;
66
7- use Sylius \ Bundle \ CoreBundle \ Fixture \ OptionsResolver \ LazyOption ;
8- use Sylius \Component \ Addressing \ Model \ ZoneInterface ;
7+ use Locastic \ SyliusStoreLocatorPlugin \ Entity \ ShippingMethodInterface ;
8+ use Sylius \Bundle \ CoreBundle \ Fixture \ Factory \ ShippingMethodExampleFactory ;
99use Sylius \Component \Channel \Repository \ChannelRepositoryInterface ;
10- use Sylius \Component \Core \Formatter \StringInflector ;
11- use Sylius \Component \Core \Model \ChannelInterface ;
12- use Sylius \Component \Core \Model \ShippingMethodInterface ;
13- use Sylius \Component \Locale \Model \LocaleInterface ;
10+ use Sylius \Component \Core \Model \ShippingMethodInterface as BaseShippingMethodInterface ;
1411use Sylius \Component \Resource \Factory \FactoryInterface ;
1512use Sylius \Component \Resource \Repository \RepositoryInterface ;
16- use Sylius \Component \Shipping \Calculator \DefaultCalculators ;
17- use Sylius \Component \Shipping \Model \ShippingCategoryInterface ;
1813use Symfony \Component \OptionsResolver \Options ;
1914use Symfony \Component \OptionsResolver \OptionsResolver ;
20- use Sylius \Bundle \CoreBundle \Fixture \Factory \AbstractExampleFactory ;
2115
22- class ShippingMethodFactory extends AbstractExampleFactory
16+ class ShippingMethodFactory extends ShippingMethodExampleFactory
2317{
24- /**
25- * @var FactoryInterface
26- */
27- private $ shippingMethodFactory ;
28-
29- /**
30- * @var RepositoryInterface
31- */
32- private $ zoneRepository ;
33-
34- /**
35- * @var RepositoryInterface
36- */
37- private $ shippingCategoryRepository ;
38-
39- /**
40- * @var RepositoryInterface
41- */
42- private $ localeRepository ;
43-
44- /**
45- * @var ChannelRepositoryInterface
46- */
47- private $ channelRepository ;
48-
4918 /**
5019 * @var \Faker\Generator
5120 */
5221 private $ faker ;
5322
54- /**
55- * @var OptionsResolver
56- */
57- private $ optionsResolver ;
58-
5923 /**
6024 * @param FactoryInterface $shippingMethodFactory
6125 * @param RepositoryInterface $zoneRepository
@@ -70,55 +34,21 @@ public function __construct(
7034 RepositoryInterface $ localeRepository ,
7135 ChannelRepositoryInterface $ channelRepository
7236 ) {
73- $ this ->shippingMethodFactory = $ shippingMethodFactory ;
74- $ this ->zoneRepository = $ zoneRepository ;
75- $ this ->shippingCategoryRepository = $ shippingCategoryRepository ;
76- $ this ->localeRepository = $ localeRepository ;
77- $ this ->channelRepository = $ channelRepository ;
78-
79- $ this ->faker = \Faker \Factory::create ();
80- $ this ->optionsResolver = new OptionsResolver ();
81-
82- $ this ->configureOptions ($ this ->optionsResolver );
37+ parent ::__construct ($ shippingMethodFactory , $ zoneRepository , $ shippingCategoryRepository , $ localeRepository , $ channelRepository );
8338 }
8439
8540 /**
8641 * {@inheritdoc}
8742 */
88- public function create (array $ options = []): ShippingMethodInterface
43+ public function create (array $ options = []): BaseShippingMethodInterface
8944 {
90- $ options = $ this ->optionsResolver ->resolve ($ options );
91-
92-
9345 /** @var ShippingMethodInterface $shippingMethod */
94- $ shippingMethod = $ this ->shippingMethodFactory ->createNew ();
95- $ shippingMethod ->setCode ($ options ['code ' ]);
96- $ shippingMethod ->setEnabled ($ options ['enabled ' ]);
97- $ shippingMethod ->setZone ($ options ['zone ' ]);
98- $ shippingMethod ->setCalculator ($ options ['calculator ' ]['type ' ]);
99- $ shippingMethod ->setConfiguration ($ options ['calculator ' ]['configuration ' ]);
100- $ shippingMethod ->setArchivedAt ($ options ['archived_at ' ]);
101-
102- if (array_key_exists ('shipping_category ' , $ options )) {
103- $ shippingMethod ->setCategory ($ options ['shipping_category ' ]);
104- }
46+ $ shippingMethod = parent ::create ($ options );
10547
10648 if (array_key_exists ('is_pickup_at_store ' , $ options )) {
10749 $ shippingMethod ->setPickupAtStore ($ options ['is_pickup_at_store ' ]);
10850 }
10951
110- foreach ($ this ->getLocales () as $ localeCode ) {
111- $ shippingMethod ->setCurrentLocale ($ localeCode );
112- $ shippingMethod ->setFallbackLocale ($ localeCode );
113-
114- $ shippingMethod ->setName ($ options ['name ' ]);
115- $ shippingMethod ->setDescription ($ options ['description ' ]);
116- }
117-
118- foreach ($ options ['channels ' ] as $ channel ) {
119- $ shippingMethod ->addChannel ($ channel );
120- }
121-
12252 return $ shippingMethod ;
12353 }
12454
@@ -127,58 +57,12 @@ public function create(array $options = []): ShippingMethodInterface
12757 */
12858 protected function configureOptions (OptionsResolver $ resolver ): void
12959 {
60+ parent ::configureOptions ($ resolver );
61+
13062 $ resolver
131- ->setDefault ('code ' , function (Options $ options ): string {
132- return StringInflector::nameToCode ($ options ['name ' ]);
133- })
134- ->setDefault ('name ' , function (Options $ options ): string {
135- return $ this ->faker ->words (3 , true );
136- })
137- ->setDefault ('description ' , function (Options $ options ): string {
138- return $ this ->faker ->sentence ();
139- })
140- ->setDefault ('enabled ' , function (Options $ options ): bool {
141- return $ this ->faker ->boolean (90 );
142- })
14363 ->setDefault ('is_pickup_at_store ' , function (Options $ options ): bool {
14464 return $ this ->faker ->boolean (90 );
14565 })
146- ->setAllowedTypes ('enabled ' , 'bool ' )
147- ->setDefault ('zone ' , LazyOption::randomOne ($ this ->zoneRepository ))
148- ->setAllowedTypes ('zone ' , ['null ' , 'string ' , ZoneInterface::class])
149- ->setNormalizer ('zone ' , LazyOption::findOneBy ($ this ->zoneRepository , 'code ' ))
150- ->setDefined ('shipping_category ' )
151- ->setAllowedTypes ('shipping_category ' , ['null ' , 'string ' , ShippingCategoryInterface::class])
152- ->setNormalizer ('shipping_category ' , LazyOption::findOneBy ($ this ->shippingCategoryRepository , 'code ' ))
153- ->setDefault ('calculator ' , function (Options $ options ): array {
154- $ configuration = [];
155- /** @var ChannelInterface $channel */
156- foreach ($ options ['channels ' ] as $ channel ) {
157- $ configuration [$ channel ->getCode ()] = ['amount ' => $ this ->faker ->randomNumber (4 )];
158- }
159-
160- return [
161- 'type ' => DefaultCalculators::FLAT_RATE ,
162- 'configuration ' => $ configuration ,
163- ];
164- })
165- ->setDefault ('channels ' , LazyOption::all ($ this ->channelRepository ))
166- ->setAllowedTypes ('channels ' , 'array ' )
167- ->setNormalizer ('channels ' , LazyOption::findBy ($ this ->channelRepository , 'code ' ))
168- ->setDefault ('archived_at ' , null )
169- ->setAllowedTypes ('archived_at ' , ['null ' , \DateTimeInterface::class])
17066 ;
17167 }
172-
173- /**
174- * @return iterable
175- */
176- private function getLocales (): iterable
177- {
178- /** @var LocaleInterface[] $locales */
179- $ locales = $ this ->localeRepository ->findAll ();
180- foreach ($ locales as $ locale ) {
181- yield $ locale ->getCode ();
182- }
183- }
18468}
0 commit comments