Completed
Push — ezp-30882-thumbnail ( 274ed9 )
by
unknown
19:23
created

ThumbnailChainStrategy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace eZ\Publish\Core\Repository\Strategy\ContentThumbnail;
4
5
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
6
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy;
7
8
class ThumbnailChainStrategy implements ThumbnailStrategy
9
{
10
    protected $strategies = [];
11
12
    public function __construct(array $strategies)
13
    {
14
        $this->setStrategies($strategies);
15
    }
16
17
    public function getThumbnail(ContentType $contentType, array $fields): ?string
18
    {
19
        foreach ($this->strategies as $priority => $strategies) {
20
            $strategies = (array)$strategies;
21
22
            /** @var \eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\ThumbnailStrategy $strategy */
23
            foreach ($strategies as $strategy) {
24
                $thumbnail = $strategy->getThumbnail($contentType, $fields);
25
26
                if ($thumbnail !== null) {
27
                    return $thumbnail;
28
                }
29
            }
30
        }
31
    }
32
33
    public function setStrategies(array $strategies): void
34
    {
35
        $this->strategies = [];
36
37
        foreach ($strategies as $priority => $strategy) {
38
            $this->addStrategy($strategy, $priority);
39
        }
40
    }
41
42
    public function addStrategy(ThumbnailStrategy $strategy, int $priority = 0): void
43
    {
44
        $this->strategies[$priority][] = $strategy;
45
46
        ksort($this->strategies);
47
    }
48
}