Passed
Push — master ( 131299...ca4eed )
by Aleksei
24:24 queued 21:58
created

OrmFactory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B __invoke() 0 45 9
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 Spiral\Core\FactoryInterface as SpiralFactoryInterface;
12
use Yiisoft\Injector\Injector;
13
use Yiisoft\Yii\Cycle\Exception\BadDeclarationException;
14
use Yiisoft\Yii\Cycle\Exception\ConfigException;
15
16
/**
17
 * The factory for the ORM Factory {@see FactoryInterface}.
18
 *
19
 * @psalm-type CollectionsConfig = array{
20
 *     default?: string|null,
21
 *     factories?: array<non-empty-string, class-string<CollectionFactoryInterface>>
22
 * }
23
 */
24
final class OrmFactory
25
{
26
    /** @var 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...
27
    private array $collectionsConfig;
28
29
    /**
30
     * @param CollectionsConfig $collectionsConfig
31
     */
32 6
    public function __construct(array $collectionsConfig)
33
    {
34 6
        $this->collectionsConfig = $collectionsConfig;
0 ignored issues
show
Documentation Bug introduced by
It seems like $collectionsConfig of type array is incompatible with the declared type Yiisoft\Yii\Cycle\Factory\CollectionsConfig of property $collectionsConfig.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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