Lines 100.00% 7 / 7
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
StoreBackendFactoryCollection 100.00% 7 / 7 100.00% 3 / 3 5 100.00% 1 / 1
 keyedByScheme 100.00% 5 / 5 100.00% 1 / 1 3
 find 100.00% 1 / 1 100.00% 1 / 1 1
 listSchemes 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Store\Collection;
6
7use Stewart\Contracts\Collection\KeyedCollection;
8use Stewart\Store\StoreBackendFactory;
9
10/** @extends KeyedCollection<string, StoreBackendFactory> */
11final readonly class StoreBackendFactoryCollection extends KeyedCollection
12{
13    /** @param iterable<StoreBackendFactory> $factories */
14    public static function keyedByScheme(iterable $factories): self
15    {
16        $factoriesByScheme = [];
17
18        foreach ($factories as $factory) {
19            foreach ($factory->listSchemes() as $scheme) {
20                $factoriesByScheme[strtolower($scheme)] = $factory;
21            }
22        }
23
24        return self::fromElementsByKey($factoriesByScheme);
25    }
26
27    public function find(string $scheme): ?StoreBackendFactory
28    {
29        return $this->elementAt(strtolower($scheme));
30    }
31
32    /** @return list<string> */
33    public function listSchemes(): array
34    {
35        return array_map(strval(...), array_keys($this->elements()));
36    }
37}