Upload::response()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Erykai\Upload;
4
5
/**
6
 * execute upload
7
 */
8
class Upload extends Resource
9
{
10
    /**
11
     * @return bool
12
     */
13
    public function save(): bool
14
    {
15
        if($this->response()->type === 'error')
16
        {
17
            return false;
18
        }
19
        if(!$this->getFiles())
20
        {
21
            return false;
22
        }
23
        if($this->upload()) {
24
            $this->setResponse(200, "success", "upload performed successfully", "upload", $this->getData());
25
            return true;
26
        }
27
        return false;
28
    }
29
30
    /**
31
     * @param string|null $path
32
     * @return bool
33
     */
34
    public function delete(?string $path = null): bool
35
    {
36
        $dir = dirname(__DIR__, 4) . "/";
37
        if($path){
38
            if(!unlink($dir . $path))
39
            {
40
                $this->setResponse(404,"error","$path file not exist","upload",dynamic: $path);
41
                return false;
42
            }
43
        }
44
        foreach ($this->response()->data as $value) {
45
            unlink($dir . $value);
46
        }
47
        $this->setResponse(200,"success","file deleted successfully","upload");
48
        return true;
49
    }
50
51
    /**
52
     * @return object
53
     */
54
    public function response(): object
55
    {
56
        return $this->getResponse();
57
    }
58
}