Completed
Push — master ( 5f86bb...797bc6 )
by Beñat
04:08
created

AccessToken::fromProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A AccessToken::setAccessToken() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace BenatEspina\StackExchangeApiClient\Model;
15
16
/**
17
 * @author Beñat Espiña <[email protected]>
18
 */
19
class AccessToken implements Model
20
{
21
    protected $accessToken;
22
    protected $accountId;
23
    protected $expiresOnDate;
24
    protected $scope;
25
26
    public static function fromJson(array $data) : self
27
    {
28
        $instance = new self();
29
        $instance
30
            ->setAccessToken(array_key_exists('access_token', $data) ? $data['access_token'] : null)
31
            ->setAccountId(array_key_exists('account_id', $data) ? $data['account_id'] : null)
32
            ->setExpiresOnDate(
33
                array_key_exists('expires_on_date', $data)
34
                    ? new \DateTimeImmutable('@' . $data['expires_on_date'])
35
                    : null
36
            )
37
            ->setScope(array_key_exists('scope', $data) ? $data['scope'] : null);
38
39
        return $instance;
40
    }
41
42
    public function setAccessToken(?string $accessToken) : self
43
    {
44
        $this->accessToken = $accessToken;
45
46
        return $this;
47
    }
48
49
    public function getAccessToken() : ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
50
    {
51
        return $this->accessToken;
52
    }
53
54
    public function setAccountId(?int $accountId) : self
55
    {
56
        $this->accountId = $accountId;
57
58
        return $this;
59
    }
60
61
    public function getAccountId() : ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
    {
63
        return $this->accountId;
64
    }
65
66
    public function setExpiresOnDate(?\DateTimeInterface $expiresOnDate) : self
67
    {
68
        $this->expiresOnDate = $expiresOnDate;
69
70
        return $this;
71
    }
72
73
    public function getExpiresOnDate() : ?\DateTimeInterface
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
74
    {
75
        return $this->expiresOnDate;
76
    }
77
78
    public function setScope(?array $scope) : self
79
    {
80
        $this->scope = $scope;
81
82
        return $this;
83
    }
84
85
    public function getScope() : ?array
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
86
    {
87
        return $this->scope;
88
    }
89
90
    public function jsonSerialize() : array
91
    {
92
        $result = [
93
            'account_id'      => $this->getAccountId(),
94
            'access_token'    => $this->getAccessToken(),
95
            'expires_on_date' => $this->getExpiresOnDate(),
96
            'scope'           => $this->getScope(),
97
        ];
98
99
        foreach ($result as $key => $element) {
100
            if (null === $element) {
101
                unset($result[$key]);
102
            }
103
        }
104
105
        return $result;
106
    }
107
}
108