Completed
Push — fix_travis_behat ( 3c7f1b...b1b900 )
by
unknown
54:25 queued 34:54
created

IntegrationTest::authenticate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Authenticator used for integration tests.
5
 *
6
 * ATTENTION: This is a only meant for the test setup for the REST server. DO
7
 * NOT USE IT IN PRODUCTION!
8
 *
9
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
10
 * @license For full copyright and license information view LICENSE file distributed with this source code.
11
 */
12
namespace eZ\Publish\Core\REST\Server\Authenticator;
13
14
use eZ\Publish\Core\REST\Server\Authenticator;
15
use eZ\Publish\API\Repository\Repository;
16
use Qafoo\RMF;
17
use InvalidArgumentException;
18
use RuntimeException;
19
20
/**
21
 * Authenticator for integration tests.
22
 *
23
 * @todo Remove when the REST client is refactored
24
 */
25
class IntegrationTest extends Authenticator
26
{
27
    /**
28
     * Creates an new Authenticator to $repository.
29
     *
30
     * @param \eZ\Publish\API\Repository\Repository $repository
31
     */
32
    public function __construct(Repository $repository)
33
    {
34
        $this->repository = $repository;
0 ignored issues
show
Bug introduced by
The property repository 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...
35
    }
36
37
    /**
38
     * Authenticates the user based on the given request.
39
     *
40
     * Performs an authentication based on the given $request and sets the
41
     * authenticated user into the $repository. Returns true on success, false
42
     * of authentication was not possible or did not succeed.
43
     *
44
     * @param RMF\Request $request
45
     *
46
     * @return bool
47
     */
48
    public function authenticate(RMF\Request $request)
49
    {
50
        try {
51
            $this->repository->setCurrentUser(
52
                $this->repository->getUserService()->loadUser($request->testUser)
53
            );
54
        } catch (InvalidArgumentException $e) {
55
            throw new RuntimeException('The Integration Test Authenticator requires a test user ID to be set using the HTTP Header X-Test-User.');
56
        }
57
    }
58
}
59