Completed
Pull Request — master (#835)
by Paweł
11:45
created

AssetLocationResolver::getMediaBasePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2019 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2019 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Resolver;
16
17
use Aws\S3\S3Client;
18
use SWP\Bundle\ContentBundle\Model\FileInterface;
19
20
class AssetLocationResolver implements AssetLocationResolverInterface
21
{
22
    private $mainAdapter;
23
24
    private $awsBucket;
25
26
    private $awsClient;
27
28
    private $localDirectory;
29
30
    public function __construct(string $mainAdapter, string $awsBucket, S3Client $awsClient, string $localDirectory)
31
    {
32
        $this->mainAdapter = $mainAdapter;
33
        $this->awsBucket = $awsBucket;
34
        $this->localDirectory = $localDirectory;
35
        $this->awsClient = $awsClient;
36
    }
37
38
    public function getAssetUrl(FileInterface $file): string
39
    {
40
        if ('aws_adapter' === $this->mainAdapter) {
41
            return $this->getAwsUrl($file);
42
        }
43
44
        if ('local_adapter' === $this->mainAdapter) {
45
            return $this->getLocalUrl($file);
46
        }
47
48
        return '';
49
    }
50
51
    public function getMediaBasePath(): string
52
    {
53
        return 'swp/media';
54
    }
55
56
    private function getAwsUrl(FileInterface $file): string
57
    {
58
        return $this->awsClient->getObjectUrl($this->awsBucket, $file->getAssetId().'.'.$file->getFileExtension());
59
    }
60
61
    private function getLocalUrl(FileInterface $file): string
62
    {
63
        return  $this->localDirectory.DIRECTORY_SEPARATOR.$this->getMediaBasePath().DIRECTORY_SEPARATOR.$file->getAssetId().'.'.$file->getFileExtension();
64
    }
65
}
66