Subject::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
}