Completed
Branch GDPR/user-data-export (1838dd)
by
unknown
53:17 queued 40:13
created

BrowserAsset::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 5
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\values\assets;
4
5
use EventEspresso\core\domain\DomainInterface;
6
use EventEspresso\core\exceptions\InvalidDataTypeException;
7
8
/**
9
 * Class BrowserAsset
10
 * An asset that gets sent to the browser such as a Javascript or CSS
11
 *
12
 * @package EventEspresso\core\domain\values\assets
13
 * @author  Brent Christensen
14
 * @since   $VID:$
15
 */
16
abstract class BrowserAsset extends Asset
17
{
18
19
    /**
20
     * @var string $source
21
     */
22
    private $source;
23
24
    /**
25
     * @var array $dependencies
26
     */
27
    private $dependencies;
28
29
    /**
30
     * @var string $version
31
     */
32
    private $version;
33
34
35
    /**
36
     * Asset constructor.
37
     *
38
     * @param string          $type
39
     * @param string          $handle
40
     * @param string          $source
41
     * @param array           $dependencies
42
     * @param DomainInterface $domain
43
     * @throws InvalidDataTypeException
44
     */
45
    public function __construct($type, $handle, $source, array $dependencies, DomainInterface $domain)
46
    {
47
        parent::__construct($type, $handle, $domain);
48
        $this->setSource($source);
49
        $this->setDependencies($dependencies);
50
    }
51
52
53
    /**
54
     * @since $VID:$
55
     */
56
    abstract public function enqueueAsset();
57
58
59
    /**
60
     * @return array
61
     */
62
    public function dependencies()
63
    {
64
        return $this->dependencies;
65
    }
66
67
68
    /**
69
     * @param array $dependencies
70
     */
71
    private function setDependencies(array $dependencies)
72
    {
73
        $this->dependencies = $dependencies;
74
    }
75
76
77
    /**
78
     * @since $VID:$
79
     * @return bool
80
     */
81
    public function hasDependencies()
82
    {
83
        return count($this->dependencies) > 0;
84
    }
85
86
87
    /**
88
     * @return string
89
     */
90
    public function source()
91
    {
92
        return $this->source;
93
    }
94
95
96
    /**
97
     * @param string $source
98
     * @throws InvalidDataTypeException
99
     */
100
    private function setSource($source)
101
    {
102
        if (! is_string($source)) {
103
            throw new InvalidDataTypeException(
104
                '$source',
105
                $source,
106
                'string'
107
            );
108
        }
109
        $this->source = $source;
110
    }
111
112
113
    /**
114
     * @return string
115
     * @throws InvalidDataTypeException
116
     */
117
    public function version()
118
    {
119
        // if version is NOT set and this asset was NOT built for distribution,
120
        // then set the version equal to the EE core plugin version
121
        if (
122
            $this->version === null
123
            && (
124
                substr($this->source, -8) !== Asset::FILE_EXTENSION_DISTRIBUTION_JS
125
                || substr($this->source, -9) !== Asset::FILE_EXTENSION_DISTRIBUTION_CSS
126
            )
127
        ) {
128
            $this->setVersion();
129
        }
130
        return $this->version;
131
    }
132
133
134
    /**
135
     * @param string $version
136
     * @return BrowserAsset
137
     * @throws InvalidDataTypeException
138
     */
139
    public function setVersion($version = EVENT_ESPRESSO_VERSION)
140
    {
141
        if (! is_string($version)) {
142
            throw new InvalidDataTypeException(
143
                '$version',
144
                $version,
145
                'string'
146
            );
147
        }
148
        $this->version = $version;
149
        return $this;
150
    }
151
}
152