Passed
Pull Request — master (#93)
by
unknown
03:48
created

VariantImagePathResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 13
dl 0
loc 25
rs 10
c 2
b 0
f 1
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 13 2
A __construct() 0 4 1
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\SyliusWishlistPlugin\Resolver;
12
13
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
14
use Sylius\Component\Core\Model\ProductVariantInterface;
15
16
final class VariantImagePathResolver implements VariantImagePathResolverInterface
17
{
18
    private CacheManager $cacheManager;
19
20
    private string $rootPath;
21
22
    public function __construct(CacheManager $cacheManager, string $rootPath)
23
    {
24
        $this->cacheManager = $cacheManager;
25
        $this->rootPath = $rootPath;
26
    }
27
28
    public function resolve(ProductVariantInterface $variant, string $baseUrl): string
29
    {
30
        if (false === $variant->getProduct()->getImages()->first()) {
0 ignored issues
show
Bug introduced by
The method getImages() does not exist on Sylius\Component\Product\Model\ProductInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Sylius\Component\Product\Model\Product. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        if (false === $variant->getProduct()->/** @scrutinizer ignore-call */ getImages()->first()) {
Loading history...
31
            return '';
32
        }
33
34
        $image = $variant->getProduct()->getImages()->first()->getPath();
35
        $filePath = sprintf('%s%s%s',$this->rootPath, self::IMAGE_PATH_IN_PROJECT, $image);
36
        $type = pathinfo($image, PATHINFO_EXTENSION);
37
        $data = file_get_contents($filePath);
38
        $dataUri = 'data:image/' . $type . ';base64,' . base64_encode($data);
39
40
        return $dataUri;
41
    }
42
}
43