Completed
Push — master ( bfc0cc...f0e226 )
by sabaku
04:05
created

Video::callbackSignVerify()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 38
Code Lines 28

Duplication

Lines 18
Ratio 47.37 %

Importance

Changes 0
Metric Value
cc 5
eloc 28
nc 9
nop 0
dl 18
loc 38
rs 8.439
c 0
b 0
f 0
1
<?php
2
namespace Upyun;
3
4
class Video {
5
    /**
6
     * @var Config
7
     */
8
    protected $config;
9
10
    public function __construct(Config $bucketConfig) {
11
        $this->setConfig($bucketConfig);
12
    }
13
14
    public function setConfig(Config $bucketConfig) {
15
        $this->config = $bucketConfig;
16
    }
17
18
    public function pretreat($source, $notifyUrl, $tasks) {
19
        $postParams['tasks'] = Util::base64Json($tasks);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$postParams was never initialized. Although not strictly required by PHP, it is generally a good practice to add $postParams = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
20
        $postParams['source'] = $source;
21
        $postParams['notify_url'] = $notifyUrl;
22
        $postParams['bucket_name'] = $this->config->bucketName;
23
        $sign = Signature::getSignature(
24
            $this->config,
25
            $postParams,
26
            Signature::SIGN_VIDEO
27
        );
28
        
29
        $response = Request::post(
30
            sprintf('http://%s/%s/', Config::ED_VIDEO, 'pretreatment'),
31
            array('Authorization' => "UpYun {$this->config->operatorName}:$sign"),
32
            $postParams
33
        );
34
35 View Code Duplication
        if($response->status_code !== 200) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
36
            $body = json_decode($response->body, true);
37
            throw new \Exception(sprintf('%s, with x-request-id=%s', $body['msg'], $body['id']), $body['code']);
38
        }
39
40
41
        $taskIds = json_decode($response->body, true);
42
        return $taskIds;
43
    }
44
45
46
    public function status($taskIds) {
47
        $limit = 20;
48
        if(count($taskIds) <= $limit) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
49
            $taskIds = implode(',', $taskIds);
50
        } else {
51
            throw new \Exception('can not query more than ' . $limit . ' tasks at one time!');
52
        }
53
54
        $query['task_ids'] = $taskIds;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
55
        $query['bucket_name'] = $this->config->bucketName;
56
        $sign = Signature::getSignature(
57
            $this->config,
58
            $query,
59
            Signature::SIGN_VIDEO
60
        );
61
62
        $response = Request::get(
63
            sprintf('http://%s/%s/', Config::ED_VIDEO, 'status'),
64
            array('Authorization' => "UpYun {$this->config->operatorName}:$sign"),
65
            $query
66
        );
67
68 View Code Duplication
        if($response->status_code !== 200) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
69
            $body = json_decode($response->body, true);
70
            throw new \Exception(sprintf('%s, with x-request-id=%s', $body['msg'], $body['id']), $body['code']);
71
        }
72
73
        $status = json_decode($response->body, true);
74
        return $status;
75
    }
76
77
    public function callbackSignVerify() {
0 ignored issues
show
Coding Style introduced by
callbackSignVerify uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
78
        $callbackKeys = array(
79
            'bucket_name',
80
            'status_code',
81
            'path',
82
            'description',
83
            'task_id',
84
            'info',
85
            'signature',
86
        );
87
        $callbackParams = array();
88
        foreach($callbackKeys as $key) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
89
            if(isset($_POST[$key])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
90
               $callbackParams[$key] = Util::trim($_POST[$key]);
91
            }
92
        }
93
94 View Code Duplication
        if(isset($callbackParams['signature'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
95
            $sign = $callbackParams['signature'];
96
            unset($callbackParams['signature']);
97
            return $sign === Signature::getSignature(
98
                $this->config,
99
                $callbackParams,
100
                Signature::SIGN_VIDEO
101
            );
102
        }
103
104 View Code Duplication
        if(isset($data['non_signature'])) {
0 ignored issues
show
Bug introduced by
The variable $data seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
105
            $sign = $callbackParams['non_signature'];
106
            unset($callbackParams['non_signature']);
107
            return $sign === Signature::getSignature(
108
                $this->config,
109
                $callbackParams,
110
                Signature::SIGN_VIDEO_NO_OPERATOR
111
            );
112
        }
113
        return false;
114
    }
115
}