55namespace Webgriffe \SyliusClerkPlugin \DataSyncInfrastructure \Normalizer ;
66
77use Liip \ImagineBundle \Imagine \Cache \CacheManager ;
8+ use Sylius \Component \Core \Model \CatalogPromotionInterface ;
89use Sylius \Component \Core \Model \ChannelInterface ;
910use Sylius \Component \Core \Model \ProductImageInterface ;
1011use Sylius \Component \Core \Model \ProductInterface ;
1112use Sylius \Component \Core \Model \ProductTranslationInterface ;
1213use Sylius \Component \Core \Model \ProductVariantInterface ;
1314use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
15+ use Webgriffe \SyliusClerkPlugin \DataSyncInfrastructure \Normalizer \Event \ProductVariantNormalizerEvent ;
1416use Webmozart \Assert \Assert ;
1517
18+ /**
19+ * @psalm-import-type clerkIoProductData from ProductVariantNormalizerEvent
20+ */
1621trait CommonProductNormalizerTrait
1722{
23+ abstract private function getImageFilterToApply (): string ;
24+
25+ abstract private function getFallbackLocale (): string ;
26+
1827 abstract private function getUrlGenerator (): UrlGeneratorInterface ;
1928
2029 abstract private function getCacheManager (): CacheManager ;
@@ -45,7 +54,7 @@ private function getMainImage(ProductInterface $product, ?ProductVariantInterfac
4554 return null ;
4655 }
4756
48- public function getUrlOfImage (ProductImageInterface $ productMainImage , ChannelInterface $ channel ): string
57+ private function getUrlOfImage (ProductImageInterface $ productMainImage , ChannelInterface $ channel ): string
4958 {
5059 $ channelRequestContext = $ this ->getUrlGenerator ()->getContext ();
5160 $ previousHost = $ channelRequestContext ->getHost ();
@@ -58,7 +67,7 @@ public function getUrlOfImage(ProductImageInterface $productMainImage, ChannelIn
5867
5968 $ imageUrl = $ this ->getCacheManager ()->getBrowserPath (
6069 $ imagePath ,
61- $ this ->imageFilterToApply ,
70+ $ this ->getImageFilterToApply () ,
6271 );
6372
6473 $ channelRequestContext ->setHost ($ previousHost );
@@ -104,4 +113,87 @@ private function getCategoryIds(ProductInterface $product): array
104113
105114 return $ categoryIds ;
106115 }
116+
117+ /**
118+ * @psalm-suppress InvalidReturnType
119+ *
120+ * @param clerkIoProductData $productData
121+ *
122+ * @return clerkIoProductData
123+ */
124+ private function enrichProductDataFromProduct (array $ productData , ProductInterface $ product , ChannelInterface $ channel , string $ localeCode ): array
125+ {
126+ foreach ($ product ->getAttributesByLocale ($ localeCode , $ this ->getFallbackLocale ()) as $ attribute ) {
127+ /** @var bool|string|int|float|\DateTimeInterface|array|null $value */
128+ $ value = $ attribute ->getValue ();
129+ if ($ value instanceof \DateTimeInterface) {
130+ $ value = $ value ->format ('Y-m-d H:i:s ' );
131+ }
132+ $ productData ['attribute_ ' . (string ) $ attribute ->getCode ()] = $ value ;
133+ }
134+ /** @var array<string, string[]> $optionWithValues */
135+ $ optionWithValues = [];
136+ foreach ($ product ->getEnabledVariants () as $ productVariant ) {
137+ foreach ($ productVariant ->getOptionValues () as $ optionValue ) {
138+ $ productOption = $ optionValue ->getOption ();
139+ if (null === $ productOption ) {
140+ continue ;
141+ }
142+ $ optionWithValues [(string ) $ productOption ->getCode ()][] = (string ) $ optionValue ->getValue ();
143+ }
144+ }
145+ foreach ($ optionWithValues as $ optionCode => $ optionValues ) {
146+ $ productData ['product_option_ ' . $ optionCode ] = $ optionValues ;
147+ }
148+ $ productData ['product_code ' ] = $ product ->getCode ();
149+ $ productData ['is_simple ' ] = $ product ->isSimple ();
150+ $ productData ['variant_selection_method ' ] = $ product ->getVariantSelectionMethod ();
151+
152+ return $ productData ; //@phpstan-ignore-line
153+ }
154+
155+ /**
156+ * @param clerkIoProductData $productData
157+ *
158+ * @return clerkIoProductData
159+ */
160+ private function enrichProductDataFromProductTranslation (array $ productData , ProductTranslationInterface $ productTranslation ): array
161+ {
162+ $ productData ['slug ' ] = $ productTranslation ->getSlug ();
163+ $ productData ['short_description ' ] = $ productTranslation ->getShortDescription ();
164+ $ productData ['meta_keywords ' ] = $ productTranslation ->getMetaKeywords ();
165+ $ productData ['meta_description ' ] = $ productTranslation ->getMetaDescription ();
166+
167+ return $ productData ; //@phpstan-ignore-line
168+ }
169+
170+ /**
171+ * @param clerkIoProductData $productData
172+ *
173+ * @return clerkIoProductData
174+ */
175+ private function enrichProductDataFromProductVariant (array $ productData , ProductVariantInterface $ productVariant , ChannelInterface $ channel ): array
176+ {
177+ $ productData ['variant_code ' ] = $ productVariant ->getCode ();
178+ $ productData ['depth ' ] = $ productVariant ->getDepth ();
179+ $ productData ['weight ' ] = $ productVariant ->getWeight ();
180+ $ productData ['height ' ] = $ productVariant ->getHeight ();
181+ $ productData ['width ' ] = $ productVariant ->getWidth ();
182+ $ productData ['on_hand ' ] = $ productVariant ->getOnHand ();
183+ $ productData ['on_hold ' ] = $ productVariant ->getOnHold ();
184+ $ productData ['version ' ] = $ productVariant ->getVersion ();
185+ foreach ($ productVariant ->getOptionValues () as $ optionValue ) {
186+ $ productOption = $ optionValue ->getOption ();
187+ if (null === $ productOption ) {
188+ continue ;
189+ }
190+ $ productData ['variant_option_ ' . (string ) $ productOption ->getCode ()] = $ optionValue ->getValue ();
191+ }
192+ /** @var CatalogPromotionInterface $promotion */
193+ foreach ($ productVariant ->getAppliedPromotionsForChannel ($ channel ) as $ promotion ) {
194+ $ productData ['variant_promotion_ ' . (string ) $ promotion ->getCode ()] = $ promotion ->getName ();
195+ }
196+
197+ return $ productData ; //@phpstan-ignore-line
198+ }
107199}
0 commit comments