CrudTrait::deleteFile()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 6
nc 4
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Tinyissue package.
5
 *
6
 * (c) Mohamed Alsharaf <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tinyissue\Model\Traits\Project\Issue\Attachment;
13
14
use Illuminate\Database\Eloquent;
15
use Tinyissue\Model\User;
16
use Tinyissue\Model\Project;
17
use Tinyissue\Model;
18
19
/**
20
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Issue\Attachment model.
21
 *
22
 * @author Mohamed Alsharaf <[email protected]>
23
 *
24
 * @property static $this
25
 */
26
trait CrudTrait
27
{
28
    /**
29
     * Upload the attachment.
30
     *
31
     * @param array   $input
32
     * @param Project $project
33
     * @param User    $user
34
     *
35
     * @return Eloquent\Model
36
     */
37 3
    public function upload(array $input, Project $project, User $user)
38
    {
39 3
        $relativePath = '/' . config('tinyissue.uploads_dir') . '/' . $project->id . '/' . $input['upload_token'];
40 3
        \Storage::disk('local')->makeDirectory($relativePath, 0777, true);
0 ignored issues
show
Unused Code introduced by
The call to Filesystem::makeDirectory() has too many arguments starting with 511.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
41 3
        $path = config('filesystems.disks.local.root') . $relativePath;
42
43
        /* @var $uploadedFile \Symfony\Component\HttpFoundation\File\UploadedFile */
44 3
        $uploadedFile = $input['upload'];
45 3
        $file         = $uploadedFile->move($path, $uploadedFile->getClientOriginalName());
46
47 3
        $this->uploaded_by   = $user->id;
0 ignored issues
show
Bug introduced by
The property uploaded_by 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...
48 3
        $this->filename      = $file->getFilename();
0 ignored issues
show
Bug introduced by
The property filename 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...
49 3
        $this->fileextension = $file->getExtension();
0 ignored issues
show
Bug introduced by
The property fileextension 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...
50 3
        $this->filesize      = $file->getSize();
0 ignored issues
show
Bug introduced by
The property filesize 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...
51 3
        $this->upload_token  = $input['upload_token'];
0 ignored issues
show
Bug introduced by
The property upload_token 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...
52
53 3
        return $this->save();
54
    }
55
56
    /**
57
     * Remove a attachment that is pending from a issue/comment.
58
     *
59
     * @param array   $input
60
     * @param Project $project
61
     * @param User    $user
62
     *
63
     * @return void
64
     */
65 1
    public function remove(array $input, Project $project, User $user)
66
    {
67 1
        $this->where('uploaded_by', '=', $user->id)
0 ignored issues
show
Bug introduced by
It seems like where() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
68 1
            ->where('upload_token', '=', $input['upload_token'])
69 1
            ->where('filename', '=', $input['filename'])
70 1
            ->delete();
71
72 1
        $path = config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $project->id . '/' . $input['upload_token'];
73 1
        $this->deleteFile($path, $input['filename']);
74 1
    }
75
76
    /**
77
     * Delete the physical file of an attachment.
78
     *
79
     * @param string $path
80
     * @param string $filename
81
     */
82 1
    public function deleteFile($path, $filename)
83
    {
84 1
        $file = $path . '/' . $filename;
85 1
        if (file_exists($file)) {
86 1
            unlink($file);
87
        }
88
89 1
        if (is_dir($path)) {
90 1
            rmdir($path);
91
        }
92 1
    }
93
94
    abstract public function save(array $options = []);
95
}
96