Completed
Push — master ( 59d6ff...569ee7 )
by
unknown
04:23
created

ImgixUrlViewHelper::getUrlParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
namespace Aoe\Imgix\ViewHelpers;
3
4
use Aoe\Imgix\TYPO3\Configuration;
5
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
6
7
class ImgixUrlViewHelper extends AbstractViewHelper
8
{
9
10
    /**
11
     * @var Configuration
12
     */
13
    private $configuration;
14
15
    /**
16
     * @param Configuration $configuration
17
     */
18 5
    public function __construct(Configuration $configuration)
19
    {
20 5
        $this->configuration = $configuration;
21 5
    }
22
23
    /**
24
     * @return void
25
     */
26
    public function initializeArguments()
27
    {
28
        parent::initializeArguments();
29
        $this->registerArgument('imageUrl', 'string', 'URL to image file', true);
30
        $this->registerArgument('urlParameters', 'array', 'API Parameters to be appended to URL', false);
31
    }
32
33
    /**
34
     * @return string
35
     */
36 5
    public function render()
37
    {
38 5
        if (false === $this->configuration->isEnabled()) {
39 1
            return $this->arguments['imageUrl'];
40
        }
41
42 4
        $url = '//' . $this->configuration->getHost() . '/' . $this->arguments['imageUrl'];
43
44 4
        if ($this->hasUrlParameters()) {
45 3
            $url .= '?' . http_build_query($this->getUrlParameters());
46 3
        }
47
48 4
        return $url;
49
    }
50
51
    /**
52
     * @return array
53
     */
54 4
    private function getUrlParameters()
55
    {
56 4
        $parameters = $this->configuration->getImgixDefaultUrlParameters();
57 4
        if ($this->hasArgument('urlParameters')) {
58 3
            $parameters = array_merge($parameters, $this->arguments['urlParameters']);
59 3
        }
60 4
        return $parameters;
61
    }
62
63
    /**
64
     * @return boolean
65
     */
66 4
    private function hasUrlParameters()
67
    {
68 4
        return sizeof($this->getUrlParameters()) > 0;
69
    }
70
}
71