Completed
Push — master ( 513898...595f7b )
by Łukasz
29:17
created

PlaceholderAliasGenerator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getVariation() 0 24 4
A setPlaceholderProvider() 0 5 1
A supportsValue() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Imagine;
10
11
use eZ\Publish\API\Repository\Values\Content\Field;
12
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
13
use eZ\Publish\Core\FieldType\Image\Value as ImageValue;
14
use eZ\Publish\Core\FieldType\Value;
15
use eZ\Publish\Core\IO\IOServiceInterface;
16
use eZ\Publish\SPI\Variation\VariationHandler;
17
use InvalidArgumentException;
18
use Liip\ImagineBundle\Exception\Imagine\Cache\Resolver\NotResolvableException;
19
use Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface;
20
21
class PlaceholderAliasGenerator implements VariationHandler
22
{
23
    /**
24
     * @var \eZ\Publish\SPI\Variation\VariationHandler
25
     */
26
    private $aliasGenerator;
27
28
    /**
29
     * @var \Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface
30
     */
31
    private $ioResolver;
32
33
    /**
34
     * @var \eZ\Publish\Core\IO\IOServiceInterface
35
     */
36
    private $ioService;
37
38
    /**
39
     * @var \eZ\Bundle\EzPublishCoreBundle\Imagine\PlaceholderProvider|null
40
     */
41
    private $placeholderProvider;
42
43
    /**
44
     * @var array
45
     */
46
    private $placeholderOptions = [];
47
48
    /**
49
     * PlaceholderAliasGenerator constructor.
50
     *
51
     * @param \eZ\Publish\SPI\Variation\VariationHandler $aliasGenerator
52
     * @param \Liip\ImagineBundle\Imagine\Cache\Resolver\ResolverInterface $ioResolver
53
     * @param \eZ\Publish\Core\IO\IOServiceInterface $ioService
54
     */
55
    public function __construct(
56
        VariationHandler $aliasGenerator,
57
        ResolverInterface $ioResolver,
58
        IOServiceInterface $ioService)
59
    {
60
        $this->aliasGenerator = $aliasGenerator;
61
        $this->ioResolver = $ioResolver;
62
        $this->ioService = $ioService;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getVariation(Field $field, VersionInfo $versionInfo, $variationName, array $parameters = [])
69
    {
70
        if ($this->placeholderProvider !== null) {
71
            /** @var \eZ\Publish\Core\FieldType\Image\Value $imageValue */
72
            $imageValue = $field->value;
73
            if (!$this->supportsValue($imageValue)) {
74
                throw new InvalidArgumentException("Value for field #{$field->id} ($field->fieldDefIdentifier) cannot be used for image placeholder generation.");
75
            }
76
77
            try {
78
                $this->ioResolver->resolve($imageValue->id, IORepositoryResolver::VARIATION_ORIGINAL);
79
            } catch (NotResolvableException $e) {
80
                // Generate placeholder for original image
81
                $binary = $this->ioService->newBinaryCreateStructFromLocalFile(
82
                    $this->placeholderProvider->getPlaceholder($imageValue, $this->placeholderOptions)
83
                );
84
                $binary->id = $imageValue->id;
85
86
                $this->ioService->createBinaryFile($binary);
87
            }
88
        }
89
90
        return $this->aliasGenerator->getVariation($field, $versionInfo, $variationName, $parameters);
91
    }
92
93
    public function setPlaceholderProvider(PlaceholderProvider $provider, array $options = [])
94
    {
95
        $this->placeholderProvider = $provider;
96
        $this->placeholderOptions = $options;
97
    }
98
99
    public function supportsValue(Value $value): bool
100
    {
101
        return $value instanceof ImageValue;
102
    }
103
}
104