ProxyMetadataBuilder::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 4
nc 4
nop 2
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 Gaufrette\Adapter\AmazonS3;
17
use Gaufrette\Adapter\AwsS3;
18
use Sonata\MediaBundle\Filesystem\Replicate;
19
use Sonata\MediaBundle\Model\MediaInterface;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
/**
23
 * @final since sonata-project/media-bundle 3.21.0
24
 */
25
class ProxyMetadataBuilder implements MetadataBuilderInterface
26
{
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private $container;
31
32
    /**
33
     * NEXT_MAJOR: remove the second parameter $map.
34
     *
35
     * @param array $map
36
     */
37
    public function __construct(ContainerInterface $container, ?array $map = null)
38
    {
39
        $this->container = $container;
40
41
        if (null !== $map) {
42
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
43
                'The "map" parameter is deprecated since sonata-project/media-bundle 2.4 and will be removed in 4.0.',
44
                E_USER_DEPRECATED
45
            );
46
        }
47
    }
48
49
    public function get(MediaInterface $media, $filename)
50
    {
51
        //get adapter for current media
52
        if (!$this->container->has($media->getProviderName())) {
53
            return [];
54
        }
55
56
        if ($meta = $this->getAmazonBuilder($media, $filename)) {
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getAmazonBuilder($media, $filename); of type array|boolean adds the type boolean to the return on line 57 which is incompatible with the return type declared by the interface Sonata\MediaBundle\Metad...taBuilderInterface::get of type array.
Loading history...
57
            return $meta;
58
        }
59
60
        if (!$this->container->has('sonata.media.metadata.noop')) {
61
            return [];
62
        }
63
64
        return $this->container->get('sonata.media.metadata.noop')->get($media, $filename);
65
    }
66
67
    /**
68
     * @param string $filename
69
     *
70
     * @return array|bool
71
     */
72
    protected function getAmazonBuilder(MediaInterface $media, $filename)
73
    {
74
        $adapter = $this->container->get($media->getProviderName())->getFilesystem()->getAdapter();
75
76
        //handle special Replicate adapter
77
        if ($adapter instanceof Replicate) {
78
            $adapterClassNames = $adapter->getAdapterClassNames();
79
        } else {
80
            $adapterClassNames = [\get_class($adapter)];
81
        }
82
83
        //for amazon s3
84
        if ((!\in_array(AmazonS3::class, $adapterClassNames, true) && !\in_array(AwsS3::class, $adapterClassNames, true)) || !$this->container->has('sonata.media.metadata.amazon')) {
85
            return false;
86
        }
87
88
        return $this->container->get('sonata.media.metadata.amazon')->get($media, $filename);
89
    }
90
}
91