Repository::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Domain\Repository;
12
13
use Cubiche\Domain\Model\AggregateRootInterface;
14
15
/**
16
 * Repository Class.
17
 *
18
 * @author Karel Osorio Ramírez <[email protected]>
19
 */
20
abstract class Repository implements RepositoryInterface
21
{
22
    /**
23
     * @var \ReflectionClass
24
     */
25
    protected $entityReflectionClass;
26
27
    /**
28
     * @param string $entityName
29
     */
30
    public function __construct($entityName)
31
    {
32
        $this->entityReflectionClass = new \ReflectionClass($entityName);
33
        if (!$this->entityReflectionClass->isSubclassOf(AggregateRootInterface::class)) {
34
            throw new \LogicException(\sprintf(
35
                '%s not implement %s, only the aggregate roots can have a repository class',
36
                $this->entityReflectionClass->name,
37
                AggregateRootInterface::class
38
            ));
39
        }
40
    }
41
42
    /**
43
     * @param mixed $item
44
     *
45
     * @throws \InvalidArgumentException
46
     */
47
    protected function checkType($item)
48
    {
49
        if (!is_object($item) || !$this->entityReflectionClass->isInstance($item)) {
50
            throw new \InvalidArgumentException(\sprintf(
51
                'Expected %s instance, instance of %s given',
52
                $this->entityReflectionClass->name,
53
                \is_object($item) ? \gettype($item) : \get_class($item)
54
            ));
55
        }
56
    }
57
}
58