AbstractScalarValueObject   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1
Metric Value
wmc 10
cbo 1
dl 0
loc 69
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isSameAs() 0 9 2
A getRawValue() 0 4 1
A __toString() 0 4 1
A guardAgainstNonScalar() 0 8 2
A areObjectsOfSameType() 0 4 3
1
<?php
2
3
namespace Utils\Domain;
4
5
/**
6
 * Scalar Value Object
7
 */
8
abstract class AbstractScalarValueObject implements ValueObjectInterface
9
{
10
    /** @var mixed */
11
    protected $value;
12
13
    /**
14
     * {@inheritDoc}
15
     */
16
    final public function __construct($value)
17
    {
18
        $this->guardAgainstNonScalar($value);
19
20
        $this->value = $value;
21
    }
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    final public function isSameAs($value)
27
    {
28
        // If the given object is not an AbstractScalarValueObject, we can't compare them
29
        if (!$this->areObjectsOfSameType($this, $value)) {
30
            return false;
31
        }
32
33
        return $this->getRawValue() === $value->getRawValue();
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    final public function getRawValue()
40
    {
41
        return $this->value;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    final public function __toString()
48
    {
49
        return (string) $this->getRawValue();
50
    }
51
52
    /**
53
     * @param mixed $value
54
     *
55
     * @throws \InvalidArgumentException
56
     */
57
    final protected function guardAgainstNonScalar($value)
58
    {
59
        if (!is_scalar($value)) {
60
            throw new \InvalidArgumentException(
61
                'Value objects does not accept non-scalar values such as '.gettype($value)
62
            );
63
        }
64
    }
65
66
    /**
67
     * @param mixed $value1
68
     * @param mixed $value2
69
     *
70
     * @return bool
71
     */
72
    final protected function areObjectsOfSameType($value1, $value2)
73
    {
74
        return is_object($value1) && is_object($value2) && (get_class($value1) === get_class($value2));
75
    }
76
}
77