Passed
Push — master ( 2f0999...9f5c06 )
by Damiano
03:44
created

Subject::build()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 4
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damianopetrungaro\PHPCommitizen\Section;
6
7
use Damianopetrungaro\PHPCommitizen\Configuration;
8
use Damianopetrungaro\PHPCommitizen\Exception\InvalidArgumentException;
9
use function strlen;
10
11
class Subject
12
{
13
    /**
14
     * @var Type
15
     */
16
    private $type;
17
18
    /**
19
     * @var Scope|null
20
     */
21
    private $scope;
22
23
    /**
24
     * @var Description
25
     */
26
    private $description;
27
28 3
    private function __construct(Type $type, ?Scope $scope, Description $description)
29
    {
30 3
        $this->type = $type;
31 3
        $this->scope = $scope;
32 3
        $this->description = $description;
33 3
    }
34
35
    /**
36
     * @throws InvalidArgumentException
37
     */
38 7
    public static function build(Type $type, ?Scope $scope, Description $description, Configuration $configuration): self
39
    {
40
        // 2 char more needed for the ": " when transforming to string
41 7
        $subjectLength = strlen((string)$type) + strlen((string)$scope) + strlen((string)$description) + 2;
42
43 7
        if ($subjectLength > $configuration->maxLengthSubject() || $subjectLength < $configuration->minLengthSubject()) {
44 4
            $errorMessage = sprintf("Invalid length for subject: '%s'. Must be between %s and %s",
45 4
                "{$type}{$scope}: {$description}",
46 4
                $configuration->minLengthSubject(),
47 4
                $configuration->maxLengthSubject()
48
            );
49 4
            throw new InvalidArgumentException($errorMessage);
50
        }
51
52 3
        return new self($type, $scope, $description);
53
    }
54
55 3
    public function __toString(): string
56
    {
57 3
        return "{$this->type}{$this->scope}: {$this->description}";
58
    }
59
}