Completed
Pull Request — master (#82)
by
unknown
10:19
created

Purifier::addCustomElements()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 2
dl 0
loc 17
ccs 13
cts 13
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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 Illuminate\Contracts\Config\Repository;
20
use Illuminate\Filesystem\Filesystem;
21
22
class Purifier
23
{
24
    /**
25
     * @var \Mews\Purifier\PurifierConfigBuilder
26
     */
27
    protected $configBuilder;
28
29
    /**
30
     * @var Filesystem
31
     */
32
    protected $files;
33
34
    /**
35
     * @var Repository
36
     */
37
    protected $config;
38
39
    /**
40
     * @var HTMLPurifier
41
     */
42
    protected $purifier;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param \Mews\Purifier\PurifierConfigBuilder $configBuilder
48 24
     * @param Filesystem $files
49
     * @param Repository $config
50 24
     * @throws Exception
51 24
     */
52
    public function __construct(PurifierConfigBuilder $configBuilder, Filesystem $files, Repository $config)
53 24
    {
54 19
        $this->configBuilder = $configBuilder;
55
        $this->config = $config;
56
        $this->files = $files;
57
58
        $this->setUp();
59
    }
60
61 24
    /**
62
     * Setup
63 24
     *
64 5
     * @throws Exception
65
     */
66
    private function setUp()
67 19
    {
68
        if (!$this->config->has('purifier')) {
69
            throw new Exception('Configuration parameters not loaded!');
70 19
        }
71
72
        $this->checkCacheDirectory();
73 19
74
        $config = $this->configBuilder->getConfig();
75
76
        // Create HTMLPurifier object
77 19
        $this->purifier = new HTMLPurifier($config);
78
    }
79
80 19
    /**
81 19
     * Check/Create cache directory
82 19
     */
83
    private function checkCacheDirectory()
84
    {
85 19
        $cachePath = $this->config->get('purifier.cachePath');
86 19
87 19
        if ($cachePath) {
88 19
            if (!$this->files->isDirectory($cachePath)) {
89 19
                $this->files->makeDirectory($cachePath, $this->config->get('purifier.cacheFileMode', 0755));
90
            }
91
        }
92 19
    }
93 19
94 19
    /**
95 19
     * @param      $dirty
96 19
     * @param null $config
97
     * 
98
     * @return mixed
99 19
     */
100 19
    public function clean($dirty, $config = null)
101
    {
102
        if (is_array($dirty)) {
103
            return array_map(function ($item) use ($config) {
104
                return $this->clean($item, $config);
105
            }, $dirty);
106
        }
107
108
        return $this->purifier->purify($dirty, $config ? $this->configBuilder->getConfig($config) : null);
109
    }
110
111 19
    /**
112
     * Get HTMLPurifier instance.
113 19
     *
114
     * @return \HTMLPurifier
115
     */
116
    public function getInstance()
117
    {
118
        return $this->purifier;
119 19
    }
120
}
121