Completed
Push — master ( 470d43...3f2648 )
by Gabriel
03:36
created

HasPivotTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 76.47%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 53
ccs 13
cts 17
cp 0.7647
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addPivotParams() 0 4 1
A generatePivotTable() 0 9 1
A generateTable() 0 3 1
A hydrateQueryWithPivotConstraints() 0 5 1
A getDB() 0 3 2
1
<?php
2
3
namespace Nip\Records\Relations\Traits;
4
5
use Nip\Database\Connections\Connection;
6
use Nip\Database\Query\Select as SelectQuery;
7
8
/**
9
 * Trait HasPivotTable
10
 * @package Nip\Records\Relations\Traits
11
 */
12
trait HasPivotTable
13
{
14
    use HasPivotForeignKey;
15
    use HasPivotPrimaryKey;
16
17
    /**
18
     * @param $params
19
     */
20
    public function addPivotParams($params)
21
    {
22
        $this->checkParamPivotFk($params);
23
        $this->checkParamPivotPrimaryKey($params);
24
    }
25
26
    /**
27
     * @return Connection
28
     */
29 1
    public function getDB()
30
    {
31 1
        return $this->getParam("link-db") == 'with' ? $this->getWith()->getDB() : parent::getDB();
0 ignored issues
show
Bug introduced by
It seems like getParam() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        return $this->/** @scrutinizer ignore-call */ getParam("link-db") == 'with' ? $this->getWith()->getDB() : parent::getDB();
Loading history...
32
    }
33
34
    /** @noinspection PhpMissingParentCallCommonInspection
35
     * @return string
36
     */
37 2
    public function generateTable()
38
    {
39 2
        return $this->generatePivotTable();
40
    }
41
42
    /**
43
     * Builds the name of a has-and-belongs-to-many association table
44
     * @return string
45
     */
46 1
    public function generatePivotTable()
47
    {
48
        $tables = [
49 1
            $this->getManager()->getTable(),
0 ignored issues
show
Bug introduced by
It seems like getManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
            $this->/** @scrutinizer ignore-call */ 
50
                   getManager()->getTable(),
Loading history...
50 1
            $this->getWith()->getTable()
51
        ];
52 1
        sort($tables);
53
54 1
        return implode("_", $tables);
55
    }
56
57
    /**
58
     * @param SelectQuery $query
59
     */
60 1
    protected function hydrateQueryWithPivotConstraints($query)
61
    {
62 1
        $pk = $this->getPivotPrimaryKey();
63 1
        $fk = $this->getPivotFK();
64 1
        $query->where("`{$this->getTable()}`.`$fk` = `{$this->getWith()->getTable()}`.`$pk`");
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $query->where("`{$this->/** @scrutinizer ignore-call */ getTable()}`.`$fk` = `{$this->getWith()->getTable()}`.`$pk`");
Loading history...
65 1
    }
66
}
67