Completed
Push — master ( 5d2be1...68f0c9 )
by Dominik
01:57
created

Resolver::lazyFind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
    public function __construct(ContainerInterface $container, array $repositoryKeys)
26
    {
27
        $this->container = $container;
28
        $this->repositoryKeys = $repositoryKeys;
29
    }
30
31
    /**
32
     * @param string $name
33
     * @param array  $arguments
34
     *
35
     * @return mixed
36
     */
37
    public function __call($name, $arguments)
38
    {
39
        $modelClass = array_shift($arguments);
40
41
        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
    public function find(string $modelClass, string $id = null)
51
    {
52
        if (null === $id) {
53
            return null;
54
        }
55
56
        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
    public function findOneBy(string $modelClass, array $criteria, array $orderBy = null)
67
    {
68
        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
    public function findBy(
81
        string $modelClass,
82
        array $criteria,
83
        array $orderBy = null,
84
        int $limit = null,
85
        int $offset = null
86
    ): array {
87
        return $this->getRepositoryByClass($modelClass)->findBy($criteria, $orderBy, $limit, $offset);
88
    }
89
90
    /**
91
     * @param ModelInterface $model
92
     */
93
    public function persist(ModelInterface $model)
94
    {
95
        $this->getRepositoryByClass(get_class($model))->persist($model);
96
    }
97
98
    /**
99
     * @param ModelInterface $model
100
     */
101
    public function remove(ModelInterface $model)
102
    {
103
        $this->getRepositoryByClass(get_class($model))->remove($model);
104
    }
105
106
    /**
107
     * @param string $modelClass
108
     *
109
     * @return RepositoryInterface
110
     */
111
    private function getRepositoryByClass(string $modelClass): RepositoryInterface
112
    {
113
        foreach ($this->repositoryKeys as $repositoryKey) {
114
            /** @var RepositoryInterface $repository */
115
            $repository = $this->container->get($repositoryKey);
116
            if ($repository->isResponsible($modelClass)) {
117
                return $repository;
118
            }
119
        }
120
121
        throw MissingRepositoryException::create($modelClass);
122
    }
123
}
124