UploadProcessor::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 14
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Itstructure\MFU\Processors;
4
5
use Exception;
6
use Itstructure\MFU\Interfaces\HasOwnerInterface;
7
use Illuminate\Support\Facades\Storage;
8
9
/**
10
 * Class UploadProcessor
11
 * @package Itstructure\MFU\Processors
12
 * @author Andrey Girnik <[email protected]>
13
 */
14
class UploadProcessor extends SaveProcessor
15
{
16
    /********************** PROCESS INTERNAL METHODS *********************/
17
    protected function isFileRequired(): bool
18
    {
19
        return true;
20
    }
21
22
    /**
23
     * @return void
24
     * @throws Exception
25
     */
26
    protected function setProcessParams(): void
27
    {
28
        $this->currentDisk = Storage::getDefaultDriver();
29
30
        $this->processDirectory = $this->getNewProcessDirectory();
31
32
        $this->outFileName = $this->getNewOutFileName();
33
34
        $this->path = $this->processDirectory . '/' . $this->outFileName;
35
    }
36
37
    /**
38
     * @return bool
39
     * @throws Exception
40
     */
41
    protected function process(): bool
42
    {
43
        if (!$this->sendFile()) {
44
            throw new \Exception('Error upload file.');
45
        }
46
47
        $this->setMediafileBaseData();
48
        $this->setMediafileMetaData();
49
50
        if (!$this->mediafileModel->save()) {
51
            throw new \Exception('Error save file data in database.');
52
        }
53
        $this->mediafileModel->refresh();
54
        return true;
55
    }
56
57
    protected function afterProcess(): void
58
    {
59
        if (!empty($this->data['owner_id']) && !empty($this->data['owner_name']) && !empty($this->data['owner_attribute'])) {
60
            $this->addOwner($this->mediafileModel, $this->data['owner_id'], $this->data['owner_name'], $this->data['owner_attribute']);
61
        }
62
    }
63
64
    /**
65
     * @param HasOwnerInterface $mediafileModel
66
     * @param int $ownerId
67
     * @param string $ownerName
68
     * @param string $ownerAttribute
69
     */
70
    protected function addOwner(HasOwnerInterface $mediafileModel, int $ownerId, string $ownerName, string $ownerAttribute): void
71
    {
72
        $mediafileModel->addOwner($ownerId, $ownerName, $ownerAttribute);
73
    }
74
}
75