Template::__construct()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 2
nop 9
dl 0
loc 31
rs 8.439
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace EaselDrawing\Templates;
4
5
use EaselDrawing\Canvas;
6
use EaselDrawing\Color;
7
use EaselDrawing\Elements;
8
use EaselDrawing\Font;
9
use EaselDrawing\Orientation;
10
11
class Template
12
{
13
    /** @var int */
14
    private $width;
15
16
    /** @var int */
17
    private $height;
18
19
    /** @var Color */
20
    private $foreground;
21
22
    /** @var Color */
23
    private $background;
24
25
    /** @var Font[] */
26
    private $fonts;
27
28
    /** @var PathResolver */
29
    private $pathResolver;
30
31
    /** @var Elements */
32
    private $elements;
33
34
    /** @var bool */
35
    private $grayScale;
36
37
    /** @var Orientation */
38
    private $orientation;
39
40
    /**
41
     * Template constructor.
42
     * @param int $width
43
     * @param int $height
44
     * @param Color $foreground
45
     * @param Color $background
46
     * @param Elements $elements
47
     * @param Font[] $fonts
48
     * @param PathResolver $pathResolver
49
     * @param bool $grayScale,
50
     * @param Orientation $orientation
51
     */
52
    public function __construct(
53
        int $width,
54
        int $height,
55
        Color $foreground,
56
        Color $background,
57
        Elements $elements,
58
        array $fonts,
59
        PathResolver $pathResolver,
60
        bool $grayScale,
61
        Orientation $orientation
62
    ) {
63
        if ($width < 1) {
64
            throw new \InvalidArgumentException("Width must be an integer greater than zero");
65
        }
66
        if ($height < 1) {
67
            throw new \InvalidArgumentException("Height must be an integer greater than zero");
68
        }
69
        foreach ($fonts as $index => $font) {
70
            if (! ($font instanceof Font)) {
71
                throw new \InvalidArgumentException("The font index $index is not valid");
72
            }
73
        }
74
        $this->width = $width;
75
        $this->height = $height;
76
        $this->foreground = $foreground;
77
        $this->background = $background;
78
        $this->fonts = $fonts;
79
        $this->pathResolver = $pathResolver;
80
        $this->elements = $elements;
81
        $this->grayScale = $grayScale;
82
        $this->orientation = $orientation;
83
    }
84
85
    public function getWidth(): int
86
    {
87
        return $this->width;
88
    }
89
90
    public function getHeight(): int
91
    {
92
        return $this->height;
93
    }
94
95
    public function getForeground(): Color
96
    {
97
        return $this->foreground;
98
    }
99
100
    public function getBackground(): Color
101
    {
102
        return $this->background;
103
    }
104
105
    /**
106
     * @return Font[]
107
     */
108
    public function getFonts(): array
109
    {
110
        return $this->fonts;
111
    }
112
113
    public function getPathResolver(): PathResolver
114
    {
115
        return $this->pathResolver;
116
    }
117
118
    public function getElements(): Elements
119
    {
120
        return $this->elements;
121
    }
122
123
    public function isGrayScale(): bool
124
    {
125
        return $this->grayScale;
126
    }
127
128
    public function getOrientation(): Orientation
129
    {
130
        return $this->orientation;
131
    }
132
133
    public function getFont(string $name): Font
134
    {
135
        if (! count($this->fonts)) {
136
            throw new \RuntimeException('There are no fonts in the template');
137
        }
138
        foreach ($this->fonts as $font) {
139
            if (0 === strcasecmp($font->getFamily(), $name)) {
140
                return $font;
141
            }
142
        }
143
        return $this->fonts[0];
144
    }
145
146
    public function asCanvas() : Canvas
147
    {
148
        return new Canvas(
149
            $this->getWidth(),
150
            $this->getHeight(),
151
            $this->getBackground(),
152
            $this->getElements(),
153
            $this->isGrayScale(),
154
            $this->getOrientation()
155
        );
156
    }
157
}
158