HasCompositePrimaryKey::getKeyForSaveQuery()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Itstructure\MFU\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
trait HasCompositePrimaryKey
8
{
9
    /**
10
     * @param Builder $query
11
     * @return Builder
12
     */
13
    protected function setKeysForSaveQuery($query)
14
    {
15
        $keys = $this->getKeyName();
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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

15
        /** @scrutinizer ignore-call */ 
16
        $keys = $this->getKeyName();
Loading history...
16
        if(!is_array($keys)){
17
            return parent::setKeysForSaveQuery($query);
18
        }
19
20
        foreach($keys as $keyName){
21
            $query->where($keyName, '=', $this->getKeyForSaveQuery($keyName));
22
        }
23
24
        return $query;
25
    }
26
27
    /**
28
     * @param null $keyName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $keyName is correct as it would always require null to be passed?
Loading history...
29
     * @return mixed
30
     */
31
    protected function getKeyForSaveQuery($keyName = null)
32
    {
33
        if(is_null($keyName)){
0 ignored issues
show
introduced by
The condition is_null($keyName) is always true.
Loading history...
34
            $keyName = $this->getKeyName();
35
        }
36
37
        if (isset($this->original[$keyName])) {
38
            return $this->original[$keyName];
39
        }
40
41
        return $this->getAttribute($keyName);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() 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

41
        return $this->/** @scrutinizer ignore-call */ getAttribute($keyName);
Loading history...
42
    }
43
}