SlidesParam   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 69
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 31 6
A isHeader1() 0 4 1
A isHeader2() 0 4 1
A normalizeNewLines() 0 6 1
1
<?php declare(strict_types=1);
2
3
4
namespace Bigwhoop\Trumpet\Config\Params;
5
6
use Bigwhoop\Trumpet\Config\ConfigException;
7
use Bigwhoop\Trumpet\Config\Presentation;
8
use Bigwhoop\Trumpet\Config\Slides;
9
10
final class SlidesParam implements Param
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15
    public function parse($value, Presentation $presentation)
16
    {
17
        if (!is_string($value)) {
18
            throw new ConfigException('Slides must be a string.');
19
        }
20
21
        $text = $this->normalizeNewLines($value);
22
23
        $lines = explode("\n", $text);
24
25
        if (empty($lines)) {
26
            throw new ConfigException('Slides must not be empty.');
27
        }
28
29
        $slides = new Slides();
30
31
        foreach ($lines as $line) {
32
            if ($this->isHeader1($line)) {
33
                $slides->addContentToNewSlide($line);
34
                $slides->addBlankSlide();
35
            } elseif ($this->isHeader2($line)) {
36
                $slides->addContentToNewSlide($line);
37
            } else {
38
                $slides->addContentToCurrentSlide($line);
39
            }
40
        }
41
42
        $slides->trim();
43
44
        $presentation->slides = $slides;
45
    }
46
47
    /**
48
     * @param string $line
49
     *
50
     * @return bool
51
     */
52
    private function isHeader1($line)
53
    {
54
        return substr($line, 0, 2) === '# ';
55
    }
56
57
    /**
58
     * @param string $line
59
     *
60
     * @return bool
61
     */
62
    private function isHeader2($line)
63
    {
64
        return substr($line, 0, 3) === '## ';
65
    }
66
67
    /**
68
     * @param string $text
69
     *
70
     * @return string
71
     */
72
    private function normalizeNewLines($text)
73
    {
74
        $text = str_replace("\r", '', $text);
75
76
        return $text;
77
    }
78
}
79