1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2015 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Loader; |
18
|
|
|
|
19
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMediaInterface; |
20
|
|
|
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface; |
21
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Context\Context; |
22
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactoryInterface; |
23
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Loader\LoaderInterface; |
24
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class RenditionLoader. |
28
|
|
|
*/ |
29
|
|
|
class RenditionLoader implements LoaderInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Fallback rendition name. |
33
|
|
|
*/ |
34
|
|
|
const FALLBACK_NAME = 'original'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Context |
38
|
|
|
*/ |
39
|
|
|
protected $templateContext; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var MetaFactoryInterface |
43
|
|
|
*/ |
44
|
|
|
protected $metaFactory; |
45
|
|
|
|
46
|
109 |
|
public function __construct(Context $templateContext, MetaFactoryInterface $metaFactory) |
47
|
|
|
{ |
48
|
109 |
|
$this->templateContext = $templateContext; |
49
|
109 |
|
$this->metaFactory = $metaFactory; |
50
|
109 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function load($metaType, $parameters = [], $responseType = self::SINGLE) |
56
|
|
|
{ |
57
|
|
|
if ($responseType === LoaderInterface::SINGLE) { |
58
|
|
|
$renditionName = null; |
|
|
|
|
59
|
|
|
$fallbackRenditionName = self::FALLBACK_NAME; |
60
|
|
|
$fallbackRendition = null; |
61
|
|
|
if (!$this->templateContext['articleMedia'] instanceof Meta) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
/** @var ArticleMediaInterface $articleMedia */ |
65
|
|
|
$articleMedia = $this->templateContext['articleMedia']->getValues(); |
66
|
|
|
|
67
|
|
View Code Duplication |
if (array_key_exists('fallback', $parameters) && is_string($parameters['fallback'])) { |
|
|
|
|
68
|
|
|
$fallbackRenditionName = $parameters['fallback']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
if (array_key_exists('name', $parameters) && is_string($parameters['name'])) { |
|
|
|
|
72
|
|
|
$renditionName = $parameters['name']; |
73
|
|
|
} else { |
74
|
|
|
$renditionName = $fallbackRenditionName; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** @var ImageRenditionInterface $rendition */ |
78
|
|
|
foreach ($articleMedia->getRenditions() as $rendition) { |
79
|
|
|
if ($rendition->getName() === $renditionName) { |
80
|
|
|
return $this->metaFactory->create($rendition); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($rendition->getName() === $fallbackRenditionName) { |
84
|
|
|
$fallbackRendition = $rendition; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (null !== $fallbackRendition) { |
89
|
|
|
return $this->metaFactory->create($fallbackRendition); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
11 |
|
public function isSupported(string $type): bool |
100
|
|
|
{ |
101
|
11 |
|
return in_array($type, ['rendition']); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.