Issues (4)

src/Entity/User.php (3 issues)

1
<?php
2
3
namespace CrudSys\OAuth2\Client\Entity;
4
5
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
6
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
7
8
class User implements ResourceOwnerInterface
9
{
10
    use ArrayAccessorTrait;
11
12
    protected array $response;
13
14 16
    public function __construct(array $response)
15
    {
16 16
        $this->response = $response;
17
    }
18
19 2
    public function toArray(): array
20
    {
21 2
        return $this->response;
22
    }
23
24 9
    private function validateString(string $key): string
25
    {
26 9
        $value = $this->getValueByKey($this->response, $key);
27
28 9
        if (!is_string($value) || \isEmptyOrNull($value)) {
29 1
            $value = '';
30
        }
31
32 9
        return $value;
33
    }
34
35 2
    private function validateBool(string $key): bool
36
    {
37 2
        $value = (bool) $this->getValueByKey($this->response, $key);
38
39 2
        if (!is_bool($value) || \isEmptyOrNull($value)) {
0 ignored issues
show
The condition is_bool($value) is always true.
Loading history...
40 1
            $value = false;
41
        }
42
43 2
        return $value;
44
    }
45
46 2
    private function validateInt(string $key): int
47
    {
48 2
        $value = (int) $this->getValueByKey($this->response, $key);
49
50 2
        if (!is_int($value) || \isEmptyOrNull($value)) {
0 ignored issues
show
The condition is_int($value) is always true.
Loading history...
51 1
            $value = 0;
52
        }
53
54 2
        return $value;
55
    }
56
57 1
    public function getId(): string
58
    {
59 1
        return $this->validateString('id');
60
    }
61
62 1
    public function getUsername(): string
63
    {
64 1
        return $this->validateString('username');
65
    }
66
67 1
    public function isPatron(): bool
68
    {
69 1
        return $this->validateBool('patron');
70
    }
71
72 1
    public function isOnline(): bool
73
    {
74 1
        return $this->validateBool('online');
75
    }
76
77 1
    public function getProfile(): array
78
    {
79 1
        return $this->getValueByKey($this->response, 'profile');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getValueBy...s->response, 'profile') could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
80
    }
81
82 1
    public function getCountry(): string
83
    {
84 1
        return $this->validateString('profile.country');
85
    }
86
87 1
    public function getLocation(): string
88
    {
89 1
        return $this->validateString('profile.location');
90
    }
91
92 1
    public function getBio(): string
93
    {
94 1
        return $this->validateString('profile.bio');
95
    }
96
97 1
    public function getFirstName(): string
98
    {
99 1
        return $this->validateString('profile.firstName');
100
    }
101
102 1
    public function getLastName(): string
103
    {
104 1
        return $this->validateString('profile.lastName');
105
    }
106
107 1
    public function getLinks(): string
108
    {
109 1
        return $this->validateString('profile.links');
110
    }
111
112 1
    public function getUrl(): string
113
    {
114 1
        return $this->validateString('url');
115
    }
116
117 1
    public function getCompletionRate(): int
118
    {
119 1
        return $this->validateInt('completionRate');
120
    }
121
122 1
    public function getBlocking(): int
123
    {
124 1
        return $this->validateInt('blocking');
125
    }
126
}
127