Completed
Push — master ( 32420b...c3461f )
by MeWebStudio - Muharrem
30:10 queued 27:44
created

Purifier::getConfig()   C

Complexity

Conditions 10
Paths 216

Size

Total Lines 53
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 10.0222

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 25
c 1
b 0
f 0
nc 216
nop 1
dl 0
loc 53
ccs 31
cts 33
cp 0.9394
crap 10.0222
rs 5.7981

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mews\Purifier;
4
5
/**
6
 * Laravel 5 HTMLPurifier package
7
 *
8
 * @copyright Copyright (c) 2015 MeWebStudio
9
 * @version   2.0.0
10
 * @author    Muharrem ERİN
11
 * @contact [email protected]
12
 * @web http://www.mewebstudio.com
13
 * @date      2014-04-02
14
 * @license   MIT
15
 */
16
17
use Exception;
18
use HTMLPurifier;
19
use HTMLPurifier_Config;
20
use HTMLPurifier_HTMLDefinition;
21
use Illuminate\Contracts\Config\Repository;
22
use Illuminate\Filesystem\Filesystem;
23
24
class Purifier
25
{
26
27
    /**
28
     * @var Filesystem
29
     */
30
    protected $files;
31
32
    /**
33
     * @var Repository
34
     */
35
    protected $config;
36
37
    /**
38
     * @var HTMLPurifier
39
     */
40
    protected $purifier;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param Filesystem $files
46
     * @param Repository $config
47
     * @throws Exception
48
     */
49 27
    public function __construct(Filesystem $files, Repository $config)
50
    {
51 27
        $this->files = $files;
52 27
        $this->config = $config;
53
54 27
        $this->setUp();
55 22
    }
56
57
    /**
58
     * Setup
59
     *
60
     * @throws Exception
61
     */
62 27
    private function setUp()
63
    {
64 27
        if (!$this->config->has('purifier')) {
65 5
            throw new Exception('Configuration parameters not loaded!');
66
        }
67
68 22
        $this->checkCacheDirectory();
69
70
        // Create a new configuration object
71 22
        $config = $this->getConfig();
72
73
        // Create HTMLPurifier object
74 22
        $this->purifier = new HTMLPurifier($config);
75 22
    }
76
77
    /**
78
     * Add a custom definition
79
     *
80
     * @see http://htmlpurifier.org/docs/enduser-customize.html
81
     * @param array $definitionConfig
82
     * @param HTMLPurifier_Config $configObject Defaults to using default config
83
     *
84
     * @return HTMLPurifier_Config $configObject
85
     */
86 22
    private function addCustomDefinition(array $definitionConfig, HTMLPurifier_Config $configObject = null)
87
    {
88 22
        if (!$configObject) {
89
            $configObject = HTMLPurifier_Config::createDefault();
90
            $configObject->loadArray($this->getConfig());
0 ignored issues
show
Documentation introduced by
$this->getConfig() is of type object<HTMLPurifier_Config>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        }
92
93
        // Setup the custom definition
94 22
        $configObject->set('HTML.DefinitionID', $definitionConfig['id']);
95 22
        $configObject->set('HTML.DefinitionRev', $definitionConfig['rev']);
96
97
        // Enable debug mode
98 22
        if (!isset($definitionConfig['debug']) || $definitionConfig['debug']) {
99
            $configObject->set('Cache.DefinitionImpl', null);
100
        }
101
102
        // Start configuring the definition
103 22
        if ($def = $configObject->maybeGetRawHTMLDefinition()) {
104
            // Create the definition attributes
105 21
            if (!empty($definitionConfig['attributes'])) {
106 21
                $this->addCustomAttributes($definitionConfig['attributes'], $def);
107 21
            }
108
109
            // Create the definition elements
110 21
            if (!empty($definitionConfig['elements'])) {
111 21
                $this->addCustomElements($definitionConfig['elements'], $def);
112 21
            }
113 21
        }
114
115 22
        return $configObject;
116
    }
117
118
    /**
119
     * Add provided attributes to the provided definition
120
     *
121
     * @param array $attributes
122
     * @param HTMLPurifier_HTMLDefinition $definition
123
     *
124
     * @return HTMLPurifier_HTMLDefinition $definition
125
     */
126 21
    private function addCustomAttributes(array $attributes, HTMLPurifier_HTMLDefinition $definition)
127
    {
128 21
        foreach ($attributes as $attribute) {
129
            // Get configuration of attribute
130 21
            $required = !empty($attribute[3]) ? true : false;
131 21
            $onElement = $attribute[0];
132 21
            $attrName = $required ? $attribute[1] . '*' : $attribute[1];
133 21
            $validValues = $attribute[2];
134
135 21
            $definition->addAttribute($onElement, $attrName, $validValues);
136 21
        }
137
138 21
        return $definition;
139
    }
140
141
    /**
142
     * Add provided elements to the provided definition
143
     *
144
     * @param array $elements
145
     * @param HTMLPurifier_HTMLDefinition $definition
146
     *
147
     * @return HTMLPurifier_HTMLDefinition $definition
148
     */
149 21
    private function addCustomElements(array $elements, HTMLPurifier_HTMLDefinition $definition)
150
    {
151 21
        foreach ($elements as $element) {
152
            // Get configuration of element
153 21
            $name = $element[0];
154 21
            $contentSet = $element[1];
155 21
            $allowedChildren = $element[2];
156 21
            $attributeCollection = $element[3];
157 21
            $attributes = isset($element[4]) ? $element[4] : null;
158
159 21
            if (!empty($attributes)) {
160 21
                $definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection, $attributes);
161 21
            } else {
162 21
                $definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection);
163
            }
164 21
        }
165 21
    }
166
167
    /**
168
     * Check/Create cache directory
169
     */
170 22
    private function checkCacheDirectory()
171
    {
172 22
        $cachePath = $this->config->get('purifier.cachePath');
173
174 22
        if ($cachePath) {
175 22
            if (!$this->files->isDirectory($cachePath)) {
176 3
                $this->files->makeDirectory($cachePath, $this->config->get('purifier.cacheFileMode', 0755),true);
177 6
            }
178 22
        }
179 22
    }
180
181
    /**
182
     * @param null $config
183
     *
184
     * @return mixed|null
185
     */
186 22
    protected function getConfig($config = null)
187
    {
188
        // Create a new configuration object
189 22
        $configObject = HTMLPurifier_Config::createDefault();
190
191
        // Allow configuration to be modified
192 22
        if (! $this->config->get('purifier.finalize')) {
193
            $configObject->autoFinalize = false;
194
        }
195
196
        // Set default config
197 22
        $defaultConfig = [];
198 22
        $defaultConfig['Core.Encoding'] = $this->config->get('purifier.encoding');
199 22
        $defaultConfig['Cache.SerializerPath'] = $this->config->get('purifier.cachePath');
200 22
        $defaultConfig['Cache.SerializerPermissions'] = $this->config->get('purifier.cacheFileMode', 0755);
201
202 22
        if (! $config) {
203 22
            $config = $this->config->get('purifier.settings.default');
204 22
        } elseif (is_string($config)) {
205 6
            $config = $this->config->get('purifier.settings.' . $config);
206 6
        }
207
208 22
        if (! is_array($config)) {
209 3
            $config = [];
210 3
        }
211
212
        // Merge configurations
213 22
        $config = $defaultConfig + $config;
214
215
        // Load to Purifier config
216 22
        $configObject->loadArray($config);
217
218
        // Load custom definition if set
219 22
        if ($definitionConfig = $this->config->get('purifier.settings.custom_definition')) {
220 22
            $this->addCustomDefinition($definitionConfig, $configObject);
221 22
        }
222
223
        // Load custom elements if set
224 22
        if ($elements = $this->config->get('purifier.settings.custom_elements')) {
225 22
            if ($def = $configObject->maybeGetRawHTMLDefinition()) {
226 21
                $this->addCustomElements($elements, $def);
227 21
            }
228 22
        }
229
230
        // Load custom attributes if set
231 22
        if ($attributes = $this->config->get('purifier.settings.custom_attributes')) {
232 22
            if ($def = $configObject->maybeGetRawHTMLDefinition()) {
233 21
                $this->addCustomAttributes($attributes, $def);
234 21
            }
235 22
        }
236
237 22
        return $configObject;
238
    }
239
240
    /**
241
     * @param      $dirty
242
     * @param null $config
243
     *
244
     * @return mixed
245
     */
246 9
    public function clean($dirty, $config = null)
247
    {
248 9
        if (is_array($dirty)) {
249 3
            return array_map(function ($item) use ($config) {
250 3
                return $this->clean($item, $config);
251 3
            }, $dirty);
252
        }
253
254 9
        return $this->purifier->purify($dirty, $config ? $this->getConfig($config) : null);
255
    }
256
257
    /**
258
     * Get HTMLPurifier instance.
259
     *
260
     * @return \HTMLPurifier
261
     */
262 3
    public function getInstance()
263
    {
264 3
        return $this->purifier;
265
    }
266
}
267