Completed
Push — develop ( 034857...1b157a )
by Christian
8s
created

HttpAuth   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 29.51 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 0
cbo 11
dl 18
loc 61
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A authenticate() 0 6 2
A createSession() 10 10 1
A delete() 8 8 2
A current() 0 5 2
A getBasicAuthMember() 0 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ntb\RestAPI;
4
5
/**
6
 * Authentication mechanism using a BasicAuth request.
7
 *
8
 * @author Andre Lohmann <[email protected]>
9
 */
10
class HttpAuth extends \Object implements IAuth {
11
12
    public static function authenticate($email, $password) {
13
        $authenticator = \Injector::inst()->get('ApiMemberAuthenticator');
14
        if($user = $authenticator->authenticate(['Password' => $password, 'Email' => $email])) {
15
	        return self::createSession($user);
16
        }
17
    }
18
19
	/**
20
	 * @param \Member $user
21
	 * @return ApiSession
22
	 */
23 View Code Duplication
	public static function createSession($user) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
		$user->logIn();
25
		/** @var \Member $user */
26
		$user = \DataObject::get(\Config::inst()->get('BaseRestController', 'Owner'))->byID($user->ID);
27
		// create session
28
		$session = ApiSession::create();
29
		$session->User = $user;
30
		$session->Token = AuthFactory::generate_token($user);
31
		return $session;
32
	}
33
34 View Code Duplication
	public static function delete($request) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
        $owner = self::current($request);
36
        if(!$owner) {
37
            throw new RestUserException("No session found", 404, 404);
38
        }
39
        $owner->logOut();
40
        return true;
41
    }
42
43
44
        /**
45
         * @param \SS_HTTPRequest $request
46
         * @return \Member
0 ignored issues
show
Documentation introduced by
Should the return type not be \Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
47
         */
48
        public static function current($request) {
49
            $member = self::getBasicAuthMember();
50
            return ($member instanceof \Member) ?
51
                \DataObject::get(\Config::inst()->get('BaseRestController', 'Owner'))->byID($member->ID) : null;
52
        }
53
        
54
        /**
55
         * @return \Member
0 ignored issues
show
Documentation introduced by
Should the return type not be \Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
56
         */
57
        protected static function getBasicAuthMember() {
58
            $realm = \Config::inst()->get('HttpAuth', 'Realm');
59
            $permissionCode = \Config::inst()->get('HttpAuth', 'PermissionCode');
60
            $isRunningTests = (class_exists('SapphireTest', false) && \SapphireTest::is_running_test());
61
            $tryUsingSessionLogin = $isRunningTests || \Config::inst()->get('HttpAuth', 'TryUsingSessionLogin');
62
            try {
63
                $member = \BasicAuth::requireLogin($realm, $permissionCode, $tryUsingSessionLogin);
64
                return $member;
65
            } catch (\Exception $ex) {
66
                return null;
67
            }
68
        }
69
70
}
71