HashManager::getDefaultOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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