ImageTransformer::transform()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusElasticsearchPlugin\Transformer\Product;
12
13
use Liip\ImagineBundle\Service\FilterService;
14
use Sylius\Component\Core\Model\ImageInterface;
15
use Sylius\Component\Core\Model\ProductInterface;
16
17
final class ImageTransformer implements TransformerInterface
18
{
19
    private const SYLIUS_THUMBNAIL_TYPE = 'thumbnail';
20
21
    private const SYLIUS_THUMBNAIL_FILTER = 'sylius_shop_product_thumbnail';
22
23
    /** @var FilterService */
24
    private $imagineFilter;
25
26
    public function __construct(FilterService $imagineFilter)
27
    {
28
        $this->imagineFilter = $imagineFilter;
29
    }
30
31
    public function transform(ProductInterface $product): ?string
32
    {
33
        $productThumbnails = $product->getImagesByType(self::SYLIUS_THUMBNAIL_TYPE);
34
35
        if ($productThumbnails->isEmpty()) {
36
            return null;
37
        }
38
39
        /** @var ImageInterface $productImage */
40
        $productImage = $productThumbnails->first();
41
42
        return $this->imagineFilter->getUrlOfFilteredImage($productImage->getPath(), self::SYLIUS_THUMBNAIL_FILTER);
43
    }
44
}
45