GitHubController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 5
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A redirectToProvider() 0 4 1
B handleProviderCallback() 0 37 6
1
<?php
2
3
namespace App\Http\Controllers\Auth;
4
5
use App\Contact;
6
use Auth;
7
use App\User;
8
use GrahamCampbell\GitHub\GitHubManager;
9
use Socialite;
10
use App\Http\Requests;
11
use Illuminate\Http\Request;
12
use App\Http\Controllers\Controller;
13
14
class GitHubController extends Controller
15
{
16
    protected $github;
17
18
    public function __construct(GitHubManager $github)
19
    {
20
        $this->github = $github;
21
    }
22
23
    /**
24
     * Redirect the user to the GitHub authentication page.
25
     *
26
     * @return Response
27
     */
28
    public function redirectToProvider()
29
    {
30
        return Socialite::driver('github')->scopes(['read:org', 'user:email'])->redirect();
31
    }
32
33
    /**
34
     * Obtain the user information from GitHub.
35
     *
36
     * @return Response
37
     */
38
    public function handleProviderCallback()
39
    {
40
        $socialiteUser = Socialite::driver('github')->user();
41
42
        //If the org filter is set get the users organisations and check them
43
        if (!empty(env('VALID_GITHUB_ORG'))) {
44
            $this->github->authenticate(\Github\Client::AUTH_URL_TOKEN, $socialiteUser->token);
45
46
            $organizations = $this->github->currentUser()->memberships()->all();
47
48
            $loginValid = false;
49
            foreach ($organizations as $org) {
50
                if ($org['organization']['login'] === env('VALID_GITHUB_ORG')) {
51
                    $loginValid = true;
52
                }
53
            }
54
55
            if (!$loginValid) {
56
                return redirect('/login')->withError('Not a member of the required organisation');
0 ignored issues
show
Bug introduced by
The method withError() does not exist on Illuminate\Http\RedirectResponse. Did you maybe mean withErrors()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
57
            }
58
        }
59
60
        //Locate a user or create an account
61
        $user = User::where('email', $socialiteUser->getEmail())->first();
62
        if (!$user) {
63
            $user = User::create(['email' => $socialiteUser->getEmail(), 'name' => $socialiteUser->getName()]);
64
            //Add a contact record for the user so they receive update notifications
65
            $contact              = new Contact;
66
            $contact->name        = $user->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
67
            $contact->email       = $user->email;
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<App\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
            $contact->filter_tags = [];
69
            $contact->active      = true;
70
            $contact->save();
71
        }
72
        Auth::login($user, true);
73
        return redirect('/pings');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return redirect('/pings'); (Illuminate\Http\RedirectResponse) is incompatible with the return type documented by App\Http\Controllers\Aut...:handleProviderCallback of type App\Http\Controllers\Auth\Response.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
74
    }
75
}
76