AbstractValue   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 9
c 1
b 0
f 0
dl 0
loc 80
ccs 14
cts 14
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 5 1
A __construct() 0 3 1
A __invoke() 0 3 1
A doHash() 0 3 1
A value() 0 3 1
A set() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\valuewrapper;
6
7
/**
8
 * Class AbstractValue.
9
 */
10
abstract class AbstractValue implements ValueInterface
11
{
12
    /**
13
     * @var mixed
14
     */
15
    private $value;
16
17
    /**
18
     * AbstractValue constructor.
19
     *
20
     * @param mixed $value
21
     */
22 126
    public function __construct($value)
23
    {
24 126
        $this->value = $value;
25 126
    }
26
27
    /**
28
     * @return mixed
29
     */
30 1
    public function __invoke()
31
    {
32 1
        return $this->value();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 7
    public function apply(callable $callable)
39
    {
40 7
        $value = $this->value();
41
42 7
        return $callable($value);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    abstract public function hash(): string;
49
50
    /**
51
     * @return string
52
     */
53
    abstract public function serialize();
54
55
    /**
56
     * @param string $serialized
57
     *
58
     * @return mixed
59
     */
60
    abstract public function unserialize($serialized);
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 89
    public function value()
66
    {
67 89
        return $this->value;
68
    }
69
70
    /**
71
     * @param string $string
72
     *   The string to hash
73
     *
74
     * @return string
75
     *   The string's hash
76
     */
77 28
    protected function doHash(string $string): string
78
    {
79 28
        return sha1($string);
80
    }
81
82
    /**
83
     * Set the value.
84
     *
85
     * @param mixed $value
86
     */
87 15
    protected function set($value): void
88
    {
89 15
        $this->value = $value;
90 15
    }
91
}
92