Completed
Push — ezp-30882-thumbnail ( 1223cf...3134e5 )
by
unknown
15:29
created

ThumbnailChainStrategy::getThumbnail()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Repository\Strategy\ContentThumbnail;
10
11
use eZ\Publish\API\Repository\Values\Content\Thumbnail;
12
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
13
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy;
14
use Traversable;
15
16
final class ThumbnailChainStrategy implements ThumbnailStrategy
17
{
18
    protected $strategies = [];
19
20
    public function __construct(Traversable $strategies)
21
    {
22
        $this->setStrategies(iterator_to_array($strategies));
23
    }
24
25
    public function getThumbnail(ContentType $contentType, array $fields): ?Thumbnail
26
    {
27
        /** @var \eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy $strategy */
28
        foreach ($this->strategies as $strategy) {
29
            $thumbnail = $strategy->getThumbnail($contentType, $fields);
30
31
            if ($thumbnail !== null) {
32
                return $thumbnail;
33
            }
34
        }
35
36
        return null;
37
    }
38
39
    public function setStrategies(array $strategies): void
40
    {
41
        $this->strategies = $strategies;
42
    }
43
44
    public function addStrategy(ThumbnailStrategy $strategy): void
45
    {
46
        $this->strategies[] = $strategy;
47
    }
48
}
49