File size: 1,822 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

declare(strict_types=1);

namespace Shared\Infrastructure\FileSystem;

use Easy\Container\Attributes\Inject;
use League\Flysystem\Visibility;
use Override;
use Shared\Infrastructure\FileSystem\Adapters\CdnAdapterInterface;
use Shared\Infrastructure\FileSystem\Adapters\LocalCdnAdapter;
use Shared\Infrastructure\FileSystem\Exceptions\AdapterNotFoundException;

class Cdn extends FileSystem implements CdnInterface
{
    private CdnAdapterInterface $adapter;

    public function __construct(
        private CdnAdapterCollectionInterface $collection,

        #[Inject('option.cdn.adapter')]
        private ?string $adapterLookupKey = LocalCdnAdapter::LOOKUP_KEY
    ) {
        if (is_null($this->adapterLookupKey)) {
            $this->adapterLookupKey = LocalCdnAdapter::LOOKUP_KEY;
        }

        try {
            $this->adapter = $this->collection->get($this->adapterLookupKey);
        } catch (AdapterNotFoundException $th) {
            if ($adapterLookupKey == LocalCdnAdapter::LOOKUP_KEY) {
                throw $th;
            }

            $this->adapter = $this->collection->get(LocalCdnAdapter::LOOKUP_KEY);
            $this->adapterLookupKey = LocalCdnAdapter::LOOKUP_KEY;
        }

        parent::__construct($this->adapter);
    }

    #[Override]
    public function getUrl(string $path): ?string
    {
        return $this->adapter->getUrl($path);
    }

    #[Override]
    public function getAdapterLookupKey(): string
    {
        return $this->adapterLookupKey;
    }

    #[Override]
    public function write(
        string $location,
        string $contents,
        array $config = []
    ): void {
        if (!isset($config['visibility'])) {
            $config['visibility'] = Visibility::PUBLIC;
        }

        parent::write($location, $contents, $config);
    }
}