Issues (45)

src/services/Tokens.php (7 issues)

1
<?php
2
/**
3
 * Socializer plugin for Craft CMS 3.x
4
 *
5
 * @link      https://enupal.com/
6
 * @copyright Copyright (c) 2019 Enupal LLC
7
 */
8
9
namespace enupal\socializer\services;
10
11
use Craft;
12
use craft\elements\User;
13
use enupal\socializer\elements\Provider;
14
use enupal\socializer\elements\Token;
15
use enupal\socializer\Socializer;
16
use yii\base\Component;
17
18
class Tokens extends Component
19
{
20
    /**
21
     * @param int $providerId
22
     * @param int $userId
23
     * @return array|\craft\base\ElementInterface|null
0 ignored issues
show
The type craft\base\ElementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
     */
25
    public function getToken(int $providerId, int $userId)
26
    {
27
        $query = Token::find();
28
        $query->providerId = $providerId;
29
        $query->userId = $userId;
30
31
        return $query->one();
32
    }
33
34
    /**
35
     * @param User $user
36
     * @param Provider $provider
37
     * @return bool
38
     * @throws \Throwable
39
     * @throws \craft\errors\ElementNotFoundException
40
     * @throws \yii\base\Exception
41
     */
42
    public function registerToken(User $user, Provider $provider)
43
    {
44
        $adapter = $provider->getAdapter();
45
        $token = Socializer::$app->tokens->getToken($provider->id, $user->id);
46
47
        if (is_null($token)){
48
            $token = new Token();
49
            $token->providerId = $provider->id;
50
            $token->userId = $user->id;
51
        }
52
53
        $token->accessToken = $adapter->getAccessToken();
0 ignored issues
show
Documentation Bug introduced by
It seems like $adapter->getAccessToken() of type array is incompatible with the declared type string of property $accessToken.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
54
55
        if (!Craft::$app->elements->saveElement($token)) {
0 ignored issues
show
The method saveElement() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
        if (!Craft::$app->elements->/** @scrutinizer ignore-call */ saveElement($token)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            Craft::error("Unable to save token: ".json_encode($token->getErrors()), __METHOD__);
57
            return false;
58
        }
59
60
        return true;
61
    }
62
63
    /**
64
     * @param int $providerId
65
     * @return array|\craft\base\ElementInterface|null
66
     */
67
    public function getCurrentUserToken(int $providerId)
68
    {
69
        $user = Craft::$app->getUser()->getIdentity();
70
71
        if ($user){
0 ignored issues
show
$user is of type yii\web\IdentityInterface, thus it always evaluated to true.
Loading history...
72
            /** @var Token $token */
73
            $token = $this->getToken($providerId, $user->id);
0 ignored issues
show
Accessing id on the interface yii\web\IdentityInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
74
            if ($token){
0 ignored issues
show
$token is of type enupal\socializer\elements\Token, thus it always evaluated to true.
Loading history...
75
                return $token->accessToken;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $token->accessToken returns the type string which is incompatible with the documented return type array|craft\base\ElementInterface|null.
Loading history...
76
            }
77
        }
78
79
        return null;
80
    }
81
}
82