Passed
Push — master ( 30b5af...c8fd96 )
by Peter
04:21
created

File::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 15
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Service\Execute\Api;
6
7
use AbterPhp\Files\Domain\Entities\File as Entity;
8
use AbterPhp\Files\Domain\Entities\FileCategory;
9
use AbterPhp\Files\Orm\FileCategoryRepo;
10
use AbterPhp\Files\Orm\FileRepo as GridRepo;
11
use AbterPhp\Files\Validation\Factory\Api\File as ValidatorFactory;
12
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
13
use AbterPhp\Framework\Filesystem\Uploader;
14
use AbterPhp\Framework\Http\Service\Execute\RepoServiceAbstract;
15
use Cocur\Slugify\Slugify;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Http\Requests\UploadedFile;
18
use Opulence\Orm\IUnitOfWork;
19
use Opulence\Orm\OrmException;
20
21
class File extends RepoServiceAbstract
22
{
23
    const INPUT_NAME_FILE = 'file';
24
25
    /** @var Slugify */
26
    protected $slugify;
27
28
    /** @var FileCategoryRepo */
29
    protected $fileCategoryRepo;
30
31
    /** @var Uploader */
32
    protected $uploader;
33
34
    /**
35
     * File constructor.
36
     *
37
     * @param GridRepo         $repo
38
     * @param ValidatorFactory $validatorFactory
39
     * @param IUnitOfWork      $unitOfWork
40
     * @param IEventDispatcher $eventDispatcher
41
     * @param Slugify          $slugify
42
     * @param FileCategoryRepo $fileCategoryRepo
43
     * @param Uploader         $uploader
44
     */
45
    public function __construct(
46
        GridRepo $repo,
47
        ValidatorFactory $validatorFactory,
48
        IUnitOfWork $unitOfWork,
49
        IEventDispatcher $eventDispatcher,
50
        Slugify $slugify,
51
        FileCategoryRepo $fileCategoryRepo,
52
        Uploader $uploader
53
    ) {
54
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
55
56
        $this->slugify          = $slugify;
57
        $this->fileCategoryRepo = $fileCategoryRepo;
58
        $this->uploader         = $uploader;
59
    }
60
61
    /**
62
     * @param string[]       $postData
63
     * @param UploadedFile[] $fileData
64
     *
65
     * @return Entity
66
     * @throws OrmException
67
     */
68
    public function create(array $postData, array $fileData): IStringerEntity
69
    {
70
        $entity = $this->fillEntity($this->createEntity(''), $postData, $fileData);
71
72
        $this->uploadFile($entity, $fileData);
73
74
        if ($this->uploader->getErrors()) {
75
            return $entity;
76
        }
77
78
        $this->repo->add($entity);
79
80
        $this->commitCreate($entity);
81
82
        return $entity;
83
    }
84
85
    /**
86
     * @param Entity         $entity
87
     * @param string[]       $postData
88
     * @param UploadedFile[] $fileData
89
     *
90
     * @return bool
91
     * @throws OrmException
92
     */
93
    public function update(IStringerEntity $entity, array $postData, array $fileData): bool
94
    {
95
        $this->fillEntity($entity, $postData, $fileData);
96
97
        if (!empty($fileData)) {
98
            $this->deleteFile($entity);
99
            $this->uploadFile($entity, $fileData);
100
        }
101
102
        $this->commitUpdate($entity);
103
104
        return true;
105
    }
106
107
    /**
108
     * @param Entity $entity
109
     *
110
     * @return bool
111
     * @throws OrmException
112
     */
113
    public function delete(IStringerEntity $entity): bool
114
    {
115
        $this->deleteFile($entity);
116
117
        $this->repo->delete($entity);
118
119
        $this->commitDelete($entity);
120
121
        return true;
122
    }
123
124
    /**
125
     * @param Entity $entity
126
     */
127
    public function deleteFile(IStringerEntity $entity)
128
    {
129
        $this->uploader->delete($entity->getOldFilesystemName());
0 ignored issues
show
Bug introduced by
The method getOldFilesystemName() does not exist on AbterPhp\Framework\Domain\Entities\IStringerEntity. It seems like you code against a sub-type of AbterPhp\Framework\Domain\Entities\IStringerEntity such as AbterPhp\Files\Domain\Entities\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        $this->uploader->delete($entity->/** @scrutinizer ignore-call */ getOldFilesystemName());
Loading history...
130
    }
131
132
    /**
133
     * @param Entity         $entity
134
     * @param UploadedFile[] $fileData
135
     */
136
    public function uploadFile(Entity $entity, array $fileData)
137
    {
138
        $paths = $this->uploader->persist($fileData);
139
140
        if (!$paths) {
141
            return;
142
        }
143
144
        $entity->setFilesystemName($paths[static::INPUT_NAME_FILE]);
145
        $entity->setPublicName($fileData[static::INPUT_NAME_FILE]->getTempFilename());
146
    }
147
148
    /**
149
     * @param string $entityId
150
     *
151
     * @return Entity
152
     */
153
    public function createEntity(string $entityId): IStringerEntity
154
    {
155
        $fileCategory = new FileCategory('', '', '', false, []);
156
157
        return new Entity($entityId, '', '', '', $fileCategory, null);
158
    }
159
160
    /**
161
     * @param Entity         $entity
162
     * @param array          $postData
163
     * @param UploadedFile[] $fileData
164
     *
165
     * @return Entity
166
     * @throws OrmException
167
     */
168
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
169
    {
170
        $description = (string)$postData['description'];
171
172
        /** @var FileCategory $fileCategory */
173
        $fileCategory = $this->fileCategoryRepo->getById($postData['category_id']);
174
175
        $entity
176
            ->setDescription($description)
0 ignored issues
show
Bug introduced by
The method setDescription() does not exist on AbterPhp\Framework\Domain\Entities\IStringerEntity. It seems like you code against a sub-type of AbterPhp\Framework\Domain\Entities\IStringerEntity such as AbterPhp\Files\Domain\Entities\File or AbterPhp\Admin\Domain\Entities\ApiClient. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
            ->/** @scrutinizer ignore-call */ 
177
              setDescription($description)
Loading history...
177
            ->setCategory($fileCategory);
178
179
        if (array_key_exists('filesystem_name', $postData)) {
180
            $entity
181
                ->setFilesystemName($postData['filesystem_name'])
0 ignored issues
show
Bug introduced by
The method setFilesystemName() does not exist on AbterPhp\Framework\Domain\Entities\IStringerEntity. It seems like you code against a sub-type of AbterPhp\Framework\Domain\Entities\IStringerEntity such as AbterPhp\Files\Domain\Entities\File. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

181
                ->/** @scrutinizer ignore-call */ 
182
                  setFilesystemName($postData['filesystem_name'])
Loading history...
182
                ->setPublicName($postData['public_name']);
183
        }
184
185
        return $entity;
186
    }
187
}
188