Completed
Push — master ( 4ac004...470d43 )
by Gabriel
06:41
created

HasPivotTable   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 55
ccs 14
cts 18
cp 0.7778
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addPivotParams() 0 5 1
A getDB() 0 4 2
A generateTable() 0 4 1
A generatePivotTable() 0 10 1
A hydrateQueryWithPivotConstraints() 0 6 1
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getWith() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
50 1
            $this->getWith()->getTable()
0 ignored issues
show
Bug introduced by
It seems like getWith() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like getWith() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
65 1
    }
66
}
67