Completed
Push — master ( 91f8d9...64265e )
by Mohamed
09:45 queued 06:57
created

CrudTrait::deleteFile()   A

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 1
Bugs 0 Features 0
Metric Value
c 1
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\Project\Issue;
18
use Tinyissue\Model;
19
20
/**
21
 * CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Issue\Attachment model.
22
 *
23
 * @author Mohamed Alsharaf <[email protected]>
24
 *
25
 * @property int                $id
26
 * @property int                $uploaded_by
27
 * @property int                $issue_id
28
 * @property int                $comment_id
29
 * @property string             $filename
30
 * @property string             $fileextension
31
 * @property int                $filesize
32
 * @property string             $upload_token
33
 * @property Project\Issue      $issue
34
 *
35
 * @method   Eloquent\Model     save()
36
 * @method   Eloquent\Model     delete()
37
 * @method   Eloquent\Model     where()
38
 */
39
trait CrudTrait
40
{
41
    /**
42
     * Upload the attachment.
43
     *
44
     * @param array   $input
45
     * @param Project $project
46
     * @param User    $user
47
     *
48
     * @return Eloquent\Model
49
     */
50 3
    public function upload(array $input, Project $project, User $user)
51
    {
52 3
        $relativePath = '/' . config('tinyissue.uploads_dir') . '/' . $project->id . '/' . $input['upload_token'];
53 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...
54 3
        $path = config('filesystems.disks.local.root') . $relativePath;
55
56
        /* @var $uploadedFile \Symfony\Component\HttpFoundation\File\UploadedFile */
57 3
        $uploadedFile = $input['upload'];
58 3
        $file         = $uploadedFile->move($path, $uploadedFile->getClientOriginalName());
59
60 3
        $this->uploaded_by   = $user->id;
61 3
        $this->filename      = $file->getFilename();
62 3
        $this->fileextension = $file->getExtension();
63 3
        $this->filesize      = $file->getSize();
64 3
        $this->upload_token  = $input['upload_token'];
65
66 3
        return $this->save();
67
    }
68
69
    /**
70
     * Remove a attachment that is pending from a issue/comment.
71
     *
72
     * @param array   $input
73
     * @param Project $project
74
     * @param User    $user
75
     *
76
     * @return void
77
     */
78 1
    public function remove(array $input, Project $project, User $user)
79
    {
80 1
        $this->where('uploaded_by', '=', $user->id)
0 ignored issues
show
Unused Code introduced by
The call to CrudTrait::where() has too many arguments starting with 'uploaded_by'.

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...
81 1
            ->where('upload_token', '=', $input['upload_token'])
82 1
            ->where('filename', '=', $input['filename'])
83 1
            ->delete();
84
85 1
        $path = config('filesystems.disks.local.root') . '/' . config('tinyissue.uploads_dir') . '/' . $project->id . '/' . $input['upload_token'];
86 1
        $this->deleteFile($path, $input['filename']);
87 1
    }
88
89
    /**
90
     * Delete the physical file of an attachment.
91
     *
92
     * @param string $path
93
     * @param string $filename
94
     */
95 1
    public function deleteFile($path, $filename)
96
    {
97 1
        $file = $path . '/' . $filename;
98 1
        if (file_exists($file)) {
99 1
            unlink($file);
100
        }
101
102 1
        if (is_dir($path)) {
103 1
            rmdir($path);
104
        }
105 1
    }
106
}
107