ImgixUrlViewHelper::getUrlParameters()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
namespace Aoe\Imgix\ViewHelpers;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2018 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use Aoe\Imgix\TYPO3\Configuration;
29
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
30
31
class ImgixUrlViewHelper extends AbstractViewHelper
32
{
33
34
    /**
35
     * @var Configuration
36
     */
37
    private $configuration;
38
39
    /**
40
     * @param Configuration $configuration
41
     */
42 5
    public function __construct(Configuration $configuration)
43
    {
44 5
        $this->configuration = $configuration;
45 5
    }
46
47
    /**
48
     * @return void
49
     */
50
    public function initializeArguments()
51
    {
52
        parent::initializeArguments();
53
        $this->registerArgument('imageUrl', 'string', 'URL to image file', true);
54
        $this->registerArgument('urlParameters', 'array', 'API Parameters to be appended to URL', false);
55
    }
56
57
    /**
58
     * @return string
59
     */
60 5
    public function render()
61
    {
62 5
        if (false === $this->configuration->isEnabled()) {
63 1
            return $this->arguments['imageUrl'];
64
        }
65
66 4
        $url = '//' . $this->configuration->getHost() . '/' . $this->arguments['imageUrl'];
67
68 4
        if ($this->hasUrlParameters()) {
69 3
            $url .= '?' . http_build_query($this->getUrlParameters());
70
        }
71
72 4
        return $url;
73
    }
74
75
    /**
76
     * @return array
77
     */
78 4
    private function getUrlParameters()
79
    {
80 4
        $parameters = $this->configuration->getImgixDefaultUrlParameters();
81 4
        if ($this->hasArgument('urlParameters')) {
82 3
            $parameters = array_merge($parameters, $this->arguments['urlParameters']);
83
        }
84 4
        return $parameters;
85
    }
86
87
    /**
88
     * @return boolean
89
     */
90 4
    private function hasUrlParameters()
91
    {
92 4
        return sizeof($this->getUrlParameters()) > 0;
93
    }
94
}
95