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

Asset::setHandle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 1
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 Asset
10
 * Value Object for providing details about a registrable asset
11
 *
12
 * @package EventEspresso\core\domain\values
13
 * @author  Brent Christensen
14
 * @since   $VID:$
15
 */
16
abstract class Asset
17
{
18
19
    /**
20
     * indicates the file extension for a build distribution CSS file
21
     */
22
    const FILE_EXTENSION_DISTRIBUTION_CSS = '.dist.css';
23
24
    /**
25
     * indicates the file extension for a build distribution JS file
26
     */
27
    const FILE_EXTENSION_DISTRIBUTION_JS = '.dist.js';
28
29
    /**
30
     * indicates a Cascading Style Sheet asset
31
     */
32
    const TYPE_CSS = 'css';
33
34
    /**
35
     * indicates a Javascript asset
36
     */
37
    const TYPE_JS = 'js';
38
39
    /**
40
     * indicates a Javascript manifest file
41
     */
42
    const TYPE_MANIFEST = 'manifest';
43
44
    /**
45
     * @var DomainInterface $domain
46
     */
47
    protected $domain;
48
49
    /**
50
     * @var string $type
51
     */
52
    private $type;
53
54
    /**
55
     * @var string $handle
56
     */
57
    private $handle;
58
59
    /**
60
     * @var bool $registered
61
     */
62
    private $registered = false;
63
64
65
    /**
66
     * Asset constructor.
67
     *
68
     * @param                 $type
69
     * @param string          $handle
70
     * @param DomainInterface $domain
71
     * @throws InvalidDataTypeException
72
     */
73
    public function __construct($type, $handle, DomainInterface $domain)
74
    {
75
        $this->domain = $domain;
76
        $this->setType($type);
77
        $this->setHandle($handle);
78
    }
79
80
81
    /**
82
     * @return array
83
     */
84
    public function validAssetTypes()
85
    {
86
        return array(
87
            Asset::TYPE_CSS,
88
            Asset::TYPE_JS,
89
            Asset::TYPE_MANIFEST,
90
        );
91
    }
92
93
94
    /**
95
     * @param string $type
96
     * @throws InvalidDataTypeException
97
     */
98
    private function setType($type)
99
    {
100
        if (! in_array($type, $this->validAssetTypes(), true)) {
101
            throw new InvalidDataTypeException(
102
                'Asset::$type',
103
                $type,
104
                'one of the TYPE_* class constants on \EventEspresso\core\domain\values\Asset is required'
105
            );
106
        }
107
        $this->type = $type;
108
    }
109
110
111
    /**
112
     * @param string $handle
113
     * @throws InvalidDataTypeException
114
     */
115
    private function setHandle($handle)
116
    {
117
        if (! is_string($handle)) {
118
            throw new InvalidDataTypeException(
119
                '$handle',
120
                $handle,
121
                'string'
122
            );
123
        }
124
        $this->handle = $handle;
125
    }
126
127
128
    /**
129
     * @return string
130
     */
131
    public function assetNamespace()
132
    {
133
        return $this->domain->assetNamespace();
134
    }
135
136
137
    /**
138
     * @return string
139
     */
140
    public function type()
141
    {
142
        return $this->type;
143
    }
144
145
146
    /**
147
     * @return string
148
     */
149
    public function handle()
150
    {
151
        return $this->handle;
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    public function isRegistered()
158
    {
159
        return $this->registered;
160
    }
161
162
    /**
163
     * @param bool $registered
164
     */
165
    public function setRegistered($registered = true)
166
    {
167
        $this->registered = filter_var($registered, FILTER_VALIDATE_BOOLEAN);
168
    }
169
}
170