Completed
Push — refactor-entity-validators ( 9b4b77...c6627d )
by Stefano
13:07 queued 10:18
created

DataMapperManager::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 2
cts 4
cp 0.5
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.5
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\Persistence\DataMapper\Manager;
9
10
use Thorr\Persistence\DataMapper\DataMapperInterface;
11
use Zend\ServiceManager\AbstractPluginManager;
12
use Zend\ServiceManager\ConfigInterface;
13
use Zend\ServiceManager\Exception;
14
15
/**
16
 * @method DataMapperInterface get($name)
17
 */
18
class DataMapperManager extends AbstractPluginManager implements DataMapperManagerInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $entityDataMapperMap;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 15
    public function __construct(ConfigInterface $configuration = null)
29
    {
30
        parent::__construct($configuration);
31
32 15
        if ($configuration instanceof DataMapperManagerConfig) {
33
            $this->entityDataMapperMap = $configuration->getEntityDataMapperMap();
34
        }
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 7
    public function validatePlugin($dataMapper)
41
    {
42 7
        if (! $dataMapper instanceof DataMapperInterface) {
43
            throw new Exception\RuntimeException(sprintf(
44 1
                'Invalid DataMapper type; expected %s, got %s',
45 1
                DataMapperInterface::class,
46
                (is_object($dataMapper) ? get_class($dataMapper) : gettype($dataMapper))
47
            ));
48
        }
49
50
        if (! class_exists($dataMapper->getEntityClass())) {
51
            throw new Exception\RuntimeException(sprintf(
52
                '%s::getEntityClass() must return a valid class', get_class($dataMapper)
53
            ));
54
        }
55 5
    }
56
57
    /**
58
     * @param string $entityClass
59
     *
60
     * @return DataMapperInterface
61
     */
62 7
    public function getDataMapperForEntity($entityClass)
63
    {
64 7
        if (! isset($this->entityDataMapperMap[$entityClass])) {
65
            throw new Exception\InvalidArgumentException(sprintf(
66 3
                "Could not find data mapper service name for entity class '%s'",
67
                $entityClass
68
            ));
69
        }
70
71 4
        $entityDMServiceName = $this->entityDataMapperMap[$entityClass];
72
73
        $dataMapper = $this->get($entityDMServiceName);
74
75
        if (! is_a($dataMapper->getEntityClass(), $entityClass, true)) {
76
            throw new Exception\RuntimeException(sprintf(
77 1
                '"%s" entity class mismatch: expected "%s", got "%s"',
78
                $entityDMServiceName,
79
                $entityClass,
80 1
                $dataMapper->getEntityClass()
81
            ));
82
        }
83
84 3
        return $dataMapper;
85
    }
86
}
87