Client   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 156
ccs 0
cts 51
cp 0
rs 10
c 2
b 0
f 0
wmc 13

12 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultAppKey() 0 3 2
A generateAppSecretForKey() 0 3 1
A __construct() 0 4 1
A appKeyForName() 0 3 1
A getAppNameForKey() 0 3 1
A getAppSecretForKey() 0 3 1
A getClients() 0 3 1
A getEncrypter() 0 3 1
A generateAppSecretForName() 0 3 1
A setClients() 0 5 1
A setEncrypter() 0 5 1
A setDefaultAppKey() 0 5 1
1
<?php
2
3
namespace ElfSundae\Laravel\Api;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Contracts\Encryption\Encrypter;
7
8
class Client
9
{
10
    /**
11
     * The Encrypter instance.
12
     *
13
     * @var \Illuminate\Contracts\Encryption\Encrypter
14
     */
15
    protected $encrypter;
16
17
    /**
18
     * The api clients.
19
     *
20
     * @var array
21
     */
22
    protected $clients = [];
23
24
    /**
25
     * The default app key.
26
     *
27
     * @var string
28
     */
29
    protected $defaultAppKey;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param  array  $clients
35
     */
36
    public function __construct(Encrypter $encrypter, array $clients = [])
37
    {
38
        $this->encrypter = $encrypter;
39
        $this->clients = $clients;
40
    }
41
42
    /**
43
     * Get the clients.
44
     *
45
     * @return array
46
     */
47
    public function getClients()
48
    {
49
        return $this->clients;
50
    }
51
52
    /**
53
     * Set the client.
54
     *
55
     * @param  array  $clients
56
     * @return $this
57
     */
58
    public function setClients(array $clients)
59
    {
60
        $this->clients = $clients;
61
62
        return $this;
63
    }
64
65
    /**
66
     * Get the Encrypter instance.
67
     *
68
     * @return \Illuminate\Contracts\Encryption\Encrypter
69
     */
70
    public function getEncrypter()
71
    {
72
        return $this->encrypter;
73
    }
74
75
    /**
76
     * Set the Encrypter instance.
77
     *
78
     * @param  \Illuminate\Contracts\Encryption\Encrypter  $encrypter
79
     * @return $this
80
     */
81
    public function setEncrypter(Encrypter $encrypter)
82
    {
83
        $this->encrypter = $encrypter;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Get the default app key.
90
     *
91
     * @return string
92
     */
93
    public function defaultAppKey()
94
    {
95
        return $this->defaultAppKey ?: Arr::first(array_keys($this->clients));
96
    }
97
98
    /**
99
     * Set the default app key.
100
     *
101
     * @param  string  $key
102
     * @return $this
103
     */
104
    public function setDefaultAppKey($key)
105
    {
106
        $this->defaultAppKey = $key;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Get the app key for the given app name.
113
     *
114
     * @param  string  $appName
115
     * @return string
116
     */
117
    public function appKeyForName($appName)
118
    {
119
        return substr(sha1($appName.$this->encrypter->getKey()), 0, 16);
0 ignored issues
show
Bug introduced by
The method getKey() does not exist on Illuminate\Contracts\Encryption\Encrypter. ( Ignorable by Annotation )

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

119
        return substr(sha1($appName.$this->encrypter->/** @scrutinizer ignore-call */ getKey()), 0, 16);

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...
120
    }
121
122
    /**
123
     * Generate an app secret for the given app key.
124
     *
125
     * @param  string  $appKey
126
     * @return string
127
     */
128
    public function generateAppSecretForKey($appKey)
129
    {
130
        return md5($this->encrypter->encrypt($appKey));
131
    }
132
133
    /**
134
     * Generate an app secret for the given app name.
135
     *
136
     * @param  string  $appName
137
     * @return string
138
     */
139
    public function generateAppSecretForName($appName)
140
    {
141
        return $this->generateAppSecretForKey($this->appKeyForName($appName));
142
    }
143
144
    /**
145
     * Get the app name for the given app key.
146
     *
147
     * @param  string  $key
148
     * @return string|null
149
     */
150
    public function getAppNameForKey($key)
151
    {
152
        return Arr::get($this->clients, $key.'.name');
153
    }
154
155
    /**
156
     * Get the app secret for the given app key.
157
     *
158
     * @param  string  $key
159
     * @return string|null
160
     */
161
    public function getAppSecretForKey($key)
162
    {
163
        return Arr::get($this->clients, $key.'.secret');
164
    }
165
}
166