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

BasicAuth   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A authenticate() 0 15 3
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\Core\REST\Server\Exceptions\AuthenticationFailedException;
16
use eZ\Publish\API\Repository\Repository;
17
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
18
use Qafoo\RMF;
19
use InvalidArgumentException;
20
21
/**
22
 * Authenticator for integration tests.
23
 *
24
 * This is, by now, just an untested stub.
25
 *
26
 * @todo Remove when the REST client is refactored
27
 */
28
class BasicAuth extends Authenticator
29
{
30
    /**
31
     * Creates an new Authenticator to $repository.
32
     *
33
     * @param \eZ\Publish\API\Repository\Repository $repository
34
     */
35
    public function __construct(Repository $repository)
36
    {
37
        $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...
38
    }
39
40
    /**
41
     * Authenticates the user based on the given request.
42
     *
43
     * Performs an authentication based on the given $request and sets the
44
     * authenticated user into the $repository. Returns true on success, false
45
     * of authentication was not possible or did not succeed.
46
     *
47
     * @param RMF\Request $request
48
     *
49
     * @return bool
50
     */
51
    public function authenticate(RMF\Request $request)
52
    {
53
        try {
54
            $this->repository->setCurrentUser(
55
                $this->repository->getUserService()->loadUserByCredentials(
56
                    $request->username,
57
                    $request->password
58
                )
59
            );
60
        } catch (InvalidArgumentException $e) {
61
            return false;
62
        } catch (NotFoundException $e) {
63
            throw new AuthenticationFailedException();
64
        }
65
    }
66
}
67