Completed
Pull Request — master (#177)
by
unknown
11:40
created

BucketExporter::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 4
nop 3
dl 0
loc 10
rs 9.4285
1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Auth;
5
use Qiniu\Config;
6
use Qiniu\Http\Client;
7
use Qiniu\Http\Error;
8
use Qiniu\Storage\BucketManager;
9
10
/**
11
 * Desc   : A simple file export tool based on BucketManager,Generate download command with php,and download with wget
12
 * Author : [email protected]
13
 *
14
 */
15
final class BucketExporter
16
{
17
    private $auth;
18
19
    public function __construct(Auth $auth,$bucket,$logPath=null)
20
    {
21
        $this->auth    = $auth;
22
        $this->bucket  = $bucket;
0 ignored issues
show
Bug introduced by
The property bucket does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
        $this->logPath = $logPath ? $logPath : '/tmp/log/qiniu_log/';
0 ignored issues
show
Bug introduced by
The property logPath does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
        $this->runningTime = date('Y-m-d_H-i-s');
0 ignored issues
show
Bug introduced by
The property runningTime does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
25
        $this->bucketItemListFilePrefix = $this->logPath.$this->bucket.'_itemLists_'.$this->runningTime;
0 ignored issues
show
Bug introduced by
The property bucketItemListFilePrefix does not seem to exist. Did you mean bucket?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
26
        $this->cmdFilePrefix            = $this->logPath.$this->bucket.'_syncCmd_'.$this->runningTime;
0 ignored issues
show
Bug introduced by
The property cmdFilePrefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        file_exists($this->logPath) || mkdir($this->logPath,0777,true);
28
    }
29
30
    public function exportBucketitem()
31
    {
32
        $bucketMgr = new BucketManager($this->auth);
33
        $marker = null;
34
        $prefix = null;
35
        $limit = 10;
36
        $delimiter = null;
0 ignored issues
show
Unused Code introduced by
$delimiter 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...
37
38
        for($i=0;;$i++)
39
        {
40
            list($item, $marker, $err) = $bucketMgr->listFiles($this->bucket, $prefix, $marker, $limit);//may be here can replace by marker parameter
0 ignored issues
show
Unused Code introduced by
The assignment to $err is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
41
            $itemLists='';
42
            $t=0;
43
            foreach($item as $k=>$v)
44
            {
45
                $itemLists.=$v['key']."\n";
46
                $t++;
47
            }
48
            $itemListFile=$this->bucketItemListFilePrefix.'_'.$i.'.txt';
0 ignored issues
show
Bug introduced by
The property bucketItemListFilePrefix does not seem to exist. Did you mean bucket?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
49
            file_put_contents($itemListFile,$itemLists,FILE_APPEND);
50
51
            if($t<$limit)
52
            {
53
                $count=$i*$limit+$t;
54
                $data=array(
55
                            'msg'=>'Dump over ,Total '.$count.' item !',
56
                            'count'=>$count,
57
                            'export_file'=>$itemListFile,
58
                            );
59
                return $data;
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61
            }
62
        }
63
    }
64
65
    public function exportFileTree($bucketDomain,$basePath=null)
66
    {
67
        $cmdFiles=array();
68
        for($i=0;;$i++)
69
        {
70
            $itemListFile=$this->bucketItemListFilePrefix.'_'.$i.'.txt';
0 ignored issues
show
Bug introduced by
The property bucketItemListFilePrefix does not seem to exist. Did you mean bucket?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
71
            if(!file_exists ($itemListFile))
72
            {
73
                break;
74
            }
75
            $item = file($itemListFile);
76
            $syncCmd='';
77
            foreach($item as $k=>$v)
78
            {
79
                if(strlen($v)<1)
80
                {
81
                    continue;//Sometimes ,Item name is empty;Skip It;
82
                }
83
                $v          = str_replace("\n","",$v);
84
                $path_info  = pathinfo($v);
85
                $base_path  = $basePath?$basePath:__DIR__;
86
                $local_path = str_replace('\\','/',$base_path.'/'.$path_info['dirname']);
87
                file_exists($local_path) || mkdir($local_path,0777,true);
88
89
                if(!file_exists($local_path.'/'.$path_info['basename']))//这里也许可以通过marker遍历位置替代
90
                {
91
                    $syncCmd.='wget \''.$bucketDomain.$v.'\' -O '.$local_path.'/'.$path_info['basename'].';'."\n";
92
                }
93
94
            }
95
            $cmdFIle=$itemListFile=$this->cmdFilePrefix.'_'.$i.'.txt';
0 ignored issues
show
Unused Code introduced by
$itemListFile 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...
96
            file_put_contents($cmdFIle,$syncCmd,FILE_APPEND);
97
            $cmdFiles[]=$cmdFIle;
98
        }
99
        return $cmdFiles;
100
    }
101
102
    public function syncBucketToLocal($bucketDomain,$basePath)
103
    {
104
        $this->exportBucketitem();
105
        $cmdFile=$this->exportFileTree($bucketDomain,$basePath);
106
        return $cmdFile;
107
    }
108
109
}
110