ActiveRecordFactory   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 17
c 1
b 0
f 0
dl 0
loc 79
ccs 14
cts 14
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A createQueryTo() 0 22 3
A createAR() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\ActiveRecord;
6
7
use Yiisoft\Db\Connection\ConnectionInterface;
8
use Yiisoft\Definitions\Exception\CircularReferenceException;
9
use Yiisoft\Definitions\Exception\InvalidConfigException;
10
use Yiisoft\Definitions\Exception\NotInstantiableException;
11
use Yiisoft\Factory\Factory;
12
use Yiisoft\Factory\NotFoundException;
13
14
final class ActiveRecordFactory
15
{
16
    public function __construct(private Factory $factory)
17
    {
18
    }
19
20
    /**
21
     * Allows you to create an active record instance through the factory.
22
     *
23 2
     * @param string $arClass active record class.
24
     * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the
25 2
     * name will be generated automatically by calling {@see getTableName()} in the active record class.
26
     * @param ConnectionInterface|null $db the database connection used for creating active record instances.
27 2
     *
28
     * @throws CircularReferenceException
29
     * @throws InvalidConfigException
30
     * @throws NotFoundException
31
     * @throws NotInstantiableException
32
     * @return ActiveRecordInterface
33
     *
34
     * @psalm-template T of ActiveRecordInterface
35
     * @psalm-param class-string<T> $arClass
36
     * @psalm-return T
37
     */
38
    public function createAR(
39
        string $arClass,
40
        string $tableName = '',
41
        ConnectionInterface $db = null
42 2
    ): ActiveRecordInterface {
43
        $params = [];
44 2
        $params['class'] = $arClass;
45
46 2
        if ($tableName !== '') {
47
            $params['__construct()']['tableName'] = $tableName;
48 2
        }
49
50
        if ($db !== null) {
51
            $params['__construct()']['db'] = $db;
52
        }
53
54
        return $this->factory->create($params);
55
    }
56
57
    /**
58
     * Allows you to create an active query instance through the factory.
59
     *
60
     * @param string $arClass active record class.
61
     * @param string $tableName The name of the table associated with this ActiveRecord class, if its empty string the
62
     * name will be generated automatically by calling {@see getTableName()} in the active record class.
63
     * @param string $queryClass custom query active query class.
64 1
     * @param ConnectionInterface|null $db the database connection used for creating active query instances.
65
     *
66 1
     * @throws CircularReferenceException
67
     * @throws InvalidConfigException
68 1
     * @throws NotFoundException
69
     * @throws NotInstantiableException
70 1
     */
71
    public function createQueryTo(
72
        string $arClass,
73
        string $tableName = '',
74
        string $queryClass = ActiveQuery::class,
75
        ConnectionInterface $db = null
76
    ): ActiveQueryInterface {
77
        $params = [
78
            'class' => $queryClass,
79
            '__construct()' => [
80
                'arClass' => $arClass,
81
            ],
82
        ];
83
84 2
        if ($tableName !== '') {
85
            $params['__construct()']['tableName'] = $tableName;
86 2
        }
87 2
88
        if ($db !== null) {
89
            $params['__construct()']['db'] = $db;
90
        }
91
92
        return $this->factory->create($params);
93
    }
94
}
95