Upload::image()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 2
nop 3
1
<?php
2
3
namespace WagnerMontanini\GoomerApi\Support;
4
5
use CoffeeCode\Uploader\File;
6
use CoffeeCode\Uploader\Image;
7
use CoffeeCode\Uploader\Media;
8
9
/**
10
 * Class Upload
11
 *
12
 * @author Wagner Montanini
13
 * @package WagnerMontanini\GoomerApi\Support
14
 */
15
class Upload
16
{
17
    /** @var Message */
18
    private $message;
19
20
    /**
21
     * Upload constructor.
22
     */
23
    public function __construct()
24
    {
25
        $this->message = new Message();
26
    }
27
28
    /**
29
     * @return Message
30
     */
31
    public function message(): Message
32
    {
33
        return $this->message;
34
    }
35
36
    /**
37
     * @param array $image
38
     * @param string $name
39
     * @param int $width
40
     * @return null|string
41
     * @throws \Exception
42
     */
43
    public function image(array $image, string $name, int $width = CONF_IMAGE_SIZE): ?string
44
    {
45
        $upload = new Image(CONF_UPLOAD_DIR, CONF_UPLOAD_IMAGE_DIR);
46
        if (empty($image['type']) || !in_array($image['type'], $upload::isAllowed())) {
47
            $this->message->error("Você não selecionou uma imagem válida");
48
            return null;
49
        }
50
51
        return str_replace(CONF_UPLOAD_DIR . "/", "", $upload->upload($image, $name, $width, CONF_IMAGE_QUALITY));
52
    }
53
54
    /**
55
     * @param array $file
56
     * @param string $name
57
     * @return null|string
58
     * @throws \Exception
59
     */
60
    public function file(array $file, string $name): ?string
61
    {
62
        $upload = new File(CONF_UPLOAD_DIR, CONF_UPLOAD_FILE_DIR);
63
        if (empty($file['type']) || !in_array($file['type'], $upload::isAllowed())) {
64
            $this->message->error("Você não selecionou um arquivo válido");
65
            return null;
66
        }
67
68
        return str_replace(CONF_UPLOAD_DIR . "/", "", $upload->upload($file, $name));
69
    }
70
71
    /**
72
     * @param array $media
73
     * @param string $name
74
     * @return null|string
75
     * @throws \Exception
76
     */
77
    public function media(array $media, string $name): ?string
78
    {
79
        $upload = new Media(CONF_UPLOAD_DIR, CONF_UPLOAD_MEDIA_DIR);
80
        if (empty($media['type']) || !in_array($media['type'], $upload::isAllowed())) {
81
            $this->message->error("Você não selecionou uma mídia válida");
82
            return null;
83
        }
84
85
        return str_replace(CONF_UPLOAD_DIR . "/", "", $upload->upload($media, $name));
86
    }
87
88
    /**
89
     * @param string $filePath
90
     */
91
    public function remove(string $filePath): void
92
    {
93
        if (file_exists($filePath) && is_file($filePath)) {
94
            unlink($filePath);
95
        }
96
    }
97
}