Completed
Pull Request — master (#82)
by
unknown
13:39
created

Purifier::getConfig()   C

Complexity

Conditions 10
Paths 216

Size

Total Lines 53
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.2918

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 24
cts 28
cp 0.8571
crap 10.2918
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 24
     */
49
    public function __construct(Filesystem $files, Repository $config)
50 24
    {
51 24
        $this->files = $files;
52
        $this->config = $config;
53 24
54 19
        $this->setUp();
55
    }
56
57
    /**
58
     * Setup
59
     *
60
     * @throws Exception
61 24
     */
62
    private function setUp()
63 24
    {
64 5
        if (!$this->config->has('purifier')) {
65
            throw new Exception('Configuration parameters not loaded!');
66
        }
67 19
68
        $this->checkCacheDirectory();
69
70 19
        // Create a new configuration object
71
        $config = $this->getConfig();
72
73 19
        // Create HTMLPurifier object
74
        $this->purifier = new HTMLPurifier($config);
75
    }
76
77 19
    /**
78
     * Add a custom definition
79
     *
80 19
     * @see http://htmlpurifier.org/docs/enduser-customize.html
81 19
     * @param array $definitionConfig
82 19
     * @param HTMLPurifier_Config $configObject Defaults to using default config
83
     *
84
     * @return HTMLPurifier_Config $configObject
85 19
     */
86 19
    private function addCustomDefinition(array $definitionConfig, HTMLPurifier_Config $configObject = null)
87 19
    {
88 19
        if (!$configObject) {
89 19
            $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 19
93 19
        // Setup the custom definition
94 19
        $configObject->set('HTML.DefinitionID', $definitionConfig['id']);
95 19
        $configObject->set('HTML.DefinitionRev', $definitionConfig['rev']);
96 19
97
        // Enable debug mode
98
        if (!isset($definitionConfig['debug']) || $definitionConfig['debug']) {
99 19
            $configObject->set('Cache.DefinitionImpl', null);
100 19
        }
101
102
        // Start configuring the definition
103
        if ($def = $configObject->maybeGetRawHTMLDefinition()) {
104
            // Create the definition attributes
105
            if (!empty($definitionConfig['attributes'])) {
106
                $this->addCustomAttributes($definitionConfig['attributes'], $def);
107
            }
108
109
            // Create the definition elements
110
            if (!empty($definitionConfig['elements'])) {
111 19
                $this->addCustomElements($definitionConfig['elements'], $def);
112
            }
113 19
        }
114
115
        return $configObject;
116
    }
117
118
    /**
119 19
     * Add provided attributes to the provided definition
120 19
     *
121
     * @param array $attributes
122
     * @param HTMLPurifier_HTMLDefinition $definition
123 19
     *
124
     * @return HTMLPurifier_HTMLDefinition $definition
125
     */
126
    private function addCustomAttributes(array $attributes, HTMLPurifier_HTMLDefinition $definition)
127
    {
128 19
        foreach ($attributes as $attribute) {
129
            // Get configuration of attribute
130 19
            $required = !empty($attribute[3]) ? true : false;
131 19
            $onElement = $attribute[0];
132 19
            $attrName = $required ? $attribute[1] . '*' : $attribute[1];
133
            $validValues = $attribute[2];
134
135 19
            $definition->addAttribute($onElement, $attrName, $validValues);
136 19
        }
137 19
138 19
        return $definition;
139
    }
140 19
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
    private function addCustomElements(array $elements, HTMLPurifier_HTMLDefinition $definition)
150
    {
151 19
        foreach ($elements as $element) {
152
            // Get configuration of element
153 19
            $name = $element[0];
154
            $contentSet = $element[1];
155 19
            $allowedChildren = $element[2];
156 19
            $attributeCollection = $element[3];
157 19
            $attributes = isset($element[4]) ? $element[4] : null;
158 19
159
            if (!empty($attributes)) {
160 19
                $definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection, $attributes);
161 19
            } else {
162
                $definition->addElement($name, $contentSet, $allowedChildren, $attributeCollection);
163 19
            }
164
        }
165
    }
166
167
    /**
168
     * Check/Create cache directory
169
     */
170
    private function checkCacheDirectory()
171
    {
172
        $cachePath = $this->config->get('purifier.cachePath');
173
174 19
        if ($cachePath) {
175
            if (!$this->files->isDirectory($cachePath)) {
176 19
                $this->files->makeDirectory($cachePath, $this->config->get('purifier.cacheFileMode', 0755));
177
            }
178 19
        }
179 19
    }
180 19
181 19
    /**
182 19
     * @param null $config
183
     *
184 19
     * @return mixed|null
185 19
     */
186 19
    protected function getConfig($config = null)
187 19
    {
188
        // Create a new configuration object
189 19
        $configObject = HTMLPurifier_Config::createDefault();
190 19
191
        // Allow configuration to be modified
192
        if (! $this->config->get('purifier.finalize')) {
193
            $configObject->autoFinalize = false;
194
        }
195 19
196
        // Set default config
197 19
        $defaultConfig = [];
198
        $defaultConfig['Core.Encoding'] = $this->config->get('purifier.encoding');
199 19
        $defaultConfig['Cache.SerializerPath'] = $this->config->get('purifier.cachePath');
200 19
        $defaultConfig['Cache.SerializerPermissions'] = $this->config->get('purifier.cacheFileMode', 0755);
201 3
202 3
        if (! $config) {
203 19
            $config = $this->config->get('purifier.settings.default');
204 19
        } elseif (is_string($config)) {
205
            $config = $this->config->get('purifier.settings.' . $config);
206
        }
207
208
        if (! is_array($config)) {
209
            $config = [];
210
        }
211 19
212
        // Merge configurations
213 19
        $config = $defaultConfig + $config;
214
215
        // Load to Purifier config
216
        $configObject->loadArray($config);
217
218
        // Load custom definition if set
219
        if ($definitionConfig = $this->config->get('purifier.settings.custom_definition')) {
220
            $this->addCustomDefinition($definitionConfig, $configObject);
221 19
        }
222
223 19
        // Load custom elements if set
224 19
        if ($elements = $this->config->get('purifier.settings.custom_elements')) {
225 19
            if ($def = $configObject->maybeGetRawHTMLDefinition()) {
226 19
                $this->addCustomElements($elements, $def);
227
            }
228 19
        }
229 19
230 19
        // Load custom attributes if set
231
        if ($attributes = $this->config->get('purifier.settings.custom_attributes')) {
232
            if ($def = $configObject->maybeGetRawHTMLDefinition()) {
233
                $this->addCustomAttributes($attributes, $def);
234 19
            }
235
        }
236
237
        return $configObject;
238 19
    }
239
240 19
    /**
241
     * @param      $dirty
242
     * @param null $config
243
     *
244
     * @return mixed
245
     */
246
    public function clean($dirty, $config = null)
247
    {
248
        if (is_array($dirty)) {
249 3
            return array_map(function ($item) use ($config) {
250
                return $this->clean($item, $config);
251 3
            }, $dirty);
252 3
        }
253 3
254 3
        return $this->purifier->purify($dirty, $config ? $this->getConfig($config) : null);
255
    }
256
257 3
    /**
258
     * Get HTMLPurifier instance.
259
     *
260
     * @return \HTMLPurifier
261
     */
262
    public function getInstance()
263
    {
264
        return $this->purifier;
265 3
    }
266
}
267