Passed
Push — master ( 81f334...c86e8c )
by Mihail
03:35
created

ActionGalleryDelete   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
C galleryDelete() 0 33 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)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $file of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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)) {
0 ignored issues
show
Bug introduced by
The property allowedExt does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
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