Completed
Push — master ( 6c0aaa...a6043f )
by
unknown
22:11
created

LiipImagineThumbnail::generatePublicUrl()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 12
nc 5
nop 3
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\Thumbnail;
15
16
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
17
use Sonata\MediaBundle\Model\MediaInterface;
18
use Sonata\MediaBundle\Provider\MediaProviderInterface;
19
use Symfony\Component\Routing\RouterInterface;
20
21
class LiipImagineThumbnail implements ThumbnailInterface
22
{
23
    /**
24
     * @deprecated Since version 3.3, will be removed in 4.0.
25
     *
26
     * @var RouterInterface
27
     */
28
    protected $router;
29
30
    /**
31
     * @var CacheManager
32
     */
33
    private $cacheManager;
34
35
    /**
36
     * @param RouterInterface|CacheManager $cacheManager
37
     */
38
    public function __construct($cacheManager)
39
    {
40
        if ($cacheManager instanceof RouterInterface) {
41
            @trigger_error(sprintf(
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...
42
                'Using an instance of %s is deprecated since version 3.3 and will be removed in 4.0. Use %s.',
43
                RouterInterface::class,
44
                CacheManager::class
45
            ), E_USER_DEPRECATED);
46
            $this->router = $cacheManager;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Thumb...agineThumbnail::$router has been deprecated with message: Since version 3.3, will be removed in 4.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
47
        }
48
        $this->cacheManager = $cacheManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheManager can also be of type object<Symfony\Component\Routing\RouterInterface>. However, the property $cacheManager is declared as type object<Liip\ImagineBundl...ine\Cache\CacheManager>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function generatePublicUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
55
    {
56
        $path = $provider->getReferenceImage($media);
57
58
        if (MediaProviderInterface::FORMAT_ADMIN === $format || MediaProviderInterface::FORMAT_REFERENCE === $format) {
59
            return $path;
60
        }
61
        if ($this->router instanceof RouterInterface && !($this->cacheManager instanceof CacheManager)) {
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Thumb...agineThumbnail::$router has been deprecated with message: Since version 3.3, will be removed in 4.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
62
            $path = $this->router->generate(
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Thumb...agineThumbnail::$router has been deprecated with message: Since version 3.3, will be removed in 4.0.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
63
                sprintf('_imagine_%s', $format),
64
                ['path' => sprintf('%s/%s_%s.jpg', $provider->generatePath($media), $media->getId(), $format)]
65
            );
66
        }
67
68
        $path = $provider->getCdnPath($path, $media->getCdnIsFlushable());
69
        if ($this->cacheManager instanceof CacheManager) {
70
            $path = $this->cacheManager->getBrowserPath($path, $format);
71
        }
72
73
        return $path;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function generatePrivateUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
80
    {
81
        if (MediaProviderInterface::FORMAT_REFERENCE !== $format) {
82
            throw new \RuntimeException('No private url for LiipImagineThumbnail');
83
        }
84
85
        $path = $provider->getReferenceImage($media);
86
87
        return $path;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function generate(MediaProviderInterface $provider, MediaInterface $media): void
94
    {
95
        // nothing to generate, as generated on demand
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function delete(MediaProviderInterface $provider, MediaInterface $media, $formats = null): void
102
    {
103
        // feature not available
104
    }
105
}
106