Test Setup Failed
Push — main ( 7bb2c5...c5b0e8 )
by Daniel
03:55
created

ArtContentRetriever::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Component\Art;
6
7
use Uxmp\Core\Component\Config\ConfigProviderInterface;
8
9
/**
10
 * Retrieves the binary data of an art item
11
 */
12
final class ArtContentRetriever implements ArtContentRetrieverInterface
13
{
14
    public function __construct(
15
        private ConfigProviderInterface $config,
16
    ) {
17
    }
18
19
    /**
20
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
21
     *  mimeType: string,
22
     *  content: string
23
     * }
24
     *
25
     * @throws Exception\ArtContentException
26
     */
27
    public function retrieve(
28
        CachableArtItemInterface $item,
29
    ): array {
30
        $artItemId = $item->getArtItemId();
31
32
        if ($artItemId === null) {
33
            throw new Exception\ArtContentException('item does not provide art');
34
        }
35
        $path = sprintf(
36
            '%s/img/%s/%s.jpg',
37
            $this->config->getAssetPath(),
38
            $item->getArtItemType(),
39
            $artItemId
40
        );
41
42
        // @todo support other mimetypes
43
        $mimeType = 'image/jpg';
44
45
        if (!file_exists($path)) {
46
            throw new Exception\ArtContentException(
47
                sprintf('File `%s` not found', $path)
48
            );
49
        }
50
51
        return [
52
            'mimeType' => $mimeType,
53
            'content' => (string) file_get_contents($path),
54
        ];
55
    }
56
}
57