Completed
Push — master ( adc836...85a2ca )
by Kevin
03:35
created

UrlHelpersTrait::clientUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php namespace PCextreme\Cloudstack\Util;
2
3
use InvalidArgumentException;
4
use PCextreme\Cloudstack\Exception\ClientException;
5
6
trait UrlHelpersTrait
7
{
8
    /**
9
     * Generate Client URL for specified username.
10
     *
11
     * @param  string  $username
12
     * @param  string  $domainId
13
     * @return string
14
     * @throws \InvalidArgumentEception
15
     */
16
    public function clientUrl($username, $domainId)
17
    {
18
        if (is_null($this->urlClient)) {
0 ignored issues
show
Bug introduced by
The property urlClient 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...
19
            throw new InvalidArgumentException(
20
                'Required options not defined: urlClient'
21
            );
22
        }
23
24
        // Prepare session.
25
        // Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url.
26
        $command = 'login';
27
        $params = [
28
            'command'   => $command,
29
            'username'  => $username,
30
            'domainid'  => $domainId,
31
            'timestamp' => round(microtime(true) * 1000),
32
            'response'  => 'json',
33
        ];
34
35
        $method  = $this->getCommandMethod($command);
0 ignored issues
show
Bug introduced by
It seems like getCommandMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Unused Code introduced by
$method 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...
36
        $query   = $this->enableSso()->getCommandQuery($params);
0 ignored issues
show
Bug introduced by
It seems like enableSso() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
38
        return $this->urlClient.'?loginUrl='.urlencode($query);
39
    }
40
41
    /**
42
     * Generate Console URL for specified username owning the virtual machine.
43
     *
44
     * @param  string  $username
45
     * @param  string  $domainId
46
     * @param  string  $virtualMachineId
47
     * @return string
48
     * @throws \InvalidArgumentEception
49
     */
50
    public function consoleUrl($username, $domainId, $virtualMachineId)
51
    {
52
        if (is_null($this->urlConsole)) {
0 ignored issues
show
Bug introduced by
The property urlConsole 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...
53
            throw new InvalidArgumentException(
54
                'Required options not defined: urlConsole'
55
            );
56
        }
57
58
        // Prepare session.
59
        // Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url.
60
        $command = 'login';
61
        $params = [
62
            'command'   => $command,
63
            'username'  => $username,
64
            'domainid'  => $domainId,
65
            'timestamp' => round(microtime(true) * 1000),
66
            'response'  => 'json',
67
        ];
68
69
        $base    = $this->urlApi;
0 ignored issues
show
Bug introduced by
The property urlApi 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...
70
        $method  = $this->getCommandMethod($command);
0 ignored issues
show
Bug introduced by
It seems like getCommandMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
71
        $query   = $this->enableSso()->getCommandQuery($params);
0 ignored issues
show
Bug introduced by
It seems like enableSso() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
72
        $url     = $this->appendQuery($base, $query);
0 ignored issues
show
Bug introduced by
It seems like appendQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
73
        $request = $this->getRequest($method, $url);
0 ignored issues
show
Bug introduced by
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
74
75
        $login = $this->getResponse($request);
0 ignored issues
show
Bug introduced by
It seems like getResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
77
        // Prepare a signed request for the Console url.
78
        // Effectively this will be the console url, it won't be requested at the Cloudstack API.
79
        $params = [
80
            'cmd'        => 'access',
81
            'vm'         => $virtualMachineId,
82
            'userid'     => $login['loginresponse']['userid'],
83
            'sessionkey' => $login['loginresponse']['sessionkey'],
84
            'timestamp'  => round(microtime(true) * 1000),
85
            'apikey'     => $this->apiKey,
0 ignored issues
show
Bug introduced by
The property apiKey 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...
86
        ];
87
88
        $base = $this->urlConsole;
89
        $query  = $this->getCommandQuery($params);
0 ignored issues
show
Bug introduced by
It seems like getCommandQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
90
91
        return $this->appendQuery($base, $query);
0 ignored issues
show
Bug introduced by
It seems like appendQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
92
    }
93
}
94