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

ContentFieldStrategy::__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
/**
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\Field;
10
11
use eZ\Publish\API\Repository\Values\Content\Field;
12
use eZ\Publish\API\Repository\Values\Content\Thumbnail;
13
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
14
use eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\ThumbnailStrategy;
15
16
final class ContentFieldStrategy implements ThumbnailStrategy
17
{
18
    /** @var \eZ\Publish\SPI\Repository\Strategy\ContentThumbnail\Field\ThumbnailStrategy[] */
19
    protected $strategies = [];
20
21
    public function __construct(array $strategies = [])
22
    {
23
        $this->setStrategies($strategies);
24
    }
25
26
    /**
27
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
28
     */
29
    public function getThumbnail(Field $field): ?Thumbnail
30
    {
31
        if (!$this->hasStrategy($field->fieldTypeIdentifier)) {
32
            throw new NotFoundException('Field\ThumbnailStrategy', $field->fieldTypeIdentifier);
33
        }
34
35
        return $this->strategies[$field->fieldTypeIdentifier]->getThumbnail($field);
36
    }
37
38
    public function hasStrategy(string $fieldTypeIdentifier): bool
39
    {
40
        return isset($this->strategies[$fieldTypeIdentifier]);
41
    }
42
43
    public function addStrategy(string $fieldTypeIdentifier, ThumbnailStrategy $thumbnailStrategy): void
44
    {
45
        $this->strategies[$fieldTypeIdentifier] = $thumbnailStrategy;
46
    }
47
48
    public function setStrategies(array $thumbnailStrategies): void
49
    {
50
        foreach ($thumbnailStrategies as $fieldTypeIdentifier => $thumbnailStrategy) {
51
            $this->addStrategy($fieldTypeIdentifier, $thumbnailStrategy);
52
        }
53
    }
54
}
55