Completed
Push — master ( 1afd97...8dffa3 )
by ARCANEDEV
10s
created

HashManager::decode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
rs 10
1
<?php namespace Arcanedev\Hasher;
2
3
use Arcanedev\Hasher\Contracts\HashManager as HashManagerContract;
4
use Arcanedev\Support\Manager;
5
6
/**
7
 * Class     HashManager
8
 *
9
 * @package  Arcanedev\Hasher
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class HashManager extends Manager implements HashManagerContract
13
{
14
    /* ------------------------------------------------------------------------------------------------
15
     |  Properties
16
     | ------------------------------------------------------------------------------------------------
17
     */
18
    /**
19
     * The driver connection.
20
     *
21
     * @var  string
22
     */
23
    protected $connection;
24
25
    /* ------------------------------------------------------------------------------------------------
26
     |  Constructor
27
     | ------------------------------------------------------------------------------------------------
28
     */
29
    /**
30
     * Create a new manager instance.
31
     *
32
     * @param  \Illuminate\Foundation\Application  $app
33
     */
34
    public function __construct($app)
35
    {
36
        parent::__construct($app);
37
38
        $this->buildDrivers();
39
    }
40
41
    /* ------------------------------------------------------------------------------------------------
42
     |  Getters & Setters
43
     | ------------------------------------------------------------------------------------------------
44
     */
45
    /**
46
     * Get the default driver name.
47
     *
48
     * @return string
49
     */
50
    public function getDefaultDriver()
51
    {
52
        return $this->getHasherConfig('default.driver');
53
    }
54
55
    /**
56
     * Get the default driver connection.
57
     *
58
     * @return string
59
     */
60
    public function getDefaultConnection()
61
    {
62
        return is_null($this->connection)
63
            ? $this->getHasherConfig('default.connection', 'main')
64
            : $this->connection;
65
    }
66
67
    /**
68
     * Set the hasher connection.
69
     *
70
     * @param  string  $connection
71
     *
72
     * @return \Arcanedev\Hasher\HashManager
73
     */
74
    public function connection($connection = null)
75
    {
76
        if ( ! is_null($connection)) {
77
            $this->connection = $connection;
78
            $this->buildDrivers();
79
        }
80
81
        return $this;
82
    }
83
84
    /* ------------------------------------------------------------------------------------------------
85
     |  Main Functions
86
     | ------------------------------------------------------------------------------------------------
87
     */
88
    /**
89
     * Get a driver instance.
90
     *
91
     * @param  string       $driver
92
     * @param  string|null  $connection
93
     *
94
     * @return \Arcanedev\Hasher\Contracts\HashDriver
95
     */
96
    public function with($connection = null, $driver = null)
97
    {
98
        return $this->connection($connection)
99
                    ->driver($driver);
100
    }
101
102
    /**
103
     * Get a driver instance.
104
     *
105
     * @param  string  $driver
106
     *
107
     * @return \Arcanedev\Hasher\Contracts\HashDriver
108
     */
109
    public function driver($driver = null)
110
    {
111
        return parent::driver($driver);
112
    }
113
114
    /**
115
     * Build all hash drivers.
116
     */
117
    private function buildDrivers()
118
    {
119
        $drivers = $this->getHasherConfig('drivers', []);
120
121
        foreach ($drivers as $name => $class) {
122
            $this->buildDriver($name, $class);
123
        }
124
    }
125
126
    /**
127
     * Build the driver.
128
     *
129
     * @param  string  $name
130
     * @param  string  $class
131
     *
132
     * @return Contracts\HashDriver
133
     */
134
    private function buildDriver($name, $class)
135
    {
136
        $connection           = $this->getDefaultConnection();
137
        $this->drivers[$name] = $this->app->make($class, [
138
            $this->getHasherConfig("connections.$name.$connection", [])
139
        ]);
140
141
        return $this->drivers[$name];
142
    }
143
144
    /**
145
     * Encode the value.
146
     *
147
     * @param  mixed  $value
148
     *
149
     * @return string
150
     */
151
    public function encode($value)
152
    {
153
        return $this->driver()->encode($value);
154
    }
155
156
    /**
157
     * Decode the hashed value.
158
     *
159
     * @param  string  $hashed
160
     *
161
     * @return mixed
162
     */
163
    public function decode($hashed)
164
    {
165
        return $this->driver()->decode($hashed);
166
    }
167
168
    /* ------------------------------------------------------------------------------------------------
169
     |  Other Functions
170
     | ------------------------------------------------------------------------------------------------
171
     */
172
    /**
173
     * Get the hasher config.
174
     *
175
     * @param  string      $name
176
     * @param  mixed|null  $default
177
     *
178
     * @return mixed
179
     */
180
    protected function getHasherConfig($name, $default = null)
181
    {
182
        return $this->app['config']->get("hasher.$name", $default);
183
    }
184
}
185