FigletString::justification()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.125

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 15
c 2
b 0
f 0
dl 0
loc 20
rs 9.4555
ccs 7
cts 14
cp 0.5
cc 5
nc 5
nop 1
crap 8.125
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Laravel Zero.
7
 *
8
 * (c) Nuno Maduro <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace LaravelZero\Framework\Components\Logo;
15
16
use InvalidArgumentException;
17
use Zend\Text\Figlet\Figlet as ZendFiglet;
18
19
/**
20
 * @internal
21
 */
22
final class FigletString
23
{
24
    private $string;
25
26
    private $figlet;
27
28
    public const DEFAULT_FONT = __DIR__.DIRECTORY_SEPARATOR.'fonts'.DIRECTORY_SEPARATOR.'big.flf';
29
30 1
    public function __construct(string $string, array $options)
31
    {
32 1
        $this->string = $string;
33 1
        $this->figlet = new ZendFiglet();
34
35 1
        $this->parseOptions($options);
36 1
    }
37
38 1
    private function parseOptions(array $config)
39
    {
40
        $this
41 1
            ->font($config['font'] ?? self::DEFAULT_FONT)
42 1
            ->outputWidth($config['outputWidth'] ?? 80)
43 1
            ->justification($config['justification'] ?? null)
44 1
            ->rightToLeft($config['rightToLeft'] ?? null);
45 1
    }
46
47 1
    private function font(?string $font)
48
    {
49 1
        if (is_null($font)) {
50
            return $this;
51
        }
52
53 1
        $this->figlet->setFont($font);
54
55 1
        return $this;
56
    }
57
58 1
    private function outputWidth(int $outputWidth)
59
    {
60 1
        $this->figlet->setOutputWidth($outputWidth);
61
62 1
        return $this;
63
    }
64
65 1
    private function justification(?string $justification)
66
    {
67 1
        switch ($justification) {
68 1
            case 'left':
69
                $this->figlet->setJustification(ZendFiglet::JUSTIFICATION_LEFT);
70
                break;
71 1
            case 'center':
72
                $this->figlet->setJustification(ZendFiglet::JUSTIFICATION_CENTER);
73
                break;
74 1
            case 'right':
75
                $this->figlet->setJustification(ZendFiglet::JUSTIFICATION_RIGHT);
76
                break;
77
            case null:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $justification of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
78
                // Let ZendFiglet handle the justification
79 1
                break;
80
            default:
81
                throw new InvalidArgumentException('Invalid value given for the `logo.justification` option');
82
        }
83
84 1
        return $this;
85
    }
86
87 1
    private function rightToLeft(?string $rightToLeft)
88
    {
89 1
        switch ($rightToLeft) {
90 1
            case 'right-to-left':
91
                $this->figlet->setRightToLeft(ZendFiglet::DIRECTION_RIGHT_TO_LEFT);
92
                break;
93 1
            case 'left-to-right':
94
                $this->figlet->setRightToLeft(ZendFiglet::DIRECTION_LEFT_TO_RIGHT);
95
                break;
96
            case null:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $rightToLeft of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
97
                // Let ZendFiglet handle this
98 1
                break;
99
            default:
100
                throw new \InvalidArgumentException('Invalid value given for the `logo.rightToLeft` option');
101
        }
102
103 1
        return $this;
104
    }
105
106 1
    public function __toString()
107
    {
108 1
        $rendered = $this->figlet->render($this->string);
109
110 1
        return "\n$rendered\n";
111
    }
112
}
113