|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Marcin Hubert <> |
| 4 | + * |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Spinbits\BaselinkerSdk\Handler\Sylius\Mapper; |
| 12 | + |
| 13 | +use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
| 14 | +use Sylius\Component\Attribute\Model\AttributeValueInterface; |
| 15 | +use Sylius\Component\Core\Model\ChannelInterface; |
| 16 | +use Sylius\Component\Core\Model\ChannelPricingInterface; |
| 17 | +use Sylius\Component\Core\Model\TaxonInterface; |
| 18 | +use Sylius\Component\Core\Model\ProductInterface; |
| 19 | +use Sylius\Component\Core\Model\ProducVariantInterface; |
| 20 | +use Sylius\Component\Core\Model\ProductImageInterface; |
| 21 | + |
| 22 | +class ProductMapper |
| 23 | +{ |
| 24 | + private ChannelInterface $channel; |
| 25 | + private CacheManager $cacheManager; |
| 26 | + |
| 27 | + public function __construct(ChannelInterface $channel, CacheManager $cacheManager) |
| 28 | + { |
| 29 | + $this->channel = $channel; |
| 30 | + $this->cacheManager = $cacheManager; |
| 31 | + } |
| 32 | + |
| 33 | + public function map(ProductInterface $product): \Generator |
| 34 | + { |
| 35 | + yield [ |
| 36 | + 'sku' => $product->getCode(), |
| 37 | + 'name' => $product->getName(), |
| 38 | + 'tax' => $this->getTax($product), |
| 39 | + 'description' => $product->getDescription(), |
| 40 | + 'categoryId' => $this->getTaxon($product), |
| 41 | + 'images' => $this->getImages($product), |
| 42 | + 'variants' => $this->getVariants($product), |
| 43 | + 'features' => $this->getFeatures($product), |
| 44 | + // non standard fields |
| 45 | + 'allCategories' => $this->getTaxonomies($product), |
| 46 | + 'allCategoriesExpanded' => $this->getTaxonomiesExpanded($product), |
| 47 | + 'shortDescription' => $product->getShortDescription(), |
| 48 | + 'slug' => $product->getSlug(), |
| 49 | + 'url' => sprintf('https://digigames.pl/pl_PL/products/%s', $product->getSlug()), |
| 50 | + ]; |
| 51 | + } |
| 52 | + |
| 53 | + private function getTax(ProductInterface $product): int |
| 54 | + { |
| 55 | + return 23; |
| 56 | + } |
| 57 | + |
| 58 | + private function getTaxon(ProductInterface $product): string |
| 59 | + { |
| 60 | + foreach ($product->getTaxons() as $taxon) { |
| 61 | + if (!$taxon->getParent() instanceof TaxonInterface) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + if ($taxon->getParent()->getcode() === "genre") { |
| 65 | + return $taxon->getCode(); |
| 66 | + } |
| 67 | + } |
| 68 | + return $product->getMainTaxon()->getCode(); |
| 69 | + } |
| 70 | + |
| 71 | + private function getImages(ProductInterface $product): array |
| 72 | + { |
| 73 | + $cache = $this->cacheManager; |
| 74 | + return $product->getImages()->map(function (ProductImageInterface $image) use ($cache) { |
| 75 | + return $cache->getBrowserPath(parse_url($image->getPath(), PHP_URL_PATH), 'sylius_admin_product_original'); |
| 76 | + })->toArray(); |
| 77 | + } |
| 78 | + |
| 79 | + private function getVariants(ProductInterface $product): array |
| 80 | + { |
| 81 | + $return = []; |
| 82 | + foreach ($product->getVariants() as $variant) { |
| 83 | + $return[$variant->getId()] = [ |
| 84 | + 'full_name' => $variant->getName(), |
| 85 | + 'name' => $variant->getName(), |
| 86 | + 'price' => $this->getPrice($variant), |
| 87 | + 'quantity' => $variant->getOnHand() - $variant->getOnHold(), |
| 88 | + 'sku' => $variant->getCode(), |
| 89 | + ]; |
| 90 | + } |
| 91 | + return $return; |
| 92 | + } |
| 93 | + |
| 94 | + private function getFeatures(ProductInterface $product) |
| 95 | + { |
| 96 | + return $product->getAttributes()->map(function (AttributeValueInterface $attribute) { |
| 97 | + return [$attribute->getAttribute()->getName(), $attribute->getValue()]; |
| 98 | + })->toArray(); |
| 99 | + } |
| 100 | + |
| 101 | + private function getTaxonomies(ProductInterface $product): array |
| 102 | + { |
| 103 | + return $product->getTaxons()->map(function (TaxonInterface $taxon) { |
| 104 | + return $taxon->getName(); |
| 105 | + })->toArray(); |
| 106 | + } |
| 107 | + |
| 108 | + private function getTaxonomiesExpanded(ProductInterface $product): array |
| 109 | + { |
| 110 | + return $product->getTaxons()->map(function (TaxonInterface $taxon) { |
| 111 | + return $taxon->getFullname(); |
| 112 | + })->toArray(); |
| 113 | + } |
| 114 | + |
| 115 | + private function getPrice(ProductVariantInterface $productVariant): float |
| 116 | + { |
| 117 | + $pricing = $productVariant->getChannelPricingForChannel($this->channel); |
| 118 | + if (!$pricing instanceof ChannelPricingInterface) { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + return $pricing->getPrice() / 100; |
| 122 | + } |
| 123 | +} |
0 commit comments