ObjectManager::modify()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the sauls/object-registry-bundle package.
4
 *
5
 * @author    Saulius Vaičeliūnas <[email protected]>
6
 * @link      http://saulius.vaiceliunas.lt
7
 * @copyright 2018
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Sauls\Bundle\ObjectRegistryBundle\Manager;
14
15
use Sauls\Bundle\ObjectRegistryBundle\EventDispatcher\EventDispatcherInterface;
16
use Sauls\Bundle\ObjectRegistryBundle\Exception\UnsupportedObjectClassException;
17
use function Sauls\Component\Helper\define_object;
18
19
class ObjectManager implements ManagerInterface, NamedManagerInterface
20
{
21
    public const DEFAULT_OBJECT_MANAGER_NAME = 'default.object_manager';
22
23
    /**
24
     * @var string
25
     */
26
    protected $objectClass;
27
    /**
28
     * @var EventDispatcherInterface
29
     */
30
    protected $eventDispatcher;
31
32
    /**
33
     * @param EventDispatcherInterface $eventDispatcher
34
     */
35 15
    public function __construct(EventDispatcherInterface $eventDispatcher)
36
    {
37 15
        $this->eventDispatcher = $eventDispatcher;
38 15
    }
39
40
    /**
41
     * @param array $properties
42
     * @return object
43
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
44
     */
45 6
    public function create(array $properties = []): object
46
    {
47 6
        return $this->modify(new $this->objectClass, $properties);
48
    }
49
50
    /**
51
     * @param object $object
52
     * @param array $properties
53
     * @return object
54
     * @throws \Sauls\Component\Helper\Exception\PropertyNotAccessibleException
55
     */
56 7
    public function modify(object $object, array $properties): object
57
    {
58 7
        $this->checkObjectIntegrity($object);
59
60 6
        return define_object($object, $properties);
61
    }
62
63 7
    public function checkObjectIntegrity(object $object): void
64
    {
65 7
        if ($this->notSupported($object)) {
66 1
            throw new UnsupportedObjectClassException(
67 1
                sprintf(
68 1
                    'Object manager of `%s` object class does not support given `%s` class',
69 1
                    $this->objectClass,
70 1
                    \get_class($object)
71
                )
72
            );
73
        }
74 6
    }
75
76 1
    private function classIsSupported(string $class): bool
77
    {
78 1
        return false === empty($this->objectClass) && \is_a($class, $this->objectClass, true);
79
    }
80
81 8
    private function objectIsSupported(object $object): bool
82
    {
83 8
        return $object instanceof $this->objectClass;
84
    }
85
86 7
    private function notSupported($value): bool
87
    {
88 7
        return false === $this->supports($value);
89
    }
90
91
    /**
92
     * @param mixed|object|string $value
93
     * @return bool
94
     */
95 10
    public function supports($value): bool
96
    {
97 10
        if (\is_string($value)) {
98 1
            return $this->classIsSupported($value);
99
        }
100
101 9
        if (\is_object($value)) {
102 8
            return $this->objectIsSupported($value);
103
        }
104
105 1
        return false;
106
    }
107
108 1
    public function getName(): string
109
    {
110 1
        return self::DEFAULT_OBJECT_MANAGER_NAME;
111
    }
112
113 14
    public function setObjectClass(string $class): void
114
    {
115 14
        $this->objectClass = $class;
116 14
    }
117
}
118