Lines 0.00% 0 / 9
Functions and Methods 0.00% 0 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
BrokerStoreBackendOpener 0.00% 0 / 9 0.00% 0 / 2 12 0.00% 0 / 1
 __construct 0.00% 0 / 1 0.00% 0 / 1 2
 openConfiguredBackend 0.00% 0 / 8 0.00% 0 / 1 6
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7use Psr\Log\LoggerInterface;
8use Stewart\Contracts\Exception\StoreException;
9use Stewart\Runtime\Config\PersistenceConfig;
10use Stewart\Store\Exception\StoreSetupException;
11use Stewart\Store\GuardedStoreBackend;
12use Stewart\Store\StoreBackendConnector;
13use Stewart\Store\StoreTiming;
14
15final readonly class BrokerStoreBackendOpener
16{
17    public function __construct(
18        private StoreBackendConnector $storeBackendConnector,
19        private LoggerInterface $logger,
20        private PersistenceConfig $persistenceConfig,
21    ) {}
22
23    /**
24     * @throws StoreException
25     * @throws StoreSetupException
26     */
27    public function openConfiguredBackend(): ?GuardedStoreBackend
28    {
29        $url = $this->persistenceConfig->url;
30
31        if ($url === null) {
32            return null;
33        }
34
35        $this->logger->debug('Apps will store to ' . $url, ['prefix' => $this->persistenceConfig->prefix->value]);
36
37        return $this->storeBackendConnector->connectToBackend(
38            $url,
39            new StoreTiming($this->persistenceConfig->timeout, $this->persistenceConfig->recoveryInterval),
40        );
41    }
42}