AbstractEntityService::__construct()   A
last analyzed

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\event\Observable;
7
use WebComplete\core\utils\paginator\Paginator;
8
use WebComplete\core\utils\traits\TraitObservable;
9
10
abstract class AbstractEntityService implements EntityRepositoryInterface, Observable
11
{
12
    use TraitObservable;
13
14
    const EVENT_SAVE_BEFORE       = 'entity_save_before';
15
    const EVENT_SAVE_AFTER        = 'entity_save_after';
16
    const EVENT_DELETE_BEFORE     = 'entity_delete_before';
17
    const EVENT_DELETE_AFTER      = 'entity_delete_after';
18
    const EVENT_DELETE_ALL_BEFORE = 'entity_delete_all_before';
19
    const EVENT_DELETE_ALL_AFTER  = 'entity_delete_all_after';
20
21
    /**
22
     * @var EntityRepositoryInterface
23
     */
24
    protected $repository;
25
26
    /**
27
     * @param EntityRepositoryInterface $repository
28
     */
29
    public function __construct(EntityRepositoryInterface $repository)
30
    {
31
        $this->repository = $repository;
32
    }
33
34
    /**
35
     * Proxy method
36
     * @param \Closure $closure
37
     * @throws \Exception
38
     */
39
    public function transaction(\Closure $closure)
40
    {
41
        return $this->repository->transaction($closure);
42
    }
43
44
    /**
45
     * Proxy method
46
     * @return AbstractEntity
47
     */
48
    public function create(): AbstractEntity
49
    {
50
        return $this->repository->create();
51
    }
52
53
    /**
54
     * Proxy method
55
     * @param array $data
56
     *
57
     * @return AbstractEntity
58
     */
59
    public function createFromData(array $data): AbstractEntity
60
    {
61
        return $this->repository->createFromData($data);
62
    }
63
64
    /**
65
     * @param Paginator $paginator
66
     * @param Condition $condition
67
     * @return AbstractEntity[]
68
     */
69
    public function list(Paginator $paginator, Condition $condition): array
70
    {
71
        if (!$paginator->getTotal()) {
72
            if (!$total = $this->count($condition)) {
73
                return [];
74
            }
75
            $paginator->setTotal($total);
76
        }
77
        $condition->limit($paginator->getLimit());
78
        $condition->offset($paginator->getOffset());
79
        return $this->findAll($condition);
80
    }
81
82
    /**
83
     * Proxy method
84
     * @param $id
85
     * @return AbstractEntity|null
86
     */
87
    public function findById($id)
88
    {
89
        return $this->repository->findById($id);
90
    }
91
92
    /**
93
     * Proxy method
94
     * @param Condition $condition
95
     * @return AbstractEntity|null
96
     */
97
    public function findOne(Condition $condition)
98
    {
99
        return $this->repository->findOne($condition);
100
    }
101
102
    /**
103
     * Proxy method
104
     * @param Condition $condition
105
     * @return AbstractEntity[]
106
     */
107
    public function findAll(Condition $condition = null): array
108
    {
109
        return $this->repository->findAll($condition);
110
    }
111
112
    /**
113
     * Proxy method
114
     * @param Condition|null $condition
115
     * @return int
116
     */
117
    public function count(Condition $condition = null): int
118
    {
119
        return $this->repository->count($condition);
120
    }
121
122
    /**
123
     * @param AbstractEntity $item
124
     * @param array $oldData
125
     */
126
    public function save(AbstractEntity $item, array $oldData = [])
127
    {
128
        $eventData = ['item' => $item, 'oldData' => $oldData];
129
        $this->trigger(self::EVENT_SAVE_BEFORE, $eventData);
130
        $this->repository->save($item);
131
        $this->trigger(self::EVENT_SAVE_AFTER, $eventData);
132
    }
133
134
    /**
135
     * @param $id
136
     * @param AbstractEntity|null $item
137
     */
138
    public function delete($id, AbstractEntity $item = null)
139
    {
140
        $eventData = ['id' => $id, 'item' => $item];
141
        $this->trigger(self::EVENT_DELETE_BEFORE, $eventData);
142
        $this->repository->delete($id);
143
        $this->trigger(self::EVENT_DELETE_AFTER, $eventData);
144
    }
145
146
    /**
147
     * @param Condition|null $condition
148
     */
149
    public function deleteAll(Condition $condition = null)
150
    {
151
        $eventData = ['condition' => $condition];
152
        $this->trigger(self::EVENT_DELETE_ALL_BEFORE, $eventData);
153
        $this->repository->deleteAll($condition);
154
        $this->trigger(self::EVENT_DELETE_ALL_AFTER, $eventData);
155
    }
156
157
    /**
158
     * @param string $field
159
     * @param string $key
160
     * @param Condition|null $condition
161
     *
162
     * @return array
163
     * @throws \TypeError
164
     */
165
    public function getMap(string $field, string $key = 'id', Condition $condition = null): array
166
    {
167
        return $this->repository->getMap($field, $key, $condition);
168
    }
169
170
    /**
171
     * @param array $conditions
172
     *
173
     * @return Condition
174
     */
175
    public function createCondition(array $conditions = []): Condition
176
    {
177
        return $this->repository->createCondition($conditions);
178
    }
179
}
180