Lines 100.00% 6 / 6
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
StoreDsn 100.00% 6 / 6 100.00% 4 / 4 5 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 parse 100.00% 3 / 3 100.00% 1 / 1 2
 reveal 100.00% 1 / 1 100.00% 1 / 1 1
 __toString 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Store;
6
7use SensitiveParameter;
8use Stewart\Store\Exception\StoreSetupException;
9use Stewart\Support\Url\UrlRedactor;
10use Stringable;
11
12final readonly class StoreDsn implements Stringable
13{
14    private const string SCHEME_PATTERN = '~^([a-z][a-z0-9+.-]*)://~i';
15
16    private function __construct(
17        #[SensitiveParameter]
18        private string $dsn,
19        public string $scheme,
20        private string $redacted,
21    ) {}
22
23    /** @throws StoreSetupException */
24    public static function parse(#[SensitiveParameter] string $dsn): self
25    {
26        if (preg_match(self::SCHEME_PATTERN, $dsn, $matches) !== 1) {
27            throw StoreSetupException::dsnInvalid($dsn);
28        }
29
30        return new self($dsn, strtolower($matches[1]), UrlRedactor::redactCredentials($dsn));
31    }
32
33    public function reveal(): string
34    {
35        return $this->dsn;
36    }
37
38    public function __toString(): string
39    {
40        return $this->redacted;
41    }
42}