Completed
Pull Request — master (#37)
by
unknown
01:14
created

CreateTempUrlKey::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sausin\LaravelOvh\Commands;
4
5
use Illuminate\Console\Command;
6
7
class CreateTempUrlKey extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'ovh:create-temp-url-key';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Generate the temp url key, allowing the use of Storage::temporaryUrl()';
22
23
    /**
24
     * Create a new command instance.
25
     *
26
     * @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...
27
     */
28
    public function __construct()
29
    {
30
        parent::__construct();
31
    }
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38
    public function handle()
39
    {
40
        // generate the key with sha512sum of time()
41
42
        $this->info("read https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-key for explanation of what we're doing here");
43
44
        $temp_url_key = hash('sha512', time());
45
        $client = new \GuzzleHttp\Client();
46
47
        // ( https://docs.ovh.com/gb/en/public-cloud/managing_tokens/ )
48
        // Request token creation
49
        //  curl -X POST ${OS_AUTH_URL}auth/tokens -H "Content-Type: application/json" -d ' { "auth": { "identity": { "methods": ["password"], "password": { "user": { "name": "'$OS_USERNAME'", "domain": { "id": "default" }, "password": "'$OS_PASSWORD'" } } }, "scope": { "project": { "name": "'$OS_TENANT_NAME'", "domain": { "id": "default" } } } } }' | python -mjson.tool
50
51
        $payload = [
52
            "auth" =>   [
53
                "identity"  => [
54
                    "methods"   =>  [
55
                        "password"
56
                    ],
57
                    "password" => [
58
                        "user"  =>  [
59
                            "name"      => config('filesystems.disks.ovh.user'),
60
                            "domain"    => [
61
                                "id"    => "default"
62
                            ],
63
                            "password" => config('filesystems.disks.ovh.pass')
64
                        ]
65
                    ]
66
                ],
67
                "scope" =>  [
68
                    "project" => [
69
                        "name" => config('filesystems.disks.ovh.tenantName'),
70
                        "domain" => [
71
                            "id" => "default"
72
                        ]
73
                    ]
74
                ]
75
            ],
76
        ];
77
78
        $this->info("getting auth token from ".config('filesystems.disks.ovh.server').'auth/tokens');
79
80
        $response = $client->request('POST', config('filesystems.disks.ovh.server').'auth/tokens', [
81
            'json'   =>  $payload
82
        ]);
83
84
        $data = json_decode($response->getBody());
85
86
        // Retrieve the token ID variables and publicURL endpoint
87
        foreach($data->token->catalog as $catalog):
88
            if ( $catalog->type == 'object-store' ):
89
                foreach($catalog->endpoints as $endpoint):
90
                    if ( $endpoint->region == config('filesystems.disks.ovh.region') ):
91
                        // $auth_token = $endpoint->id;
92
                        $endpoint = $endpoint->url;
93
                        break(2);
94
                    endif;
95
                endforeach;
96
            endif;
97
        endforeach;
98
99
        // export token=$(curl -is -X POST ${OS_AUTH_URL}auth/tokens -H "Content-Type: application/json" -d ' { "auth": { "identity": { "methods": ["password"], "password": { "user": { "name": "'$OS_USERNAME'", "domain": { "id": "default" }, "password": "'$OS_PASSWORD'" } } }, "scope": { "project": { "name": "'$OS_TENANT_NAME'", "domain": { "id": "default" } } } } }' | grep '^X-Subject-Token' | cut -d" " -f2)
100
        $headers = $response->getHeaders();
101
        $auth_token = $headers["X-Subject-Token"][0];
102
103
104
        // then define the temp-url-key header ( https://docs.ovh.com/gb/en/public-cloud/share_an_object_via_a_temporary_url/#generate-the-key )
105
        // curl -i -X POST \ -H "X-Account-Meta-Temp-URL-Key: 12345" \ -H "X-Auth-Token: abcdef12345" \ https://storage.sbg1.cloud.ovh.net/v1/AUTH_ProjectID
106
107
        $payload = [
108
            'headers'   =>  [
109
                'X-Account-Meta-Temp-URL-Key'   =>  $temp_url_key,
110
                'X-Auth-Token'                  =>  $auth_token
111
            ]
112
        ];
113
        $this->info("posting to ".$endpoint);
0 ignored issues
show
Bug introduced by
The variable $endpoint does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
114
115
        $response = $client->request('POST', $endpoint, $payload);
0 ignored issues
show
Unused Code introduced by
$response 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...
116
117
        $this->alert("add this line to your .env file");
118
        $this->info("OVH_URL_KEY=".$temp_url_key.PHP_EOL);
119
120
    }
121
122
123
124
}
125