ProductCode::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace SyliusCart\Domain\ValueObject;
6
7
use SyliusCart\Domain\Exception\ProductCodeCannotBeEmptyException;
8
9
/**
10
 * @author Arkadiusz Krakowiak <[email protected]>
11
 */
12
final class ProductCode
13
{
14
    /**
15
     * @var string
16
     */
17
    private $code;
18
19
    /**
20
     * @param string $code
21
     */
22
    private function __construct(string $code)
23
    {
24
        if ('' === $code) {
25
            throw new ProductCodeCannotBeEmptyException();
26
        }
27
28
        $this->code = $code;
29
    }
30
31
    /**
32
     * @param string $code
33
     *
34
     * @return self
35
     */
36
    public static function fromString($code): self
37
    {
38
        return new self($code);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function __toString(): string
45
    {
46
        return $this->code;
47
    }
48
49
    /**
50
     * @param ProductCode $productCode
51
     *
52
     * @return bool
53
     */
54
    public function equals(ProductCode $productCode): bool
55
    {
56
        return  $productCode->__toString() === $this->code;
57
    }
58
}
59