Passed
Push — master ( f85738...3d85c1 )
by Peter
04:52
created

RepoServiceAbstract::commitCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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