BasicObjectDataRepository   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
c 2
b 0
f 0
dl 0
loc 64
ccs 16
cts 24
cp 0.6667
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 13 3
A getObjectIdentifier() 0 5 1
A getIdentifier() 0 5 1
A getClassName() 0 3 1
A setClassName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\DataRepository;
6
7
use Doctrine\SkeletonMapper\ObjectManagerInterface;
8
use RuntimeException;
9
use function array_combine;
10
use function is_array;
11
12
abstract class BasicObjectDataRepository extends ObjectDataRepository
13
{
14
    /** @var ObjectManagerInterface */
15
    protected $objectManager;
16
17
    /** @var string */
18
    protected $className;
19
20 33
    public function __construct(ObjectManagerInterface $objectManager, string $className)
21
    {
22 33
        $this->objectManager = $objectManager;
23 33
        $this->className     = $className;
24 33
    }
25
26 20
    public function getClassName() : string
27
    {
28 20
        return $this->className;
29
    }
30
31
    public function setClassName(string $className) : void
32
    {
33
        $this->className = $className;
34
    }
35
36
    /**
37
     * @param mixed $id
38
     *
39
     * @return mixed[]
40
     */
41 19
    public function find($id) : ?array
42
    {
43 19
        $identifier = $this->getIdentifier();
44
45 19
        $identifierValues = is_array($id) ? $id : [$id];
46
47 19
        $criteria = array_combine($identifier, $identifierValues);
48
49 19
        if ($criteria === false) {
50
            throw new RuntimeException('array_combine failed. Make sure you passed a value for each identifier.');
51
        }
52
53 19
        return $this->findOneBy($criteria);
54
    }
55
56
    /**
57
     * @return mixed[]
58
     */
59 19
    protected function getIdentifier() : array
60
    {
61 19
        return $this->objectManager
62 19
            ->getClassMetadata($this->getClassName())
63 19
            ->getIdentifier();
64
    }
65
66
    /**
67
     * @param object $object
68
     *
69
     * @return mixed[]
70
     */
71
    protected function getObjectIdentifier($object) : array
72
    {
73
        return $this->objectManager
74
            ->getRepository($this->getClassName())
75
            ->getObjectIdentifier($object);
76
    }
77
}
78