AmazonMetadataBuilder::getDefaultMetadata()   C
last analyzed

Complexity

Conditions 13
Paths 96

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 6.6166
c 0
b 0
f 0
cc 13
nc 96
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Metadata;
15
16
use GuzzleHttp\Psr7;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
19
/**
20
 * @final since sonata-project/media-bundle 3.21.0
21
 */
22
class AmazonMetadataBuilder implements MetadataBuilderInterface
23
{
24
    public const PRIVATE_ACCESS = 'private';
25
    public const PUBLIC_READ = 'public-read';
26
    public const PUBLIC_READ_WRITE = 'public-read-write';
27
    public const AUTHENTICATED_READ = 'authenticated-read';
28
    public const BUCKET_OWNER_READ = 'bucket-owner-read';
29
    public const BUCKET_OWNER_FULL_CONTROL = 'bucket-owner-full-control';
30
31
    public const STORAGE_STANDARD = 'STANDARD';
32
    public const STORAGE_REDUCED = 'REDUCED_REDUNDANCY';
33
    public const STORAGE_GLACIER = 'GLACIER';
34
35
    /**
36
     * @var array
37
     */
38
    protected $settings;
39
40
    /**
41
     * @var string[]
42
     */
43
    protected $acl = [
44
        'private' => self::PRIVATE_ACCESS,
45
        'public' => self::PUBLIC_READ,
46
        'open' => self::PUBLIC_READ_WRITE,
47
        'auth_read' => self::AUTHENTICATED_READ,
48
        'owner_read' => self::BUCKET_OWNER_READ,
49
        'owner_full_control' => self::BUCKET_OWNER_FULL_CONTROL,
50
    ];
51
52
    public function __construct(array $settings)
53
    {
54
        $this->settings = $settings;
55
    }
56
57
    public function get(MediaInterface $media, $filename)
58
    {
59
        return array_replace_recursive(
60
            $this->getDefaultMetadata(),
61
            $this->getContentType($filename)
62
        );
63
    }
64
65
    /**
66
     * Get data passed from the config.
67
     *
68
     * @return array
69
     */
70
    protected function getDefaultMetadata()
71
    {
72
        //merge acl
73
        $output = [];
74
        if (isset($this->settings['acl']) && !empty($this->settings['acl'])) {
75
            $output['ACL'] = $this->acl[$this->settings['acl']];
76
        }
77
78
        //merge storage
79
        if (isset($this->settings['storage'])) {
80
            if ('standard' === $this->settings['storage']) {
81
                $output['storage'] = static::STORAGE_STANDARD;
82
            } elseif ('reduced' === $this->settings['storage']) {
83
                $output['storage'] = static::STORAGE_REDUCED;
84
            }
85
        }
86
87
        //merge meta
88
        if (isset($this->settings['meta']) && !empty($this->settings['meta'])) {
89
            $output['meta'] = $this->settings['meta'];
90
        }
91
92
        //merge cache control header
93
        if (isset($this->settings['cache_control']) && !empty($this->settings['cache_control'])) {
94
            $output['CacheControl'] = $this->settings['cache_control'];
95
        }
96
97
        //merge encryption
98
        if (isset($this->settings['encryption']) && !empty($this->settings['encryption'])) {
99
            if ('aes256' === $this->settings['encryption']) {
100
                $output['encryption'] = 'AES256';
101
            }
102
        }
103
104
        return $output;
105
    }
106
107
    /**
108
     * Gets the correct content-type.
109
     *
110
     * @param string $filename
111
     *
112
     * @return array
113
     */
114
    protected function getContentType($filename)
115
    {
116
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
117
        $contentType = Psr7\mimetype_from_extension($extension);
118
119
        return ['contentType' => $contentType];
120
    }
121
}
122