Completed
Push — master ( 00e4c5...e016d8 )
by Kevin
06:06
created

UrlHelpersTrait::consoleUrl()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 42
rs 8.8571
c 1
b 0
f 1
cc 2
eloc 26
nc 2
nop 3
1
<?php namespace PCextreme\Cloudstack\Util;
2
3
use InvalidArgumentException;
4
use PCextreme\Cloudstack\Exception\ClientException;
5
6
trait UrlHelpersTrait
7
{
8
    /**
9
     * Generate Console URL for specified username owning the virtual machine.
10
     *
11
     * @param  string  $username
12
     * @param  string  $domainId
13
     * @param  string  $virtualMachineId
14
     * @return string
15
     * @throws \InvalidArgumentEception
16
     */
17
    public function consoleUrl($username, $domainId, $virtualMachineId)
18
    {
19
        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...
20
            throw new InvalidArgumentException(
21
                'Required options not defined: urlConsole'
22
            );
23
        }
24
25
        // Prepare session.
26
        // Using the SSO (Single Sign On) key we can generate a sessionkey used for the console url.
27
        $command = 'login';
28
        $params = [
29
            'command'   => $command,
30
            'username'  => $username,
31
            'domainid'  => $domainId,
32
            'response'  => 'json',
33
        ];
34
35
        $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...
36
        $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...
37
        $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...
38
        $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...
39
        $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...
40
41
        $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...
42
43
        // Prepare a signed request for the Console url.
44
        // Effectively this will be the console url, it won't be requested at the Cloudstack API.
45
        $params = [
46
            'cmd'        => 'access',
47
            'vm'         => $virtualMachineId,
48
            'userid'     => $login['loginresponse']['userid'],
49
            'sessionkey' => $login['loginresponse']['sessionkey'],
50
            'timestamp'  => round(microtime(true) * 1000),
51
            '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...
52
        ];
53
54
        $base = $this->urlConsole;
55
        $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...
56
57
        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...
58
    }
59
}
60