Completed
Push — fulltext_indexing_test ( 4cf81d )
by André
33:01
created

Authentication   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A usePermissionsOfRole() 0 23 3
A useAnonymousRole() 0 13 3
A createSession() 0 19 1
A cleanupSession() 0 11 2
1
<?php
2
3
/**
4
 * File containing the Authentication context class for RestBundle.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Bundle\EzPublishRestBundle\Features\Context\SubContext;
12
13
use eZ\Publish\Core\REST\Server\Values\SessionInput;
14
15
trait Authentication
16
{
17
    /**
18
     * @var eZ\Publish\Core\REST\Server\Values\UserSession
19
     */
20
    protected $userSession;
21
22
    /**
23
     * @Given I have :role permissions
24
     */
25
    public function usePermissionsOfRole($role)
26
    {
27
        $credentials = $this->getCredentialsFor($role);
0 ignored issues
show
Bug introduced by
It seems like getCredentialsFor() 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...
28
29
        switch ($this->authType) {
0 ignored issues
show
Bug introduced by
The property authType 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...
30
            case self::AUTHTYPE_BASICHTTP:
31
                $this->restDriver->setAuthentication(
0 ignored issues
show
Bug introduced by
The property restDriver 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...
32
                    $credentials['login'],
33
                    $credentials['password']
34
                );
35
                break;
36
            case self::AUTHTYPE_SESSION:
37
                $this->createSession($credentials['login'], $credentials['password']);
38
                break;
39
            default:
40
                throw new \Exception("Unknown auth type: '{$this->authType}'.");
41
        }
42
43
        // also authenticate the user on the local repository instance
44
        $this->getRepository()->setCurrentUser(
0 ignored issues
show
Bug introduced by
It seems like getRepository() 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...
45
            $this->getRepository()->getUserService()->loadUserByLogin($credentials['login'])
0 ignored issues
show
Bug introduced by
It seems like getRepository() 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...
46
        );
47
    }
48
49
    /**
50
     * @Given I don't have permissions
51
     * @Given I do not have permissions
52
     */
53
    public function useAnonymousRole()
54
    {
55
        switch ($this->authType) {
56
            case self::AUTHTYPE_BASICHTTP:
57
                $this->restDriver->setAuthentication('anonymous', '');
58
                break;
59
            case self::AUTHTYPE_SESSION:
60
                $this->cleanupSession();
61
                break;
62
            default:
63
                throw new \Exception("Unknown auth type: '{$this->authType}'.");
64
        }
65
    }
66
67
    /**
68
     * @When I create a (new) session with login :login and password :password
69
     */
70
    public function createSession($login, $password)
71
    {
72
        $this->createRequest('post', '/user/sessions');
0 ignored issues
show
Bug introduced by
It seems like createRequest() 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
        $this->setHeaderWithObject('accept', 'Session');
0 ignored issues
show
Bug introduced by
It seems like setHeaderWithObject() 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
        $this->setHeaderWithObject('content-type', 'SessionInput');
0 ignored issues
show
Bug introduced by
It seems like setHeaderWithObject() 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...
75
76
        $this->makeObject('SessionInput');
0 ignored issues
show
Bug introduced by
It seems like makeObject() 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...
77
        $this->setFieldToValue('login', $login);
0 ignored issues
show
Bug introduced by
It seems like setFieldToValue() 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...
78
        $this->setFieldToValue('password', $password);
0 ignored issues
show
Bug introduced by
It seems like setFieldToValue() 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...
79
        $this->sendRequest();
0 ignored issues
show
Bug introduced by
It seems like sendRequest() 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...
80
81
        $this->userSession = $this->getResponseObject();
0 ignored issues
show
Bug introduced by
It seems like getResponseObject() 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...
82
83
        $this->resetDriver();
0 ignored issues
show
Bug introduced by
It seems like resetDriver() 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...
84
85
        // apply session/csrf token to next request
86
        $this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}");
87
        $this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken);
88
    }
89
90
    /**
91
     * @AfterScenario
92
     *
93
     * Cleanup session, if applicable.
94
     */
95
    public function cleanupSession()
96
    {
97
        if ($this->userSession) {
98
            $this->resetDriver();
0 ignored issues
show
Bug introduced by
It seems like resetDriver() 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...
99
            $this->createRequest('post', "/user/sessions/{$this->userSession->sessionId}");
0 ignored issues
show
Bug introduced by
It seems like createRequest() 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...
100
            $this->restDriver->setHeader('cookie', "{$this->userSession->sessionName}={$this->userSession->sessionId}");
101
            $this->restDriver->setHeader('x-csrf-token', $this->userSession->csrfToken);
102
            $this->sendRequest();
0 ignored issues
show
Bug introduced by
It seems like sendRequest() 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...
103
            $this->userSession = null;
104
        }
105
    }
106
}
107