Completed
Push — master ( 51b9ae...79147b )
by Craig
10:12
created

PageAssetApi::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 11
rs 9.4285
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\ThemeModule\Api;
13
14
use Zikula\ThemeModule\Api\ApiInterface\PageAssetApiInterface;
15
use Zikula\ThemeModule\Engine\AssetBag;
16
17
class PageAssetApi implements PageAssetApiInterface
18
{
19
    /**
20
     * @var AssetBag
21
     */
22
    private $styleSheets;
23
24
    /**
25
     * @var AssetBag
26
     */
27
    private $scripts;
28
29
    /**
30
     * @var AssetBag
31
     */
32
    private $headers;
33
34
    /**
35
     * @var AssetBag
36
     */
37
    private $footers;
38
39
    /**
40
     * constructor.
41
     * @param AssetBag $styleSheets
42
     * @param AssetBag $scripts
43
     * @param AssetBag $headers
44
     * @param AssetBag $footers
45
     */
46
    public function __construct(
47
        AssetBag $styleSheets,
48
        AssetBag $scripts,
49
        AssetBag $headers,
50
        AssetBag $footers
51
    ) {
52
        $this->styleSheets = $styleSheets;
53
        $this->scripts = $scripts;
54
        $this->headers = $headers;
55
        $this->footers = $footers;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function add($type, $value, $weight = AssetBag::WEIGHT_DEFAULT)
62
    {
63
        if (empty($type) || empty($value)) {
64
            throw new \InvalidArgumentException();
65
        }
66
        if (!in_array($type, ['stylesheet', 'javascript', 'header', 'footer']) || !is_numeric($weight)) {
67
            throw new \InvalidArgumentException();
68
        }
69
70
        // ensure proper variable types
71
        $value = (string) $value;
72
        $type = (string) $type;
73
        $weight = (int) $weight;
74
75
        if ('stylesheet' == $type) {
76
            $this->styleSheets->add([$value => $weight]);
77
        } elseif ('javascript' == $type) {
78
            $this->scripts->add([$value => $weight]);
79
        } elseif ('header' == $type) {
80
            $this->headers->add([$value => $weight]);
81
        } elseif ('footer' == $type) {
82
            $this->footers->add([$value => $weight]);
83
        }
84
    }
85
}
86