Completed
Pull Request — master (#15)
by Jonathan
05:36
created

BasicObjectDataRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 69.57%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 62
ccs 16
cts 23
cp 0.6957
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 11 2
A __construct() 0 4 1
A getObjectIdentifier() 0 5 1
A getClassName() 0 3 1
A setClassName() 0 3 1
A getIdentifier() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\SkeletonMapper\DataRepository;
6
7
use Doctrine\SkeletonMapper\ObjectManagerInterface;
8
use function array_combine;
9
use function assert;
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 27
    public function __construct(ObjectManagerInterface $objectManager, string $className)
21
    {
22 27
        $this->objectManager = $objectManager;
23 27
        $this->className     = $className;
24 27
    }
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
        assert($criteria !== false);
50
51 19
        return $this->findOneBy($criteria);
52
    }
53
54
    /**
55
     * @return mixed[]
56
     */
57 19
    protected function getIdentifier() : array
58
    {
59 19
        return $this->objectManager
60 19
            ->getClassMetadata($this->getClassName())
61 19
            ->getIdentifier();
62
    }
63
64
    /**
65
     * @param object $object
66
     *
67
     * @return mixed[]
68
     */
69
    protected function getObjectIdentifier($object) : array
70
    {
71
        return $this->objectManager
72
            ->getRepository($this->getClassName())
73
            ->getObjectIdentifier($object);
74
    }
75
}
76