Completed
Push — master ( 50437b...6d2b5a )
by Adam
02:31
created

VOObject::count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
3
namespace BestServedCold\PhalueObjects;
4
5
use BestServedCold\PhalueObjects\Exception\InvalidTypeException;
6
7
/**
8
 * Class VOObject
9
 *
10
 * @package BestServedCold\PhalueObjects\ValueObject
11
 */
12
class VOObject extends ValueObject implements \Countable
13
{
14
    /**
15
     * @var \ReflectionClass
16
     */
17
    protected $reflection;
18
19
    /**
20
     * ObjectValueObject constructor.
21
     *
22
     * @param $value
23
     */
24 8
    public function __construct($value)
25
    {
26 8
        if (!is_object($value)) {
27 1
            throw new InvalidTypeException($value, [ 'object' ]);
28
        }
29
30 8
        $this->reflection = new \ReflectionClass($value);
31 8
        parent::__construct($value);
32 8
    }
33
34
    /**
35
     * @return object
36
     */
37 7
    public function getValue()
38
    {
39 7
        return (object) parent::getValue();
40
    }
41
42
43
    /**
44
     * @return string
45
     */
46 2
    public function getType()
47
    {
48 2
        return get_class($this->getValue());
49
    }
50
51
    /**
52
     * @return string
53
     */
54 1
    public function getShortName()
55
    {
56 1
        return $this->reflection->getShortName();
57
    }
58
59
    /**
60
     * @return integer
61
     * @throws \Exception
62
     */
63
    public function count()
64
    {
65
        if (! method_exists($this->getValue(), 'count')) {
66
            throw new \Exception('[' . $this->getShortName() . '] does not have a count method.');
67
        }
68
69
        return $this->getValue()->count();
70
    }
71
}
72