OrmFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 29
c 4
b 0
f 0
dl 0
loc 63
ccs 28
cts 28
cp 1
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B __invoke() 0 45 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Factory;
6
7
use Cycle\Database\DatabaseManager;
8
use Cycle\ORM\Collection\CollectionFactoryInterface;
9
use Cycle\ORM\Factory;
10
use Cycle\ORM\FactoryInterface;
11
use RuntimeException;
12
use Spiral\Core\FactoryInterface as SpiralFactoryInterface;
13
use Yiisoft\Injector\Injector;
14
use Yiisoft\Yii\Cycle\Exception\BadDeclarationException;
15
use Yiisoft\Yii\Cycle\Exception\ConfigException;
16
17
/**
18
 * The factory for the ORM Factory {@see FactoryInterface}.
19
 *
20
 * @psalm-type CollectionsConfig = array{
21
 *     default?: string|null,
22
 *     factories?: array<non-empty-string, class-string<CollectionFactoryInterface>>
23
 * }
24
 */
25
final class OrmFactory
26
{
27
    /**
28
     * @psalm-var CollectionsConfig
29
     */
30
    private array $collectionsConfig;
31
32
    /**
33
     * @param CollectionsConfig $collectionsConfig
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\Cycle\Factory\CollectionsConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     */
35 30
    public function __construct(array $collectionsConfig)
36
    {
37 30
        $this->collectionsConfig = $collectionsConfig;
38
    }
39
40
    /**
41
     * @throws ConfigException
42
     */
43 28
    public function __invoke(
44
        DatabaseManager $dbal,
45
        SpiralFactoryInterface $factory,
46
        Injector $injector,
47
    ): FactoryInterface {
48
        // Manage collection factory list
49 28
        $cfgPath = ['yiisoft/yii-cycle', 'collections'];
50
        try {
51
            // Resolve collection factories
52 28
            $factories = [];
53 28
            foreach ($this->collectionsConfig['factories'] ?? [] as $alias => $definition) {
54 4
                $factories[$alias] = $injector->make($definition);
55 4
                if (!$factories[$alias] instanceof CollectionFactoryInterface) {
56 1
                    $cfgPath[] = 'factories';
57 1
                    throw new BadDeclarationException(
58 1
                        "Collection factory `$alias`",
59 1
                        CollectionFactoryInterface::class,
60 1
                        $factories[$alias]
61 1
                    );
62
                }
63
            }
64
65
            // Resolve default collection factory
66 27
            $default = $this->collectionsConfig['default'] ?? null;
67 27
            if ($default !== null) {
68 3
                if (!\array_key_exists($default, $factories)) {
69 2
                    if (!\is_a($default, CollectionFactoryInterface::class, true)) {
70 1
                        $cfgPath[] = 'default';
71 1
                        throw new RuntimeException(\sprintf('Default collection factory `%s` not found.', $default));
72
                    }
73 1
                    $default = $injector->make($default);
74
                } else {
75 26
                    $default = $factories[$default];
76
                }
77
            }
78 2
        } catch (\Throwable $e) {
79 2
            throw new ConfigException($cfgPath, $e->getMessage(), 0, $e);
80
        }
81
82 26
        $result = new Factory($dbal, null, $factory, $default);
83
        // attach collection factories
84 26
        foreach ($factories as $alias => $collectionFactory) {
85 2
            $result = $result->withCollectionFactory($alias, $collectionFactory);
86
        }
87 26
        return $result;
88
    }
89
}
90