DumpDAO::addDumpToMedia()   C
last analyzed

Complexity

Conditions 17
Paths 41

Size

Total Lines 111
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 53
nc 41
nop 8
dl 0
loc 111
rs 5.2166
c 0
b 0
f 0

How to fix   Long Method    Complexity    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../Model/Dump/Dump.php" ;
6
require_once __DIR__."/../Model/User/User.php" ;
7
require_once __DIR__."/../../vendor/autoload.php";
8
9
/**
10
 * DAO for Dump
11
 */
12
class DumpDAO {
13
    private $mysqli;
14
15
    const FORMAT_STX = 'STX';
16
    const FORMAT_MSA = 'MSA';
17
    const FORMAT_RAW = 'RAW';
18
    const FORMAT_SCP = 'SCP';
19
20
    public function __construct($mysqli) {
21
        $this->mysqli = $mysqli;
22
    }
23
24
    /**
25
     * Get all the formats
26
     * @return String[] A list of formats
27
     */
28
    public function getFormats() {
29
        return array(
30
            DumpDAO::FORMAT_STX,
31
            DumpDAO::FORMAT_MSA,
32
            DumpDAO::FORMAT_RAW,
33
            DumpDAO::FORMAT_SCP
34
        );
35
    }
36
37
     /**
38
     * Add a dump to a media
39
     *
40
     * @param int media_id
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\media_id was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
     * @param varchar format
42
     * @param varchar file name
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\file was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     * @param text info
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\info was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
     */
45
    public function addDumpToMedia(
46
        $media_id,
47
        $format,
48
        $file_name,
49
        $tempfilename,
50
        $info,
51
        $game_file_temp_path,
52
        $game_file_path,
53
        $filesize
54
    ) {
55
        //check the extension of the file, is it zipped or not?
56
        $file_ext = strrchr($file_name, ".");
57
        $file_ext = explode(".", $file_ext);
58
        $file_ext = strtolower($file_ext[1]);
59
60
        // If it is ZIP, do all the zippidy zip stuff
61
        if ($file_ext == 'zip') {
62
            // Time for zip magic
63
            $zip = new \PclZip("$tempfilename");
0 ignored issues
show
Bug introduced by
The type PclZip was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
64
65
            // Obtain the contentlist of the zip file.
66
            if (($list = $zip->listContent()) == 0) {
67
                die("Error : " . $zip->errorInfo(true));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
68
            }
69
70
            // Get the filename from the returned array
71
            $filename = $list[0]['filename'];
72
73
            // Split the filename to get the extention
74
            $ext = strrchr($filename, ".");
75
76
            // Get rid of the . in the extention
77
            $ext = explode(".", $ext);
78
79
            // convert to lowercase just incase....
80
            $ext = strtolower($ext[1]);
81
82
            // check if the extention is valid.
83
            if ($ext == "stx" || $ext == "msa" || $ext == "st"  || $file_ext == "scp") { // pretty isn't it? ;)
84
            } else {
85
                exit("Try uploading a diskimage type that is allowed, like stx or msa not $ext");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
86
            }
87
        } else {
88
            if ($file_ext == "stx" || $file_ext == "msa" ||
89
                $file_ext == "st" || $file_ext == "scp") { // pretty isn't it? ;)
90
            } else {
91
                exit("Try uploading a diskimage type that is allowed $file_ext");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
92
            }
93
        }
94
95
        // create a timestamp for the date of upload
96
        $timestamp = time();
97
98
        $stmt = \AL\Db\execute_query(
0 ignored issues
show
Unused Code introduced by
The assignment to $stmt is dead and can be removed.
Loading history...
99
            "DumpDAO: addDumptoMedia",
100
            $this->mysqli,
101
            "INSERT INTO dump (`media_id`, `format`, `info`, `date`, `size`, user_id ) VALUES (?, ?, ?, ?, ?, ?)",
102
            "issiii", $media_id, $format, $info, $timestamp, $filesize, $_SESSION['user_id']
103
        );
104
105
        //get the new dump id
106
        $new_dump_id = $this->mysqli->insert_id;
107
108
        if ($file_ext == 'zip') {
109
            // Time to unzip the file to the temporary directory
110
            $archive = new \PclZip("$tempfilename");
111
112
            if ($archive->extract(PCLZIP_OPT_PATH, "$game_file_temp_path") == 0) {
0 ignored issues
show
Bug introduced by
The constant AL\Common\DAO\PCLZIP_OPT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
113
                die("Error : " . $archive->errorInfo(true));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
114
            }
115
116
            // rename diskimage to increment number
117
            rename("$game_file_temp_path$filename", "$game_file_temp_path$new_dump_id.$ext")
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filename does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ext does not seem to be defined for all execution paths leading up to this point.
Loading history...
118
                or die("couldn't rename the file");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
119
        } else {
120
            $file_data = rename("$tempfilename", "$game_file_temp_path$new_dump_id.$file_ext");
0 ignored issues
show
Unused Code introduced by
The assignment to $file_data is dead and can be removed.
Loading history...
121
        }
122
123
        //Time to rezip file and place it in the proper location.
124
        $archive = new \PclZip("$game_file_path$new_dump_id.zip");
125
        if ($file_ext == 'zip') {
126
            $v_list  = $archive->create("$game_file_temp_path$new_dump_id.$ext", PCLZIP_OPT_REMOVE_ALL_PATH);
0 ignored issues
show
Bug introduced by
The constant AL\Common\DAO\PCLZIP_OPT_REMOVE_ALL_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
127
        } else {
128
            $v_list  = $archive->create("$game_file_temp_path$new_dump_id.$file_ext", PCLZIP_OPT_REMOVE_ALL_PATH);
129
        }
130
        if ($v_list == 0) {
131
            die("Error : " . $archive->errorInfo(true));
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
132
        }
133
134
        // Time to do the safeties, here we do a sha512 file hash that we later enter into
135
        // the database, this will be used in the download function to check everytime the file
136
        // is being downloaded... if the hashes don't match, then datacorruption have changed the file.
137
        $crc = openssl_digest("$game_file_path$new_dump_id.zip", 'sha512');
138
139
        $stmt = \AL\Db\execute_query(
140
            "DumpDAO: addDumptoMedia",
141
            $this->mysqli,
142
            "UPDATE dump SET sha512 = ? WHERE id = ?",
143
            "si", $crc, $new_dump_id
144
        );
145
146
        // Chmod file so that we can backup/delete files through ftp.
147
        chmod("$game_file_path$new_dump_id.zip", 0777);
148
149
        // Delete the unzipped file in the temporary directory
150
        if ($file_ext == 'zip') {
151
            unlink("$game_file_temp_path$new_dump_id.$ext");
152
        } else {
153
            unlink("$game_file_temp_path$new_dump_id.$file_ext");
154
        }
155
        $stmt->close();
156
    }
157
158
    /**
159
     * Get all dumps from media
160
     *
161
     * @return \AL\Common\Model\Dump\Dump[] An array of dumps
162
     */
163
    public function getAllDumpsFromMedia($media_id) {
164
        $stmt = \AL\Db\execute_query(
165
            "DumpDAO: getAllDumpsFromMedia",
166
            $this->mysqli,
167
            "SELECT id, format, sha512, date, size, info, dump.user_id, users.userid FROM dump
168
            LEFT JOIN users ON (users.user_id = dump.user_id)
169
            WHERE media_id = ?",
170
            "i", $media_id
171
        );
172
173
        \AL\Db\bind_result(
174
            "DumpDAO: getAllDumpsFromMedia",
175
            $stmt,
176
            $id, $format, $sha512, $date, $size, $info, $user_id, $userid
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $sha512 seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $size seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $info seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $format seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $user_id seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable $userid seems to be never defined.
Loading history...
177
        );
178
179
        $dump = [];
180
        while ($stmt->fetch()) {
181
            $dump[] = new \AL\Common\Model\Dump\Dump(
182
                $id, $media_id, $format, $sha512, $date, $size, $info,
183
                new \AL\Common\Model\User\User($user_id, $userid, null, null, null, null, null)
184
            );
185
        }
186
187
        $stmt->close();
188
189
        return $dump;
190
    }
191
192
      /**
193
     * delete a dump from a media
194
     *
195
     * @param int dump_id
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\dump_id was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
196
     */
197
    public function deleteDumpFromMedia($dump_id, $game_file_path) {
198
        $stmt = \AL\Db\execute_query(
199
            "DumpDAO: deleteDumpFromMedia",
200
            $this->mysqli,
201
            "DELETE FROM dump WHERE id = ?",
202
            "i", $dump_id
203
        );
204
205
        unlink("$game_file_path$dump_id.zip");
206
207
        $stmt->close();
208
    }
209
}
210