Passed
Push — main ( 8285d8...9556f0 )
by José
03:31
created

User::validateString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3.1406

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 3.1406
rs 10
c 0
b 0
f 0
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
use CrudSys\OAuth2\Client\Helper\Helper;
8
9
class User implements ResourceOwnerInterface
10
{
11
    use ArrayAccessorTrait;
12
13
    /**
14
     * Raw response
15
     */
16
    protected array $response;
17
18
    /**
19
     * @param array $response
20
     */
21 14
    public function __construct(array $response)
22
    {
23 14
        $this->response = $response;
24
    }
25
26 1
    public function toArray(): array
27
    {
28 1
        return $this->response;
29
    }
30
31 9
    private function validateString($value): string
32
    {
33 9
        if( !is_string($value) || Helper::isEmptyOrNull($value)) {
34
            $value = '';
35
        }
36
37 9
        return $value;
38
    }
39
40 1
    public function getId(): string
41
    {
42 1
        return $this->validateString( $this->getValueByKey($this->response, 'id') );
43
    }
44
45 1
    public function getUsername(): string
46
    {
47 1
        return $this->validateString( $this->getValueByKey($this->response, 'username') );
48
    }
49
50 1
    public function isPatron(): bool
51
    {
52 1
        return $this->getValueByKey($this->response, 'patron');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getValueBy...is->response, 'patron') could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
55 1
    public function isOnline(): bool
56
    {
57 1
        return $this->getValueByKey($this->response, 'online');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getValueBy...is->response, 'online') could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
58
    }
59
60 1
    public function getProfile(): array
61
    {
62 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...
63
    }
64
65 1
    public function getCountry(): string
66
    {
67 1
        return $this->validateString( $this->getValueByKey($this->response, 'profile.country') );
68
    }
69
70 1
    public function getLocation(): string
71
    {
72 1
        return $this->validateString( $this->getValueByKey($this->response, 'profile.location') );
73
    }
74
75 1
    public function getBio(): string
76
    {
77 1
        return $this->validateString( $this->getValueByKey($this->response, 'profile.bio') );
78
    }
79
80 1
    public function getFirstName(): string
81
    {
82 1
        return $this->validateString( $this->getValueByKey($this->response, 'profile.firstName') );
83
    }
84
85 1
    public function getLastName(): string
86
    {
87 1
        return $this->validateString( $this->getValueByKey($this->response, 'profile.lastName') );
88
    }
89
90 1
    public function getLinks(): string
91
    {
92 1
        return $this->validateString( $this->getValueByKey($this->response, 'profile.links') );
93
    }
94
95 1
    public function getUrl(): string
96
    {
97 1
        return $this->validateString( $this->getValueByKey($this->response, 'url') );
98
    }
99
100 1
    public function getCompletionRate(): int
101
    {
102 1
        return $this->getValueByKey($this->response, 'completionRate');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getValueBy...onse, 'completionRate') could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
103
    }
104
}
105