Passed
Push — master ( 672e2e...25b94b )
by Alexander
01:54
created

ActiveRecordFactory::withConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord;
6
7
use Yiisoft\ActiveRecord\Redis\ActiveQuery as RedisActiveQuery;
8
use Yiisoft\Db\Connection\ConnectionInterface;
9
use Yiisoft\Definitions\Exception\InvalidConfigException;
10
use Yiisoft\Factory\Factory;
11
12
final class ActiveRecordFactory
13
{
14
    /**
15
     * @var Factory
16
     */
17
    private Factory $factory;
18
19
    public function __construct(Factory $factory)
20
    {
21
        $this->factory = $factory;
22
    }
23 2
24
    /**
25 2
     * Allows you to create an active record instance through the factory.
26
     *
27 2
     * @param string $arClass active record class.
28
     * @param ConnectionInterface $connection the database connection used for creating active record instances.
29
     *
30
     * @throws InvalidConfigException
31
     *
32
     * @return ActiveRecordInterface
33
     */
34
    public function createAR(string $arClass, ConnectionInterface $connection = null): ActiveRecordInterface
35
    {
36
        return $this->factory->create(
37
            [
38
                'class' => $arClass,
39
                '__construct()' => ['db' => $connection ?? $this->getConnection()],
40
            ]
41
        );
42 2
    }
43
44 2
    /**
45
     * Allows you to create an active query instance through the factory.
46 2
     *
47
     * @param string $arClass active record class.
48 2
     * @param string|null $queryClass custom query active query class.
49
     * @param ConnectionInterface $connection the database connection used for creating active query instances.
50
     *
51
     * @throws InvalidConfigException
52
     *
53
     * @return ActiveQueryInterface
54
     */
55
    public function createQueryTo(
56
        string $arClass,
57
        string $queryClass = null,
58
        ConnectionInterface $connection = null
59
    ): ActiveQueryInterface {
60
        return $this->factory->create(
61
            [
62
                'class' => $queryClass ?? ActiveQuery::class,
63
                '__construct()' => [
64 1
                    $arClass,
65
                    $connection ?? $this->getConnection(),
66 1
                ],
67
            ]
68 1
        );
69
    }
70 1
71
    /**
72
     * Allows you to create an redis active query instance through the factory.
73
     *
74
     * @param string $arClass active record class.
75
     * @param string|null $queryClass custom query active query class.
76
     * @param ConnectionInterface $connection the database connection used for creating active query instances.
77
     *
78
     * @throws InvalidConfigException
79
     *
80
     * @return ActiveQueryInterface
81
     */
82
    public function createRedisQueryTo(
83
        string $arClass,
84 2
        string $queryClass = null,
85
        ConnectionInterface $connection = null
86 2
    ): ActiveQueryInterface {
87 2
        return $this->factory->create(
88
            [
89
                'class' => $queryClass ?? RedisActiveQuery::class,
90
                '__construct()' => [
91
                    $arClass,
92
                    $connection ?? $this->getConnection(),
93
                ],
94
            ]
95
        );
96 2
    }
97
98 2
    /**
99
     * Returns the active connection at the factory.
100
     *
101
     * @throws InvalidConfigException
102
     *
103
     * @return ConnectionInterface
104
     */
105
    public function getConnection(): ConnectionInterface
106
    {
107
        return $this->factory->create(ConnectionInterface::class);
108
    }
109
}
110