Completed
Pull Request — master (#54)
by Konrad
07:28 queued 03:14
created

ImageTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A transform() 0 12 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\Transformer\Product;
14
15
use Liip\ImagineBundle\Service\FilterService;
16
use Sylius\Component\Core\Model\ImageInterface;
17
use Sylius\Component\Core\Model\ProductInterface;
18
19
final class ImageTransformer implements TransformerInterface
20
{
21
    private const SYLIUS_THUMBNAIL_TYPE = 'thumbnail';
22
    private const SYLIUS_THUMBNAIL_FILTER = 'sylius_shop_product_thumbnail';
23
24
    /** @var FilterService */
25
    private $imagineFilter;
26
27
    public function __construct(FilterService $imagineFilter)
28
    {
29
        $this->imagineFilter = $imagineFilter;
30
    }
31
32
    public function transform(ProductInterface $product): ?string
33
    {
34
        $productThumbnails = $product->getImagesByType(self::SYLIUS_THUMBNAIL_TYPE);
35
36
        if ($productThumbnails->isEmpty()) {
37
            return null;
38
        }
39
40
        /** @var ImageInterface $productImage */
41
        $productImage = $productThumbnails->first();
42
43
        return $this->imagineFilter->getUrlOfFilteredImage($productImage->getPath(), self::SYLIUS_THUMBNAIL_FILTER);
44
    }
45
}
46