Completed
Push — master ( 43cbf0...3a1ac6 )
by Maxim
03:36
created

AbstractEntityService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\core\entity;
4
5
use WebComplete\core\condition\Condition;
6
use WebComplete\core\utils\paginator\Paginator;
7
8
abstract class AbstractEntityService implements EntityRepositoryInterface
9
{
10
11
    /**
12
     * @var EntityRepositoryInterface
13
     */
14
    protected $repository;
15
16
    /**
17
     * @param EntityRepositoryInterface $repository
18
     */
19
    public function __construct(EntityRepositoryInterface $repository)
20
    {
21
        $this->repository = $repository;
22
    }
23
24
    /**
25
     * Proxy method
26
     * @param \Closure $closure
27
     * @throws \Exception
28
     */
29
    public function transaction(\Closure $closure)
30
    {
31
        return $this->repository->transaction($closure);
32
    }
33
34
    /**
35
     * Proxy method
36
     * @return AbstractEntity
37
     */
38
    public function create(): AbstractEntity
39
    {
40
        return $this->repository->create();
41
    }
42
43
    /**
44
     * Proxy method
45
     * @param array $data
46
     *
47
     * @return AbstractEntity
48
     */
49
    public function createFromData(array $data): AbstractEntity
50
    {
51
        return $this->repository->createFromData($data);
52
    }
53
54
    /**
55
     * @param Paginator $paginator
56
     * @param Condition $condition
57
     * @return AbstractEntity[]
58
     */
59
    public function list(Paginator $paginator, Condition $condition): array
60
    {
61
        if (!$paginator->getTotal()) {
62
            if (!$total = $this->count($condition)) {
63
                return [];
64
            }
65
            $paginator->setTotal($total);
66
        }
67
        $condition->limit($paginator->getLimit());
68
        $condition->offset($paginator->getOffset());
69
        return $this->findAll($condition);
70
    }
71
72
    /**
73
     * Proxy method
74
     * @param $id
75
     * @return AbstractEntity|null
76
     */
77
    public function findById($id)
78
    {
79
        return $this->repository->findById($id);
80
    }
81
82
    /**
83
     * Proxy method
84
     * @param Condition $condition
85
     * @return AbstractEntity|null
86
     */
87
    public function findOne(Condition $condition)
88
    {
89
        return $this->repository->findOne($condition);
90
    }
91
92
    /**
93
     * Proxy method
94
     * @param Condition $condition
95
     * @return AbstractEntity[]
96
     */
97
    public function findAll(Condition $condition = null): array
98
    {
99
        return $this->repository->findAll($condition);
100
    }
101
102
    /**
103
     * Proxy method
104
     * @param Condition|null $condition
105
     * @return int
106
     */
107
    public function count(Condition $condition = null): int
108
    {
109
        return $this->repository->count($condition);
110
    }
111
112
    /**
113
     * Proxy method
114
     * @param AbstractEntity $item
115
     */
116
    public function save(AbstractEntity $item)
117
    {
118
        return $this->repository->save($item);
119
    }
120
121
    /**
122
     * Proxy method
123
     * @param $id
124
     */
125
    public function delete($id)
126
    {
127
        return $this->repository->delete($id);
128
    }
129
130
    /**
131
     * @param Condition|null $condition
132
     *
133
     * @return mixed
134
     */
135
    public function deleteAll(Condition $condition = null)
136
    {
137
        return $this->repository->deleteAll($condition);
138
    }
139
140
    /**
141
     * @param string $field
142
     * @param string $key
143
     * @param Condition|null $condition
144
     *
145
     * @return array
146
     * @throws \TypeError
147
     */
148
    public function getMap(string $field, string $key = 'id', Condition $condition = null): array
149
    {
150
        return $this->repository->getMap($field, $key, $condition);
151
    }
152
153
    /**
154
     * @param array $conditions
155
     *
156
     * @return Condition
157
     */
158
    public function createCondition(array $conditions = []): Condition
159
    {
160
        return $this->repository->createCondition($conditions);
161
    }
162
}
163