ConfigProvider::getHashAlgorithm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Scavenger\Service;
6
7
use Exception;
8
use Illuminate\Contracts\Auth\Authenticatable;
9
use Illuminate\Support\Facades\Config;
10
use Illuminate\Support\Facades\Hash;
11
use ReliqArts\Scavenger\Contract\ConfigProvider as ConfigProviderContract;
12
use ReliqArts\Scavenger\Exception\BadDaemonConfig;
13
14
final class ConfigProvider implements ConfigProviderContract
15
{
16
    private const DEFAULT_GUZZLE_SETTINGS = [
17
        'timeout' => 60,
18
    ];
19
20
    /**
21
     * Get config.
22
     */
23
    public function get(): array
24
    {
25
        return Config::get('scavenger', []);
26
    }
27
28
    public function getGuzzleSettings(): array
29
    {
30
        return Config::get('scavenger.guzzle_settings', self::DEFAULT_GUZZLE_SETTINGS);
31
    }
32
33
    /**
34
     * Get targets.
35
     */
36
    public function getTargets(): array
37
    {
38
        return Config::get('scavenger.targets', []);
39
    }
40
41
    /**
42
     * Get scavenger daemon (user) instance. Creates daemon if he doesn't exist.
43
     *
44
     * @throws BadDaemonConfig
45
     */
46
    public function getDaemon(): Authenticatable
47
    {
48
        $badDaemonConfigMessage = 'Scavenger daemon does not exist and could not be created. Check database config.';
49
        $daemon = $this->getDaemonModel()
50
            ->where(
0 ignored issues
show
Bug introduced by
The method where() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

50
            ->/** @scrutinizer ignore-call */ where(

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...
51
                $this->getDaemonModelIdProp(),
52
                $this->getDaemonModelId()
53
            )
54
            ->first();
55
56
        if (!$daemon) {
57
            // attempt to create
58
            try {
59
                $daemon = $this->getDaemonModel()
60
                    ->create($this->getDaemonInfo());
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

60
                    ->/** @scrutinizer ignore-call */ create($this->getDaemonInfo());

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...
61
            } catch (Exception $exception) {
62
                // fail, could not create daemon user
63
                throw new BadDaemonConfig(sprintf('%s %s', $badDaemonConfigMessage, $exception->getMessage()), $exception->getCode(), $exception);
64
            }
65
        }
66
67
        return $daemon;
68
    }
69
70
    /**
71
     * Get daemon model.
72
     */
73
    public function getDaemonModel()
74
    {
75
        return resolve($this->getDaemonModelName());
76
    }
77
78
    /**
79
     * Get daemon model name.
80
     */
81
    public function getDaemonModelName(): string
82
    {
83
        return Config::get('scavenger.daemon.model', 'App\\User');
84
    }
85
86
    /**
87
     * Get ID property  for daemon.
88
     */
89
    public function getDaemonModelIdProp(): string
90
    {
91
        return Config::get('scavenger.daemon.id_prop', 'email');
92
    }
93
94
    /**
95
     * Get ID property value for daemon.
96
     */
97
    public function getDaemonModelId(): string
98
    {
99
        return Config::get('scavenger.daemon.id', '[email protected]');
100
    }
101
102
    /**
103
     * Get attribute values for daemon.
104
     */
105
    public function getDaemonInfo(): array
106
    {
107
        $infoConfig = Config::get('scavenger.daemon.info', []);
108
        $info = array_merge($infoConfig, [
109
            $this->getDaemonModelIdProp() => $this->getDaemonModelId(),
110
        ]);
111
        if (!empty($infoConfig['password'])) {
112
            // hash password
113
            $info['password'] = Hash::make($infoConfig['password']);
114
        }
115
116
        return $info;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getHashAlgorithm(): string
123
    {
124
        $algorithm = Config::get('scavenger.hash_algorithm');
125
126
        if (empty($algorithm)) {
127
            return self::DEFAULT_HASH_ALGORITHM;
128
        }
129
130
        return $algorithm;
131
    }
132
133
    public function getLogDir(): string
134
    {
135
        return $this->get()['storage']['log_dir'] ?? 'scavenger';
136
    }
137
138
    public function getVerbosity(): int
139
    {
140
        $verbosity = (int)Config::get('scavenger.verbosity');
141
142
        if ($verbosity === 0) {
143
            return self::DEFAULT_VERBOSITY;
144
        }
145
146
        return $verbosity;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function isLoggingEnabled(): bool
153
    {
154
        return (bool)Config::get('scavenger.log');
155
    }
156
157
    /**
158
     * Get scavenger scraps table.
159
     */
160
    public static function getScrapsTable(): string
161
    {
162
        return Config::get('scavenger.database.scraps_table', 'scavenger_scraps');
163
    }
164
165
    /**
166
     * Convert config key name to special key.
167
     */
168
    public static function specialKey(string $keyName): string
169
    {
170
        return self::SPECIAL_KEY_PREFIX . $keyName;
171
    }
172
173
    /**
174
     * Check if key name is config key/special key name.
175
     */
176
    public static function isSpecialKey(?string $keyName): bool
177
    {
178
        if ($keyName === null) {
179
            return false;
180
        }
181
182
        return strpos($keyName, self::SPECIAL_KEY_PREFIX) === 0;
183
    }
184
}
185