GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d9d5ed...b105dd )
by Ryun
03:15 queued 30s
created

Sociable::detachProviderById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Humweb\Sociable\Models;
4
5
/**
6
 * Class Sociable
7
 *
8
 * @package Humweb\SociableConnection\Models
9
 */
10
trait Sociable
11
{
12
13
    /**
14
     * SocialConnection relationship
15
     *
16
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
17
     */
18 18
    public function social()
19
    {
20 18
        return $this->hasMany(SocialConnection::class, 'user_id');
0 ignored issues
show
Bug introduced by
It seems like hasMany() 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...
21
    }
22
23
24
    /**
25
     * Link provider to user
26
     *
27
     * @param string $provider
28
     * @param mixed  $user
29
     */
30 18
    public function attachProvider($provider, $user)
31
    {
32 18
        $version = $this->detectOAuthVersion($user);
33
34
        // OAuth One Providers
35 18
        if ($version === 1) {
36
            $data = [
37 9
                'token'       => $user->token,
38 9
                'tokenSecret' => $user->tokenSecret
39 9
            ];
40 9
        } // OAuth Two Providers
41
        else {
42
            $data = [
43 9
                'token'        => $user->token,
44 9
                'refreshToken' => $user->refreshToken, // not always provided
45 9
                'expiresIn'    => $user->expiresIn,
46 9
            ];
47
        }
48
49 18
        $socialData = SocialConnection::create([
50 18
            'social_id'     => $user->id,
51 18
            'provider'      => $provider,
52 18
            'oauth_version' => $version,
53 18
            'data'          => $data,
54 18
        ]);
55
56 18
        $this->social()->save($socialData);
57 18
    }
58
59
60
    /**
61
     * Unlink provider from user by provider name(s)
62
     *
63
     * @param string|array $provider
64
     *
65
     * @return int
66
     */
67 3
    public function detachProviderByName($provider)
68
    {
69 3
        $count = 0;
70 3
        $providers = $this->social()->ofProvider($provider)->get();
71 3
        foreach ($providers as $provider) {
72 3
            $provider->delete();
73 3
            $count++;
74 3
        }
75
76 3
        return $count;
77
    }
78
79
80
    /**
81
     * Unlink providers from user by provider id(s)
82
     *
83
     * @param string|array $provider
84
     *
85
     * @return int
86
     */
87 3
    public function detachProviderById($provider)
88
    {
89 3
        $count = 0;
90 3
        $providers = $this->social()->ofProviderId($provider)->get();
91 3
        foreach ($providers as $provider) {
92 3
            $provider->delete();
93 3
            $count++;
94 3
        }
95 3
        return $count;
96
    }
97
98
99 3
    public function listProviders()
100
    {
101 3
        return $this->social()->lists('provider')->all();
102
    }
103
104
105 18
    public function detectOAuthVersion($user)
106
    {
107 18
        return property_exists($user, 'tokenSecret') ? 1 : 2;
108
    }
109
110
}