Completed
Push — master ( 3f79e5...1ec99b )
by Kevin
02:42
created

Groundskeeper::clean()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.8571
cc 3
eloc 15
nc 1
nop 1
crap 3
1
<?php
2
3
namespace Groundskeeper;
4
5
use Groundskeeper\Tokens\Tokenizer;
6
use Psr\Log\LoggerAwareInterface;
7
use Psr\Log\LoggerInterface;
8
9
class Groundskeeper implements LoggerAwareInterface
10
{
11
    /** @var Configuration */
12
    private $configuration;
13
14
    /** @var null|LoggerInterface */
15
    private $logger;
16
17
    /**
18
     * Constructor
19
     *
20
     * @param array|Configuration $options
21
     */
22 44
    public function __construct($options = array())
23
    {
24 44
        $this->logger = null;
25 44
        if ($options instanceof Configuration) {
26 2
            $this->configuration = $options;
27
28 1
            return;
29 1
        }
30
31 44
        if (!is_array($options)) {
32 1
            throw new \InvalidArgumentException('Invalid option type.');
33
        }
34
35 43
        $this->configuration = new Configuration($options);
36 43
    }
37
38 42
    public function clean($html)
39
    {
40
        // Tokenize.
41 42
        $tokenizer = new Tokenizer($this->configuration);
42 42
        $tokenContainer = $tokenizer->tokenize($html);
43
44
        // Remove unwanted tokens.
45 42
        $tokenContainer->remove($this->logger);
46
47
        // Build output generator.
48
        $outputClassName = 'Groundskeeper\\Output\\' .
49 42
            ucfirst($this->configuration->get('output'));
50 42
        $outputGenerator = new $outputClassName();
51
52
        // Clean
53 42
        $i = 0;
54
        do {
55 42
            $preCleaningOutput = $outputGenerator($tokenContainer);
56 42
            $tokenContainer->clean($this->logger);
57 42
            $cleanedOutput = $outputGenerator($tokenContainer);
58 42
            $i++;
59 42
        } while ($i < 5 && $preCleaningOutput != $cleanedOutput);
60
61 42
        return $cleanedOutput;
62
    }
63
64 1
    public function getConfiguration()
65
    {
66 1
        return $this->configuration;
67
    }
68
69 43
    public function setLogger(LoggerInterface $logger)
70
    {
71 43
        $this->logger = $logger;
72 43
    }
73
}
74