Lines 0.00% 0 / 5
Functions and Methods 0.00% 0 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerStoresFactory 0.00% 0 / 5 0.00% 0 / 2 12 0.00% 0 / 1
 __construct 0.00% 0 / 1 0.00% 0 / 1 2
 createStores 0.00% 0 / 4 0.00% 0 / 1 6
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Worker;
6
7use LogicException;
8use Stewart\Runtime\Ipc\Message\Bootstrap;
9use Stewart\Store\DisabledStores;
10use Stewart\Store\Exception\StoreSetupException;
11use Stewart\Store\KeyspacedStores;
12use Stewart\Store\StoreBackend;
13use Stewart\Store\StorePrefix;
14use Stewart\Store\Stores;
15use Stewart\Store\StoreValueCodec;
16use Stewart\Support\Text\ClosestNameFinder;
17
18final readonly class WorkerStoresFactory
19{
20    public function __construct(
21        private Bootstrap $bootstrap,
22        private StoreValueCodec $codec,
23        private ClosestNameFinder $closestNameFinder,
24        private ?StoreBackend $storeBackend = null,
25    ) {}
26
27    /** @throws StoreSetupException */
28    public function createStores(): Stores
29    {
30        if ($this->storeBackend === null) {
31            return new DisabledStores();
32        }
33
34        $prefix = $this->bootstrap->store->prefix ?? throw new LogicException('A store backend was opened without store settings.');
35
36        return new KeyspacedStores($this->storeBackend, new StorePrefix($prefix), $this->bootstrap->knownAppIds->collection, $this->codec, $this->closestNameFinder);
37    }
38}