Notification::formatLine()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of CaptainHook
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Hook\Notify;
13
14
/**
15
 * Class Notification
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/captainhook-git/captainhook
20
 * @since   Class available since Release 5.4.5
21
 */
22
class Notification
23
{
24
    /**
25
     * List of rules to check
26
     *
27
     * @var string[]
28
     */
29
    private $lines = [];
30
31
    /**
32
     * Max line length
33
     *
34
     * @var int
35
     */
36
    private $maxLineLength = 0;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param  string[] $lines
42
     */
43 5
    public function __construct(array $lines)
44
    {
45 5
        $this->lines = $lines;
46 5
        foreach ($this->lines as $line) {
47 4
            $lineLength = mb_strlen($line);
48 4
            if ($lineLength > $this->maxLineLength) {
49 4
                $this->maxLineLength = $lineLength;
50
            }
51
        }
52
    }
53
54
    /**
55
     * Return line count
56
     *
57
     * @return int
58
     */
59 2
    public function length(): int
60
    {
61 2
        return count($this->lines);
62
    }
63
64
    /**
65
     * Returns the string to display
66
     *
67
     * @return string
68
     */
69 3
    public function banner(): string
70
    {
71 3
        $text   = [];
72 3
        $text[] = '<error>' . str_repeat(' ', $this->maxLineLength + 6) . '</error>';
73 3
        $text[] = '<error>  </error> ' . str_repeat(' ', $this->maxLineLength) . ' <error>  </error>';
74 3
        foreach ($this->lines as $line) {
75 3
            $text[] = $this->formatLine($line);
76
        }
77 3
        $text[] = '<error>  </error> ' . str_repeat(' ', $this->maxLineLength) . ' <error>  </error>';
78 3
        $text[] = '<error>' . str_repeat(' ', $this->maxLineLength + 6) . '</error>';
79
80 3
        return PHP_EOL . implode(PHP_EOL, $text) . PHP_EOL;
81
    }
82
83 3
    private function formatLine(string $line): string
84
    {
85 3
        $length = mb_strlen($line);
86 3
        $left   = '';
87 3
        $right  = '';
88 3
        if ($length < $this->maxLineLength) {
89 1
            $space = $this->maxLineLength - $length;
90 1
            $left  = str_repeat(' ', (int) floor($space / 2));
91 1
            $right = str_repeat(' ', (int) ceil($space / 2));
92
        }
93 3
        return '<error>  </error> ' . $left . $line . $right . ' <error>  </error>';
94
    }
95
}
96