1 | <?php |
||
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'); |
|
|
|||
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) |
|
109 | |||
110 | } |
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
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. 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.