RowRelations::getRelations()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Db\Traits;
13
14
use Bluz\Db\Relations;
15
use Bluz\Db\Row;
16
use Bluz\Db\Exception\RelationNotFoundException;
17
use Bluz\Db\Exception\TableNotFoundException;
18
use Bluz\Db\RowInterface;
19
20
/**
21
 * RowRelations
22
 *
23
 * @package  Bluz\Db\Traits
24
 * @author   Anton Shevchuk
25
 */
26
trait RowRelations
27
{
28
    /**
29
     * @var array relations rows
30
     */
31
    protected $relations = [];
32
33
    /**
34
     * Set relation
35
     *
36
     * @param  Row $row
37
     *
38
     * @return void
39
     * @throws TableNotFoundException
40
     */
41
    public function setRelation(Row $row): void
42
    {
43
        $modelName = $row->getTable()->getModel();
44
        $this->relations[$modelName] = [$row];
45
    }
46
47
    /**
48
     * Get relation by model name
49
     *
50
     * @param  string $modelName
51
     *
52
     * @return RowInterface
53
     * @throws RelationNotFoundException
54
     * @throws TableNotFoundException
55
     */
56 1
    public function getRelation($modelName): ?RowInterface
57
    {
58 1
        $relations = $this->getRelations($modelName);
59
        return empty($relations) ? null : current($relations);
60
    }
61
62
    /**
63
     * Get relations by model name
64
     *
65
     * @param  string $modelName
66
     *
67
     * @return RowInterface[]
68
     * @throws RelationNotFoundException
69
     * @throws TableNotFoundException
70
     */
71 1
    public function getRelations($modelName): array
72
    {
73 1
        if (!isset($this->relations[$modelName])) {
74 1
            $this->relations[$modelName] = Relations::findRelation($this, $modelName);
75
        }
76
77
        return $this->relations[$modelName];
0 ignored issues
show
Bug Best Practice introduced by
The property $relations is declared protected in Bluz\Db\Row. Since you implement __get, consider adding a @property or @property-read.
Loading history...
78
    }
79
}
80