Completed
Pull Request — master (#39)
by
unknown
02:10
created

Purifier::getInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Mews\Purifier;
2
3
/**
4
 * Laravel 5 HTMLPurifier package
5
 *
6
 * @copyright Copyright (c) 2015 MeWebStudio
7
 * @version   2.0.0
8
 * @author    Muharrem ERİN
9
 * @contact [email protected]
10
 * @web http://www.mewebstudio.com
11
 * @date      2014-04-02
12
 * @license   MIT
13
 */
14
15
use Exception;
16
use HTMLPurifier;
17
use HTMLPurifier_Config;
18
use Illuminate\Config\Repository;
19
use Illuminate\Filesystem\Filesystem;
20
21
class Purifier
22
{
23
24
    /**
25
     * @var Filesystem
26
     */
27
    protected $files;
28
29
    /**
30
     * @var Repository
31
     */
32
    protected $config;
33
34
    /**
35
     * @var HTMLPurifier
36
     */
37
    protected $purifier;
38
39
    /**
40
     * Constructor
41
     *
42
     * @param Filesystem $files
43
     * @param Repository $config
44
     * @throws Exception
45
     */
46
    public function __construct(Filesystem $files, Repository $config)
47
    {
48
        $this->files  = $files;
49
        $this->config = $config;
50
51
        $this->setUp();
52
    }
53
54
    /**
55
     * Setup
56
     *
57
     * @throws Exception
58
     */
59
    private function setUp()
60
    {
61
        if (!$this->config->has('purifier')) {
62
            if (!$this->config->has('mews.purifier')) {
63
                throw new Exception('Configuration parameters not loaded!');
64
            }
65
            $this->config->set('purifier', $this->config->get('mews.purifier'));
66
        }
67
68
        $this->checkCacheDirectory();
69
70
        // Create a new configuration object
71
        $config = HTMLPurifier_Config::createDefault();
72
73
        // Allow configuration to be modified
74
        if (!$this->config->get('purifier.finalize')) {
75
            $config->autoFinalize = false;
76
        }
77
78
        $config->loadArray($this->getConfig());
79
80
        // Create HTMLPurifier object
81
        $this->purifier = new HTMLPurifier($this->configure($config));
82
    }
83
84
    /**
85
     * Check/Create cache directory
86
     */
87
    private function checkCacheDirectory()
88
    {
89
        $cachePath = $this->config->get('purifier.cachePath');
90
91
        if ($cachePath) {
92
            if (!$this->files->isDirectory($cachePath)) {
93
                $this->files->makeDirectory($cachePath);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @param HTMLPurifier_Config $config
100
     * @return HTMLPurifier_Config
101
     */
102
    protected function configure(HTMLPurifier_Config $config)
103
    {
104
        return HTMLPurifier_Config::inherit($config);
105
    }
106
107
    /**
108
     * @param null $config
109
     * @return mixed|null
110
     */
111
    protected function getConfig($config = null)
112
    {
113
        $default_config = [];
114
        $default_config['Core.Encoding']        = $this->config->get('purifier.encoding');
115
        $default_config['Cache.SerializerPath'] = $this->config->get('purifier.cachePath');
116
117
        if (!$config) {
118
            $config = $this->config->get('purifier.settings.default');
119
        } elseif (is_string($config)) {
120
            $config = $this->config->get('purifier.settings.' . $config);
121
        }
122
123
        if (!is_array($config)) {
124
            $config = [];
125
        }
126
127
        $config = $default_config + $config;
128
129
        return $config;
130
    }
131
132
    /**
133
     * @param      $dirty
134
     * @param null $config
135
     * @return mixed
136
     */
137
    public function clean($dirty, $config = null)
138
    {
139
        if (is_array($dirty)) {
140
            return array_map(function ($item) use ($config) {
141
                return $this->clean($item, $config);
142
            }, $dirty);
143
        } else {
144
            //the htmlpurifier use replace instead merge, so we merge
145
            return $this->purifier->purify($dirty, $this->getConfig($config));
0 ignored issues
show
Documentation introduced by
$this->getConfig($config) is of type array, but the function expects a object<HTMLPurifier_Config>|null.

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...
146
        }
147
    }
148
149
    /**
150
     * Get HTMLPurifier instance.
151
     *
152
     * @return \HTMLPurifier
153
     */
154
    public function getInstance()
155
    {
156
        return $this->purifier;
157
    }
158
}
159