SocialiteRepository::createEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace App\Repositories;
4
5
use Laravel\Socialite\Contracts\User as ProviderUser;
6
use App\Socialite;
7
8
class SocialiteRepository extends Repository
9
{
10
    /**
11
     * @return Socialite
12
     */
13
    public function getModel()
14
    {
15
        return new Socialite();
16
    }
17
18
    /**
19
     * Get user by provider id.
20
     *
21
     * @param $provider
22
     * @param $id
23
     * @return null
24
     */
25
    public function getUserByProvider($provider, $id)
26
    {
27
        $model = self::getModel();
28
29
        switch ($provider)
30
        {
31
            case $model::PROVIDER_FACEBOOK :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
32
                return $this->getFacebookUserById($id);
33
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
34
35
            case $model::PROVIDER_GOOGLE :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a CASE statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.

switch ($selector) {
    case "A": //right
        doSomething();
        break;
    case "B" : //wrong
        doSomethingElse();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
36
                return $this->getGoogleUserById($id);
37
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
38
39
            default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
40
                return null;
41
        }
42
    }
43
44
    /**
45
     * Get facebook user.
46
     *
47
     * @param $id
48
     * @return mixed
49
     */
50
    public function getFacebookUserById($id)
51
    {
52
        return $this->getModel()
0 ignored issues
show
Bug introduced by
The method facebook() does not exist on App\Socialite. Did you maybe mean scopeFacebook()?

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...
53
            ->facebook()
54
            ->where('provider_id', $id)
55
            ->first();
56
    }
57
58
    /**
59
     * Get google user.
60
     *
61
     * @param $id
62
     * @return mixed
63
     */
64
    public function getGoogleUserById($id)
65
    {
66
        return $this->getModel()
0 ignored issues
show
Bug introduced by
The method google() does not exist on App\Socialite. Did you maybe mean scopeGoogle()?

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...
67
            ->google()
68
            ->where('provider_id', $id)
69
            ->first();
70
    }
71
72
    public function createEmpty($provider, ProviderUser $user)
73
    {
74
        return self::getModel()
75
            ->create([
76
                'provider' => $provider,
77
                'provider_id' => $user->getId(),
78
                'callback' => $this->cleanCallback($user),
79
                'active' => 1
80
            ]);
81
    }
82
83
    /**
84
     * Check if user exists.
85
     *
86
     * @param $provider
87
     * @param $id
88
     * @return bool
89
     */
90
    public function checkProviderUser($provider, $id)
91
    {
92
        return (bool) $this->getUserByProvider($provider, $id);
93
    }
94
95
    /**
96
     * Callback to json.
97
     *
98
     * @param $callback
99
     * @return string
100
     */
101
    private function cleanCallback($callback)
102
    {
103
        $array_callback = (array) $callback;
104
        $user           = $array_callback['user'];
105
        unset($array_callback['user']);
106
        return json_encode(array_merge($array_callback, $user));
107
    }
108
}