Passed
Push — master ( d01bf5...5a7f24 )
by Alexander
14:14
created

ActiveRecordFactory::createQueryTo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 7
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\Factory\Exceptions\InvalidConfigException;
10
use Yiisoft\Factory\Factory;
11
12
final class ActiveRecordFactory extends Factory
13
{
14
    /**
15
     * Allows you to create an active record instance through the factory.
16
     *
17
     * @param string $arClass active record class.
18
     *
19
     * @throws InvalidConfigException
20
     *
21
     * @return ActiveRecordInterface
22
     */
23
    public function createAR(string $arClass): ActiveRecordInterface
24
    {
25
        return $this->create(
26
            [
27
                '__class' => $arClass
28
            ]
29
        );
30
    }
31
32
    /**
33
     * Allows you to create an active query instance through the factory.
34
     *
35
     * @param string $arClass active record class.
36
     * @param string|null $queryClass custom query active query class.
37
     *
38
     * @throws InvalidConfigException
39
     *
40
     * @return ActiveQueryInterface
41
     */
42
    public function createQueryTo(string $arClass, ?string $queryClass = null): ActiveQueryInterface
43
    {
44
        return $this->create(
45
            [
46
                '__class' => $queryClass ?? ActiveQuery::class,
47
                '__construct()' => [
48
                    $arClass
49
                ]
50
            ]
51
        );
52
    }
53
54
    /**
55
     * Allows you to create an redis active query instance through the factory.
56
     *
57
     * @param string $arClass active record class.
58
     * @param string|null $queryClass custom query active query class.
59
     *
60
     * @throws InvalidConfigException
61
     *
62
     * @return ActiveQueryInterface
63
     */
64
    public function createRedisQueryTo(string $arClass, ?string $queryClass = null): ActiveQueryInterface
65
    {
66
        return $this->create(
67
            [
68
                '__class' => $queryClass ?? RedisActiveQuery::class,
69
                '__construct()' => [
70
                    $arClass
71
                ]
72
            ]
73
        );
74
    }
75
76
    /**
77
     * Allows you to configure the connection that will be used in the factory, through
78
     * {@see ConnectionInterface::class}.
79
     *
80
     * @throws InvalidConfigException
81
     *
82
     * @param ConnectionInterface $connection connection defined in container-di.
83
     */
84
    public function withConnection(ConnectionInterface $connection): void
85
    {
86
        $this->set(ConnectionInterface::class, $connection);
87
    }
88
89
    /**
90
     * Returns the active connection at the factory.
91
     *
92
     * @throws InvalidConfigException
93
     *
94
     * @return ConnectionInterface
95
     */
96
    public function getConnection(): ConnectionInterface
97
    {
98
        return $this->create(ConnectionInterface::class);
99
    }
100
}
101