Completed
Push — master ( a02954...8fdef2 )
by Elf
03:38
created

Client   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 1
dl 0
loc 152
rs 10
c 0
b 0
f 0

12 Methods

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

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