ObjectConverter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 4
dl 0
loc 63
rs 10
c 0
b 0
f 0
ccs 18
cts 18
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setPropertyPath() 0 4 1
A getPropertyPath() 0 4 1
B __invoke() 0 18 5
1
<?php
2
3
namespace Ddeboer\DataImport\ValueConverter;
4
5
use Ddeboer\DataImport\Exception\UnexpectedTypeException;
6
use Symfony\Component\PropertyAccess\PropertyPath;
7
use Symfony\Component\PropertyAccess\PropertyAccess;
8
use Symfony\Component\PropertyAccess\PropertyAccessor;
9
10
/**
11
 * @author Markus Bachmann <[email protected]
12
 */
13
class ObjectConverter
14
{
15
    /**
16
     * @var string|null
17
     */
18
    protected $propertyPath;
19
20
    /**
21
     * @var PropertyAccessor
22
     */
23
    protected $propertyAccessor;
24
25
    /**
26
     * @param string|null $propertyPath
27
     */
28 5
    public function __construct($propertyPath = null)
29
    {
30 5
        $this->propertyPath = $propertyPath;
31 5
        $this->propertyAccessor = PropertyAccess::createPropertyAccessor();
32 5
    }
33
34
    /**
35
     * Sets the property
36
     *
37
     * @param string $propertyPath
38
     */
39 1
    public function setPropertyPath($propertyPath)
40
    {
41 1
        $this->propertyPath = $propertyPath;
42 1
    }
43
44
    /**
45
     * Gets the property
46
     *
47
     * @return null|string
48
     */
49 1
    public function getPropertyPath()
50
    {
51 1
        return $this->propertyPath;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 4
    public function __invoke($input)
58
    {
59 4
        if (!is_object($input)) {
60 1
            throw new UnexpectedTypeException($input, 'object');
61
        }
62
63 3
        if (null === $this->propertyPath && !method_exists($input, '__toString')) {
64 1
            throw new \RuntimeException;
65
        }
66
67 2
        if (null === $this->propertyPath) {
68 1
            return (string) $input;
69
        }
70
71 1
        $path = new PropertyPath($this->propertyPath);
72
73 1
        return $this->propertyAccessor->getValue($input, $path);
74
    }
75
}
76