Resolver::findBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 9
ccs 2
cts 2
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Model;
6
7
use Interop\Container\ContainerInterface;
8
9
final class Resolver implements ResolverInterface
10
{
11
    /**
12
     * @var ContainerInterface|RepositoryInterface[]
13
     */
14
    private $container;
15
16
    /**
17
     * @var string[]|array
18
     */
19
    private $repositoryKeys;
20
21
    /**
22
     * @param ContainerInterface $container
23
     * @param array              $repositoryKeys
24
     */
25 9
    public function __construct(ContainerInterface $container, array $repositoryKeys)
26
    {
27 9
        $this->container = $container;
28 9
        $this->repositoryKeys = $repositoryKeys;
29 9
    }
30
31
    /**
32
     * @param string $name
33
     * @param array  $arguments
34
     *
35
     * @return mixed
36
     */
37 1
    public function __call($name, $arguments)
38
    {
39 1
        $modelClass = array_shift($arguments);
40
41 1
        return $this->getRepositoryByClass($modelClass)->$name(...$arguments);
42
    }
43
44
    /**
45
     * @param string      $modelClass
46
     * @param string|null $id
47
     *
48
     * @return ModelInterface|null
49
     */
50 3
    public function find(string $modelClass, string $id = null)
51
    {
52 3
        if (null === $id) {
53 1
            return null;
54
        }
55
56 2
        return $this->getRepositoryByClass($modelClass)->find($id);
57
    }
58
59
    /**
60
     * @param string     $modelClass
61
     * @param array      $criteria
62
     * @param array|null $orderBy
63
     *
64
     * @return ModelInterface|null
65
     */
66 1
    public function findOneBy(string $modelClass, array $criteria, array $orderBy = null)
67
    {
68 1
        return $this->getRepositoryByClass($modelClass)->findOneBy($criteria, $orderBy);
69
    }
70
71
    /**
72
     * @param string     $modelClass
73
     * @param array      $criteria
74
     * @param array|null $orderBy
75
     * @param int|null   $limit
76
     * @param int|null   $offset
77
     *
78
     * @return array
79
     */
80 1
    public function findBy(
81
        string $modelClass,
82
        array $criteria,
83
        array $orderBy = null,
84
        int $limit = null,
85
        int $offset = null
86
    ): array {
87 1
        return $this->getRepositoryByClass($modelClass)->findBy($criteria, $orderBy, $limit, $offset);
88
    }
89
90
    /**
91
     * @param ModelInterface $model
92
     */
93 2
    public function persist(ModelInterface $model)
94
    {
95 2
        $this->getRepositoryByClass(get_class($model))->persist($model);
96 2
    }
97
98
    /**
99
     * @param ModelInterface $model
100
     */
101 1
    public function remove(ModelInterface $model)
102
    {
103 1
        $this->getRepositoryByClass(get_class($model))->remove($model);
104 1
    }
105
106
    /**
107
     * @param string $modelClass
108
     *
109
     * @return RepositoryInterface
110
     */
111 8
    private function getRepositoryByClass(string $modelClass): RepositoryInterface
112
    {
113 8
        foreach ($this->repositoryKeys as $repositoryKey) {
114
            /** @var RepositoryInterface $repository */
115 7
            $repository = $this->container->get($repositoryKey);
116 7
            if ($repository->isResponsible($modelClass)) {
117 7
                return $repository;
118
            }
119
        }
120
121 1
        throw MissingRepositoryException::create($modelClass);
122
    }
123
}
124