ActionGalleryDelete   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 18
dl 0
loc 42
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B galleryDelete() 0 32 8
1
<?php
2
3
namespace Apps\Controller\Api\Content;
4
5
use Ffcms\Core\Exception\ForbiddenException;
6
use Ffcms\Core\Exception\NativeException;
7
use Ffcms\Core\Helper\FileSystem\File;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Arr;
10
use Ffcms\Core\Helper\Type\Str;
11
use Ffcms\Core\Network\Request;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionGalleryDelete
16
 * @package Apps\Controller\Api\Content
17
 * @property Request $request
18
 * @property Response $response
19
 * @method void setJsonHeader()
20
 */
21
trait ActionGalleryDelete
22
{
23
    /**
24
     * Remove items from gallery (preview+full)
25
     * @param string $id
26
     * @param string $file
27
     * @throws ForbiddenException
28
     * @throws NativeException
29
     * @return string
30
     */
31
    public function galleryDelete(string $id, ?string $file = null): ?string
32
    {
33
        $this->setJsonHeader();
34
        if (!$file || Any::isEmpty($file)) {
35
            $file = (string)$this->request->query->get('file', null);
36
        }
37
38
        // check passed data
39
        if (Any::isEmpty($file) || !Any::isInt($id)) {
40
            throw new NativeException('Wrong input data');
41
        }
42
43
        // check passed file extension
44
        $fileExt = Str::lastIn($file, '.', true);
45
        $fileName = Str::firstIn($file, '.');
46
        if (!Arr::in($fileExt, $this->allowedExt)) {
47
            throw new ForbiddenException('Wrong file extension');
48
        }
49
50
        // generate path
51
        $thumb = '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg';
52
        $full = '/upload/gallery/' . $id . '/orig/' . $file;
53
54
        // check if file exists and remove
55
        if (File::exist($thumb) || File::exist($full)) {
56
            File::remove($thumb);
57
            File::remove($full);
58
        } else {
59
            throw new NativeException('Image is not founded');
60
        }
61
62
        return json_encode(['status' => 1, 'msg' => 'Image is removed']);
63
    }
64
}
65