StringIdentity   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 57
wmc 8
lcom 1
cbo 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromString() 0 4 1
A __toString() 0 4 1
A equals() 0 4 2
A guardString() 0 6 3
1
<?php
2
3
namespace Domain\Identity;
4
5
/**
6
 * @author Sebastiaan Hilbers <[email protected]>
7
 */
8
abstract class StringIdentity implements Identity
9
{
10
    /**
11
     * @var string
12
     */
13
    private $string;
14
15
    public function __construct($string)
16
    {
17
        self::guardString($string);
18
        $this->string = $string;
19
    }
20
21
    /**
22
     * Creates an identifier object from a string representation
23
     *
24
     * @param $string
25
     * @return static
26
     */
27
    public static function fromString($string)
28
    {
29
        return new static($string);
30
    }
31
32
    /**
33
     * Returns a string that can be parsed by fromString()
34
     *
35
     * @return string
36
     */
37
    public function __toString()
38
    {
39
        return (string) $this->string;
40
    }
41
42
    /**
43
     * Compares the object to another IdentifiesAggregate object. Returns true if both have the same type and value.
44
     *
45
     * @param Identity $other
46
     * @return boolean
47
     */
48
    public function equals(Identity $other)
49
    {
50
        return $other instanceof static && $this->string == $other->string;
51
    }
52
53
    /**
54
     * Make sure that the input is in fact a string
55
     *
56
     * @param $string
57
     */
58
    private static function guardString($string)
59
    {
60
        if (!is_string($string) || empty($string)) {
61
            throw new \InvalidArgumentException("String expected");
62
        }
63
    }
64
}
65