Repository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A checkType() 0 10 4
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