JsonFileAdapter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the secrecy/secrecy package.
5
 *
6
 * (c) Webtools Ltd <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Secrecy\Adapter;
15
16
use JsonSchema\Constraints\Constraint;
17
use JsonSchema\Exception\ValidationException;
18
use JsonSchema\Validator;
19
use Safe\Exceptions\FilesystemException;
20
use Safe\Exceptions\JsonException;
21
use function Safe\file_get_contents;
22
use function Safe\file_put_contents;
23
use function Safe\json_decode;
24
use function Safe\json_encode;
25
use Secrecy\Exception\JsonFileLoadException;
26
use Secrecy\Exception\JsonFilePersistenceException;
27
use Secrecy\Exception\SecretAlreadyExistsException;
28
use Secrecy\Exception\SecretNotFoundException;
29
30
class JsonFileAdapter implements AdapterInterface
31
{
32
    /**
33
     * @var string
34
     */
35
    private $path;
36
37
    /**
38
     * @var array<string, array>
39
     */
40
    private $data;
41
42
    /**
43
     * Json schema used to validate the secrets file when loading.
44
     *
45
     * @var array<string, array<int|string, array<string, array<string, string>|string>|string>|string|false>
46
     */
47
    private $schema = [
48
        'type' => 'object',
49
        'properties' => [
50
            'secrets' => ['type' => 'object', 'additionalProperties' => ['type' => 'string']],
51
        ],
52
        'required' => ['secrets'],
53
        'additionalProperties' => false,
54
    ];
55
56
    /**
57
     * JsonFileAdapter constructor.
58
     *
59
     * @throws JsonFileLoadException
60
     */
61
    public function __construct(string $path)
62
    {
63
        $this->path = $path;
64
        $this->load();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function get(string $name): string
71
    {
72
        $this->assertSecretExists($name);
73
74
        return (string) $this->data['secrets'][$name];
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function list(): iterable
81
    {
82
        return $this->data['secrets'];
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @throws JsonFilePersistenceException
89
     */
90
    public function update(string $name, string $value): void
91
    {
92
        $this->assertSecretExists($name);
93
        $this->data['secrets'][$name] = $value;
94
        $this->persist();
95
    }
96
97
    /**
98
     * @throws JsonFilePersistenceException
99
     * @throws SecretAlreadyExistsException
100
     */
101
    public function create(string $name, string $value): void
102
    {
103
        $this->assertSecretDoesNotExist($name);
104
        $this->data['secrets'][$name] = $value;
105
        $this->persist();
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function remove(string $name): void
112
    {
113
        $this->assertSecretExists($name);
114
        unset($this->data['secrets'][$name]);
115
        $this->persist();
116
    }
117
118
    /**
119
     * @throws JsonFileLoadException
120
     */
121
    private function load(): void
122
    {
123
        try {
124
            $validator = new Validator();
125
            $this->data = json_decode(@file_get_contents($this->path), true);
126
            $validator->validate($this->data, $this->schema,
127
                Constraint::CHECK_MODE_EXCEPTIONS | Constraint::CHECK_MODE_TYPE_CAST);
128
        } catch (JsonException | ValidationException | FilesystemException $exception) {
129
            throw new JsonFileLoadException($exception->getMessage(), $exception->getCode(), $exception);
130
        }
131
    }
132
133
    /**
134
     * Data will be encoded and persisted to filesystem, any issues should trigger an exception.
135
     *
136
     * @throws JsonFilePersistenceException
137
     */
138
    private function persist(): void
139
    {
140
        try {
141
            @file_put_contents($this->path, json_encode($this->data, JSON_PRETTY_PRINT));
142
        } catch (FilesystemException | JsonException $exception) {
143
            throw new JsonFilePersistenceException($exception->getMessage(), $exception->getCode(), $exception);
144
        }
145
    }
146
147
    /**
148
     * @throws SecretNotFoundException
149
     */
150
    private function assertSecretExists(string $name): void
151
    {
152
        if (!\array_key_exists($name, $this->data['secrets'])) {
153
            throw SecretNotFoundException::create($name);
154
        }
155
    }
156
157
    /**
158
     * @throws SecretAlreadyExistsException
159
     */
160
    private function assertSecretDoesNotExist(string $name): void
161
    {
162
        if (\array_key_exists($name, $this->data['secrets'])) {
163
            throw SecretAlreadyExistsException::create($name);
164
        }
165
    }
166
}
167