Completed
Push — develop-3.0 ( 4fe777...24fc5d )
by Mohamed
09:15
created

Updater   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 12.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 11
loc 91
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A delete() 0 7 1
A upload() 0 17 1
A remove() 0 7 1
A deleteFile() 11 12 3
A updateIssueToken() 0 4 1
A updateCommentToken() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Repository\Project\Issue\Attachment;
13
14
use Tinyissue\Contracts\Model\UserInterface;
15
use Tinyissue\Model\Project;
16
use Tinyissue\Repository\RepositoryUpdater;
17
18
class Updater extends RepositoryUpdater
19
{
20
    public function __construct(Project\Issue\Attachment $model)
21
    {
22
        $this->model = $model;
23
    }
24
25
    public function delete()
26
    {
27
        $path = $this->getUploadStorage($this->model->issue->project, $this->model->upload_token);
28
        $this->deleteFile($path, $this->model->filename);
29
30
        return parent::delete();
31
    }
32
33
    /**
34
     * Upload the attachment.
35
     *
36
     * @param array         $input
37
     * @param Project       $project
38
     * @param UserInterface $user
39
     *
40
     * @return Eloquent\Model
41
     */
42
    public function upload(array $input, Project $project, UserInterface $user)
43
    {
44
        // Upload path
45
        $path = $this->getUploadStorage($project, $input['upload_token']);
46
47
        /* @var $uploadedFile \Symfony\Component\HttpFoundation\File\UploadedFile */
48
        $uploadedFile = $input['upload'];
49
        $file = $uploadedFile->move($path, $uploadedFile->getClientOriginalName());
50
51
        $this->model->uploaded_by = $user->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Tinyissue\Contracts\Model\UserInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
52
        $this->model->filename = $file->getFilename();
53
        $this->model->fileextension = $file->getExtension();
54
        $this->model->filesize = $file->getSize();
55
        $this->model->upload_token = $input['upload_token'];
56
57
        return $this->save();
58
    }
59
60
    /**
61
     * Remove a attachment that is pending from a issue/comment.
62
     *
63
     * @param array         $input
64
     * @param Project       $project
65
     * @param UserInterface $user
66
     *
67
     * @return void
68
     */
69
    public function remove(array $input, Project $project, UserInterface $user)
70
    {
71
        $this->model->byUser($user)->forToken($input['upload_token'])->filename($input['filename'])->delete();
72
73
        $path = $this->getUploadStorage($project, $input['upload_token']);
74
        $this->deleteFile($path, $input['filename']);
75
    }
76
77
    /**
78
     * Delete the physical file of an attachment.
79
     *
80
     * @param string $path
81
     * @param string $filename
82
     */
83 View Code Duplication
    public function deleteFile($path, $filename)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    {
85
        $file = $path . '/' . $filename;
86
87
        if (file_exists($file)) {
88
            unlink($file);
89
        }
90
91
        if (is_dir($path)) {
92
            rmdir($path);
93
        }
94
    }
95
96
    public function updateIssueToken($token, $uploadBy, $issueId)
97
    {
98
        return $this->model->forToken($token)->byUser($uploadBy)->update(['issue_id' => $issueId]);
99
    }
100
101
    public function updateCommentToken($token, $uploadBy, $issueId, $commentId)
102
    {
103
        return $this->model->forToken($token)->byUser($uploadBy)->update([
104
            'issue_id'   => $issueId,
105
            'comment_id' => $commentId,
106
        ]);
107
    }
108
}
109