Passed
Push — master ( 54de5e...97aeae )
by Peter
02:25
created

RepoServiceAbstract::retrieveList()   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 5
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Http\Service\Execute;
6
7
use AbterPhp\Framework\Constant\Event;
8
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
9
use AbterPhp\Framework\Events\EntityChange;
10
use AbterPhp\Framework\Orm\IGridRepo;
11
use Opulence\Events\Dispatchers\IEventDispatcher;
12
use Opulence\Http\Requests\UploadedFile;
13
use Opulence\Orm\IUnitOfWork;
14
use Opulence\Orm\OrmException;
15
use Opulence\Validation\Factories\IValidatorFactory;
16
use Opulence\Validation\IValidator;
17
18
abstract class RepoServiceAbstract implements IRepoService
19
{
20
    /** @var IGridRepo */
21
    protected $repo;
22
23
    /** @var IValidatorFactory */
24
    protected $validatorFactory;
25
26
    /** @var IUnitOfWork */
27
    protected $unitOfWork;
28
29
    /** @var IEventDispatcher */
30
    protected $eventDispatcher;
31
32
    /** @var IValidator */
33
    protected $validator;
34
35
    /**
36
     * RepoExecuteAbstract constructor.
37
     *
38
     * @param IGridRepo         $repo
39
     * @param IValidatorFactory $validatorFactory
40
     * @param IUnitOfWork       $unitOfWork
41
     * @param IEventDispatcher  $eventDispatcher
42
     */
43
    public function __construct(
44
        IGridRepo $repo,
45
        IValidatorFactory $validatorFactory,
46
        IUnitOfWork $unitOfWork,
47
        IEventDispatcher $eventDispatcher
48
    ) {
49
        $this->repo             = $repo;
50
        $this->validatorFactory = $validatorFactory;
51
        $this->unitOfWork       = $unitOfWork;
52
        $this->eventDispatcher  = $eventDispatcher;
53
    }
54
55
    /**
56
     * @param array $postData
57
     *
58
     * @return array
59
     */
60
    public function validateForm(array $postData): array
61
    {
62
        if ($this->getValidator()->isValid($postData)) {
63
            return [];
64
        }
65
66
        return $this->validator->getErrors()->getAll();
67
    }
68
69
    /**
70
     * @return IValidator
71
     */
72
    protected function getValidator(): IValidator
73
    {
74
        if ($this->validator) {
75
            return $this->validator;
76
        }
77
78
        $this->validator = $this->validatorFactory->createValidator();
79
80
        return $this->validator;
81
    }
82
83
    /**
84
     * @param string[]       $postData
85
     * @param UploadedFile[] $fileData
86
     *
87
     * @return IStringerEntity
88
     * @throws OrmException
89
     */
90
    public function create(array $postData, array $fileData): IStringerEntity
91
    {
92
        $entity = $this->fillEntity($this->createEntity(''), $postData, $fileData);
93
94
        $this->repo->add($entity);
95
96
        $this->commitCreate($entity);
97
98
        return $entity;
99
    }
100
101
    /**
102
     * @param IStringerEntity $entity
103
     * @param string[]        $postData
104
     * @param UploadedFile[]  $fileData
105
     *
106
     * @return bool
107
     * @throws OrmException
108
     */
109
    public function update(IStringerEntity $entity, array $postData, array $fileData): bool
110
    {
111
        $this->fillEntity($entity, $postData, $fileData);
112
113
        $this->commitUpdate($entity);
114
115
        return true;
116
    }
117
118
    /**
119
     * @param IStringerEntity $entity
120
     *
121
     * @return bool
122
     * @throws OrmException
123
     */
124
    public function delete(IStringerEntity $entity): bool
125
    {
126
        $this->repo->delete($entity);
127
128
        $this->commitDelete($entity);
129
130
        return true;
131
    }
132
133
    /**
134
     * @param IStringerEntity $entity
135
     *
136
     * @throws OrmException
137
     */
138
    protected function commitCreate(IStringerEntity $entity)
139
    {
140
        $event = new EntityChange($entity, Event::ENTITY_CREATE);
141
142
        $this->eventDispatcher->dispatch(Event::ENTITY_PRE_CHANGE, $event);
143
144
        $this->unitOfWork->commit();
145
146
        $this->eventDispatcher->dispatch(Event::ENTITY_POST_CHANGE, $event);
147
    }
148
149
    /**
150
     * @param IStringerEntity $entity
151
     *
152
     * @throws OrmException
153
     */
154
    protected function commitUpdate(IStringerEntity $entity)
155
    {
156
        $event = new EntityChange($entity, Event::ENTITY_UPDATE);
157
158
        $this->eventDispatcher->dispatch(Event::ENTITY_PRE_CHANGE, $event);
159
160
        $this->unitOfWork->commit();
161
162
        $this->eventDispatcher->dispatch(Event::ENTITY_POST_CHANGE, $event);
163
    }
164
165
    /**
166
     * @param IStringerEntity $entity
167
     *
168
     * @throws OrmException
169
     */
170
    protected function commitDelete(IStringerEntity $entity)
171
    {
172
        $event = new EntityChange($entity, Event::ENTITY_DELETE);
173
174
        $this->eventDispatcher->dispatch(Event::ENTITY_PRE_CHANGE, $event);
175
176
        $this->unitOfWork->commit();
177
178
        $this->eventDispatcher->dispatch(Event::ENTITY_POST_CHANGE, $event);
179
    }
180
181
    /**
182
     * @param string $entityId
183
     *
184
     * @return IStringerEntity
185
     * @throws OrmException
186
     */
187
    public function retrieveEntity(string $entityId): IStringerEntity
188
    {
189
        /** @var IStringerEntity $entity */
190
        $entity = $this->repo->getById($entityId);
191
192
        return $entity;
193
    }
194
195
    /**
196
     * @param int      $offset
197
     * @param int      $limit
198
     * @param string[] $orders
199
     * @param array    $conditions
200
     * @param array    $params
201
     *
202
     * @return IStringerEntity[]
203
     */
204
    public function retrieveList(int $offset, int $limit, array $orders, array $conditions, array $params): array
205
    {
206
        return $this->repo->getPage($offset, $limit, $orders, $conditions, $params);
207
    }
208
209
    /**
210
     * @param string $entityId
211
     *
212
     * @return IStringerEntity
213
     */
214
    abstract public function createEntity(string $entityId): IStringerEntity;
215
216
    /**
217
     * @param IStringerEntity $entity
218
     * @param array           $postData
219
     * @param UploadedFile[]  $fileData
220
     *
221
     * @return IStringerEntity
222
     */
223
    abstract protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity;
224
}
225