LiipImagineThumbnail   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 0
loc 73
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B generatePublicUrl() 0 21 6
A __construct() 0 12 2
A generatePrivateUrl() 0 10 2
A generate() 0 4 1
A delete() 0 4 1
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
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class LiipImagineThumbnail implements ThumbnailInterface
25
{
26
    /**
27
     * @deprecated since sonata-project/media-bundle 3.3, will be removed in 4.0.
28
     *
29
     * @var RouterInterface
30
     */
31
    protected $router;
32
33
    /**
34
     * @var CacheManager
35
     */
36
    private $cacheManager;
37
38
    /**
39
     * @param RouterInterface|CacheManager $cacheManager
40
     */
41
    public function __construct($cacheManager)
42
    {
43
        if ($cacheManager instanceof RouterInterface) {
44
            @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...
45
                'Using an instance of %s is deprecated since version 3.3 and will be removed in 4.0. Use %s.',
46
                RouterInterface::class,
47
                CacheManager::class
48
            ), E_USER_DEPRECATED);
49
            $this->router = $cacheManager;
0 ignored issues
show
Deprecated Code introduced by
The property Sonata\MediaBundle\Thumb...agineThumbnail::$router has been deprecated with message: since sonata-project/media-bundle 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...
50
        }
51
        $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...
52
    }
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 sonata-project/media-bundle 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 sonata-project/media-bundle 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
    public function generatePrivateUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
77
    {
78
        if (MediaProviderInterface::FORMAT_REFERENCE !== $format) {
79
            throw new \RuntimeException('No private url for LiipImagineThumbnail');
80
        }
81
82
        $path = $provider->getReferenceImage($media);
83
84
        return $path;
85
    }
86
87
    public function generate(MediaProviderInterface $provider, MediaInterface $media): void
88
    {
89
        // nothing to generate, as generated on demand
90
    }
91
92
    public function delete(MediaProviderInterface $provider, MediaInterface $media, $formats = null): void
93
    {
94
        // feature not available
95
    }
96
}
97