GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

File   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 152
Duplicated Lines 33.55 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 51
loc 152
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 20 20 1
A getById() 15 15 1
A getByHashedName() 16 16 1
B create() 0 31 3
A deleteById() 0 21 2

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
  * File Class
5
  *
6
  * @license    http://opensource.org/licenses/MIT The MIT License (MIT)
7
  * @author     Omar El Gabry <[email protected]>
8
  */
9
10
class File extends Model{
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
12
    /**
13
     * get all files.
14
     *
15
     * @access public
16
     * @param  integer  $pageNum
17
     * @return array
18
     *
19
     */
20 View Code Duplication
    public function getAll($pageNum = 1){
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...
21
22
        // get pagination object
23
        $pagination = Pagination::pagination("files", "", [], $pageNum);
24
        $offset     = $pagination->getOffset();
25
        $limit      = $pagination->perPage;
26
27
        $database   = Database::openConnection();
28
        $query  = "SELECT files.id AS id, files.filename, users.id AS user_id, users.name AS user_name, files.extension AS format, files.hashed_filename, files.date ";
29
        $query .= "FROM users, files ";
30
        $query .= "WHERE users.id = files.user_id ";
31
        $query .= "ORDER BY files.date DESC ";
32
        $query .= "LIMIT $limit OFFSET $offset";
33
34
        $database->prepare($query);
35
        $database->execute();
36
        $files = $database->fetchAllAssociative();
37
38
        return array("files" => $files, "pagination" => $pagination);
39
     }
40
41
    /**
42
     * get file by Id.
43
     *
44
     * @access public
45
     * @param  string  $fileId
46
     * @return array   Array holds the data of the file
47
     */
48 View Code Duplication
    public function getById($fileId){
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...
49
50
        $database = Database::openConnection();
51
        $query  = "SELECT files.id AS id, files.filename, users.id AS user_id, users.name AS user_name, files.extension AS format, files.hashed_filename, files.date ";
52
        $query .= "FROM users, files ";
53
        $query .= "WHERE files.id = :id ";
54
        $query .= "AND users.id = files.user_id LIMIT 1 ";
55
56
        $database->prepare($query);
57
        $database->bindValue(':id', (int)$fileId);
58
        $database->execute();
59
60
        $file = $database->fetchAllAssociative();
61
        return $file;
62
     }
63
64
    /**
65
     * get file by hashed name.
66
     * files are unique by the hashed file name(= hash(original filename . extension)).
67
     *
68
     * @access public
69
     * @param  string  $hashedFileName
70
     * @return array   Array holds the data of the file
71
     */
72 View Code Duplication
    public function getByHashedName($hashedFileName){
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...
73
74
        $database = Database::openConnection();
75
76
        $query  = "SELECT files.id AS id, files.filename, files.extension, files.hashed_filename ";
77
        $query .= "FROM  files ";
78
        $query .= "WHERE hashed_filename = :hashed_filename ";
79
        $query .= "LIMIT 1 ";
80
81
        $database->prepare($query);
82
        $database->bindValue(':hashed_filename', $hashedFileName);
83
        $database->execute();
84
85
        $file = $database->fetchAssociative();
86
        return $file;
87
    }
88
89
    /**
90
     * create file.
91
     *
92
     * @access public
93
     * @param  integer   $userId
94
     * @param  array     $fileData
95
     * @return array     Array holds the created file
96
     * @throws Exception If file couldn't be created
97
     */
98
    public function create($userId, $fileData){
99
100
        // upload
101
        $file = Uploader::uploadFile($fileData);
102
103
        if(!$file) {
104
            $this->errors = Uploader::errors();
105
            return false;
106
        }
107
108
        $database = Database::openConnection();
109
110
        $query = "INSERT INTO files (user_id, filename, hashed_filename, extension) VALUES (:user_id, :filename, :hashed_filename, :extension)";
111
112
        $database->prepare($query);
113
        $database->bindValue(':user_id', $userId);
114
        $database->bindValue(':filename', $file["filename"]);
115
        $database->bindValue(':hashed_filename', $file["hashed_filename"]);
116
        $database->bindValue(':extension', strtolower($file["extension"]));
117
        $database->execute();
118
119
        // if insert failed, then delete the file
120
        if($database->countRows() !== 1){
121
            Uploader::deleteFile(APP ."uploads/" . $file["basename"]);
122
            throw new Exception ("Couldn't upload file");
123
        }
124
125
        $fileId = $database->lastInsertedId();
126
        $file = $this->getById($fileId);
127
        return $file;
128
     }
129
130
    /**
131
     * deletes file.
132
     * This method overrides the deleteById() method in Model class.
133
     *
134
     * @access public
135
     * @param  array    $id
136
     * @throws Exception If failed to delete the file
137
     *
138
     */
139
    public function deleteById($id){
140
141
        $database = Database::openConnection();
142
143
        $database->getById("files", $id);
144
        $file = $database->fetchAssociative();
145
146
        // start a transaction to guarantee the file will be deleted from both; database and filesystem
147
        $database->beginTransaction();
148
        $database->deleteById("files", $id);
149
150
        if($database->countRows() !== 1){
151
            $database->rollBack();
152
            throw new Exception ("Couldn't delete file");
153
        }
154
155
        $basename = $file["hashed_filename"] . "." . $file["extension"];
156
        Uploader::deleteFile(APP ."uploads/" . $basename);
157
158
        $database->commit();
159
     }
160
161
 }