PlayerRar2Zip::handle()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 49
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 49
rs 8.7972
cc 4
eloc 22
nc 4
nop 0
1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 *
7
 * Get all rar gamefiles and repack with zip
8
 */
9
10
namespace App\Console\Commands;
11
12
use App\Models\GamesFile;
13
use Illuminate\Console\Command;
14
15
class PlayerRar2Zip extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'player:rar2zip';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'convert rar files to zip';
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return mixed
45
     */
46
    public function handle()
47
    {
48
        //get all rar files from database
49
        $files = GamesFile::whereExtension('rar')->orderBy('filesize', 'asc')->get();
0 ignored issues
show
Bug introduced by
The method orderBy does only exist in Illuminate\Database\Query\Builder, but not in App\Models\GamesFile.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
50
51
        foreach ($files as $f) {
52
            //Check for maker engine 2=rm2k, 3=rm2k3, 9=rm2k3 Steam Edition
53
            if (! array_search($f->game->maker_id, [2, 3, 6, 9, 11]) === false) {
54
                echo "Gamefile: $f->filename";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $f instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
55
56
                //prepare the path variables
57
                $pathrar = storage_path('app/public/'.$f->filename); // Path to original rar file
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
58
                $pathzip = storage_path('app/public/'.str_replace('.rar', '.zip', $f->filename)); //Path to zip destination
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
59
                $pathdest = storage_path('app/public/games/'.$f->id.'/'); //destination for unrared files
60
61
                //delete old temp files (just in case.)
62
                $this->Delete($pathdest);
63
64
                // unrar the rar archive
65
                $command = 'unrar x \''.$pathrar.'\' '.$pathdest;
66
                exec($command);
67
68
                $handle = opendir($pathdest); // erm??
0 ignored issues
show
Unused Code introduced by
$handle is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
70
                //zip previous decompressed files
71
                $this->Zip($pathdest, $pathzip);
72
73
                // Check for zip file
74
                if(!file_exists($pathzip)){
75
                    // zip file does not exist. debug & die.
76
                    dd($pathzip);
77
                }
78
79
                //delete decomressed files
80
                $this->Delete($pathdest);
81
82
                //Update the Database
83
                $upd = GamesFile::whereId($f->id)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
84
                $upd->extension = 'zip';
85
                $upd->filename = str_replace('.rar', '.zip', $f->filename);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
86
                $upd->save();
87
88
                //delete rar file
89
                unlink($pathrar);
90
91
                echo " - done\n";
92
            }
93
        }
94
    }
95
96
    public function Delete($path)
97
    {
98
        // check for directory is not a file
99
        if (is_dir($path) === true) {
100
            // get all files and directorys recursive from all directorys
101
            $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::CHILD_FIRST);
102
103
            foreach ($files as $file) {
104
                // if file is . or .. skip
105
                if (in_array($file->getBasename(), ['.', '..']) !== true) {
106
                    // if $file is a directory
107
                    if ($file->isDir() === true) {
108
                        // delete directory
109
                        rmdir($file->getPathName());
110
                    } elseif (($file->isFile() === true) || ($file->isLink() === true)) { // or a file
111
                        // delete file
112
                        unlink($file->getPathname());
113
                    }
114
                }
115
            }
116
117
            return rmdir($path);
118
        } elseif ((is_file($path) === true) || (is_link($path) === true)) {
119
            return unlink($path);
120
        }
121
122
        return false;
123
    }
124
125
    public function Zip($source, $destination)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
126
    {
127
        // check for php_zip extension and file existance
128
        if (! extension_loaded('zip') || ! file_exists($source)) {
129
            return false;
130
        }
131
132
        // create ZipArchive Object
133
        $zip = new \ZipArchive();
134
135
        // Create a new ZIP
136
        if (! $zip->open($destination, \ZIPARCHIVE::CREATE)) {
137
            return false;
138
        }
139
140
        $source = str_replace('\\', '/', realpath($source));
141
142
        if (is_dir($source) === true) {
143
            $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
144
145
            foreach ($files as $file) {
146
                $file = str_replace('\\', '/', $file);
147
148
                // Ignore "." and ".." folders
149
                if (in_array(substr($file, strrpos($file, '/') + 1), ['.', '..'])) {
150
                    continue;
151
                }
152
153
                $file = realpath($file);
154
155
                if (is_dir($file) === true) {
156
                    $zip->addEmptyDir(str_replace($source.'/', '', $file.'/'));
157
                } elseif (is_file($file) === true) {
158
                    $zip->addFromString(str_replace($source.'/', '', $file), file_get_contents($file));
159
                }
160
            }
161
        } elseif (is_file($source) === true) {
162
            $zip->addFromString(basename($source), file_get_contents($source));
163
        }
164
165
        return $zip->close();
166
    }
167
}
168