Completed
Push — master ( 5bf305...2d7c2d )
by Dominik
01:49
created

MethodAccessor::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Accessor;
6
7
final class MethodAccessor implements AccessorInterface
8
{
9
    /**
10
     * @var string
11
     */
12
    private $property;
13
14
    /**
15
     * @param string $property
16
     */
17 6
    public function __construct(string $property)
18
    {
19 6
        $this->property = $property;
20 6
    }
21
22
    /**
23
     * @param object $object
24
     * @param mixed  $value
25
     *
26
     * @throws AccessorException
27
     */
28 2
    public function setValue($object, $value)
29
    {
30 2
        $set = 'set'.ucfirst($this->property);
31 2
        if (!method_exists($object, $set)) {
32 1
            throw AccessorException::createMissingMethod(get_class($object), [$set]);
33
        }
34
35 1
        return $object->$set($value);
36
    }
37
38
    /**
39
     * @param object $object
40
     *
41
     * @return mixed
42
     *
43
     * @throws AccessorException
44
     */
45 4
    public function getValue($object)
46
    {
47 4
        $get = 'get'.ucfirst($this->property);
48 4
        $has = 'has'.ucfirst($this->property);
49 4
        $is = 'is'.ucfirst($this->property);
50
51 4
        if (method_exists($object, $get)) {
52 1
            return $object->$get();
53
        }
54
55 3
        if (method_exists($object, $has)) {
56 1
            return $object->$has();
57
        }
58
59 2
        if (method_exists($object, $is)) {
60 1
            return $object->$is();
61
        }
62
63 1
        throw AccessorException::createMissingMethod(get_class($object), [$get, $has, $is]);
64
    }
65
}
66