Context::getRedirectUris()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BPCI\SumUp;
4
5
class Context implements ContextInterface
6
{
7
    /**
8
     * Index to use the right uri
9
     * 
10
     * @var number
11
     */
12
    private $uriIndexToUse = 0;
13
14
    /**
15
     * id
16
     *
17
     * @var string
18
     */
19
    private $id;
20
21
    /**
22
     * name
23
     *
24
     * @var string
25
     */
26
    private $name;
27
28
    /**
29
     * client_id
30
     *
31
     * @var string
32
     */
33
    private $clientId;
34
35
    /**
36
     * client_secret
37
     *
38
     * @var string
39
     */
40
    private $clientSecret;
41
42
    /**
43
     * application_type
44
     * web|android|ios|other
45
     *
46
     * @var string
47
     */
48
    private $applicationType;
49
50
    /**
51
     * redirect_uris
52
     *
53
     * @var array
54
     */
55
    private $redirectUris;
56
57
    /**
58
     * cors_uris
59
     *
60
     * @var array
61
     */
62
    private $corsUris;
63
64 9
    public function __construct(Array $context = [])
65
    {
66 9
        $this->id = $context['id'] ?? null;
67 9
        $this->name = $context['name'] ?? null;
68 9
        $this->clientId = $context['client_id'] ?? null;
69 9
        $this->clientSecret = $context['client_secret'] ?? null;
70 9
        $this->applicationType = $context['application_type'] ?? null;
71 9
        $this->redirectUris = $context['redirect_uris'] ?? null;
72 9
        $this->corsUris = $context['cors_uris'] ?? null;
73 9
    }
74
75
    /**
76
     * Load Context From file  
77
     * the file must have only json context inside.
78
     *
79
     * @param string $filePath
80
     * @return Context
81
     */
82 9
    public static function loadContextFromFile(string $filePath): ContextInterface
83
    {
84 9
        if(!file_exists($filePath)){
85 1
            throw new Exception\FileNotFoundException('Context file not found: '.$filePath, 404, null, $filePath);
86
        }
87
88 9
        $contents = file_get_contents($filePath);
89 9
        $context_array = json_decode($contents, true);
90
91 9
        if($context_array === null){
92
            throw new Exception\MalformedJsonException('JSON sintax error.');
93
        }
94
95 9
        return new self($context_array);
96
    }
97
98
    /**
99
     * Get context data as array
100
     *
101
     * @return Array
102
     */
103 1
    public function getContextData(): Array
104
    {
105
        return [
106 1
        'id' => $this->getId(),
107 1
        'name' => $this->getName(),
108 1
        'client_id' => $this->getClientId(),
109 1
        'client_secret' => $this->getClientSecret(),
110 1
        'application_type' => $this->getApplicationType(),
111 1
        'redirect_uris' => $this->getRedirectUris(),
112 1
        'cors_uris' => $this->getCorsUris()
113
        ];
114
    }
115
116 1
    public function getId()
117
    {
118 1
        return $this->id;
119
    }
120
    
121 1
    public function getName()
122
    {
123 1
        return $this->name;
124
    }
125
    
126 1
    public function getClientId()
127
    {
128 1
        return $this->clientId;
129
    }
130
    
131 1
    public function getClientSecret()
132
    {
133 1
        return $this->clientSecret;
134
    }
135
    
136 1
    public function getApplicationType()
137
    {
138 1
        return $this->applicationType;
139
    }
140
    
141 1
    public function getRedirectUris()
142
    {
143 1
        return $this->redirectUris;
144
    }
145
    
146 1
    public function getCorsUris()
147
    {
148 1
        return $this->corsUris;
149
    }
150
151
    /**
152
     * Set Index of URI to use on requests
153
     *
154
     * @param int $index
155
     * @return ContextInterface
156
     */
157 1
    public function setIndexUri(int $index): ContextInterface
158
    {
159 1
        $this->uriIndexToUse = $index;
160
161 1
        return $this;
162
    }
163
164 1
    public function getRedirectUri()
165
    {
166 1
        return $this->redirectUris[$this->uriIndexToUse];
167
    }
168
169
}