Completed
Pull Request — master (#219)
by Christopher
08:26 queued 03:44
created

MetadataKeyMethodNamesTrait::polyglotThroughKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alex
5
 * Date: 13/02/20
6
 * Time: 4:22 AM.
7
 */
8
namespace AlgoWeb\PODataLaravel\Models;
9
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\BelongsTo;
12
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
14
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
15
use Illuminate\Database\Eloquent\Relations\Relation;
16
use Mockery\Mock;
17
use POData\Common\InvalidOperationException;
18
19
trait MetadataKeyMethodNamesTrait
20
{
21
    protected function polyglotFkKey(Relation $rel)
22
    {
23
        switch (true) {
24
            case $rel instanceof BelongsTo:
25
                return $rel->{$this->checkMethodNameList($rel, ['getForeignKeyName', 'getForeignKey'])}();
26
            case $rel instanceof BelongsToMany:
27
                return $rel->getForeignPivotKeyName();
28
            case $rel instanceof HasOneOrMany:
29
                return $rel->getForeignKeyName();
30
            case $rel instanceof HasManyThrough:
31
                $segments = explode('.', $rel->getQualifiedFarKeyName());
32
                return end($segments);
33
            default:
34
                $msg = sprintf('Unknown Relationship Type %s', get_class($rel));
35
                throw new InvalidOperationException($msg);
36
        }
37
    }
38
    protected function polyglotRkKey(Relation $rel)
39
    {
40
        switch (true) {
41
            case $rel instanceof BelongsTo:
42
                return $rel->{$this->checkMethodNameList($rel, ['getOwnerKey', 'getOwnerKeyName'])}();
43
            case $rel instanceof BelongsToMany:
44
                return $rel->getRelatedPivotKeyName();
45
            case $rel instanceof HasOneOrMany:
46
                $segments = explode('.', $rel->{$this->checkMethodNameList($rel, ['getLocalKeyName', 'getQualifiedParentKeyName'])}());
47
                return end($segments);
48
            case $rel instanceof HasManyThrough:
49
                $segments = explode('.', $rel->getQualifiedParentKeyName());
50
                return end($segments);
51
            default:
52
                $msg = sprintf('Unknown Relationship Type %s', get_class($rel));
53
                throw new InvalidOperationException($msg);
54
        }
55
    }
56
57
    protected function polyglotThroughKey(Relation $rel){
58
        if(! $rel instanceof HasManyThrough){
59
            return null;
60
        }
61
        $segments = explode('.', $rel->{$this->checkMethodNameList($rel, ['getThroughKey', 'getQualifiedFirstKeyName'])}());
62
        return end($segments);
63
    }
64
65
    /**
66
     * @param  Model $model
67
     * @return array
68
     */
69
    protected function getModelClassMethods(Model $model)
70
    {
71
        return array_diff(
72
            get_class_methods($model),
73
            get_class_methods(\Illuminate\Database\Eloquent\Model::class),
74
            //TODO: sandi what will happen if Mock is not installed (I.e. Production?)
75
            get_class_methods(Mock::class),
76
            get_class_methods(MetadataTrait::class)
77
        );
78
    }
79
80
    /**
81
     * @param  Relation                  $foo
82
     * @param  array                     $methodList
83
     * @throws InvalidOperationException
84
     * @return string
85
     */
86
    protected function checkMethodNameList(Relation $foo, array $methodList)
87
    {
88
        foreach ($methodList as $methodName) {
89
            if (method_exists($foo, $methodName)) {
90
                return $methodName;
91
            }
92
        }
93
        $msg = 'Expected at least 1 element in related-key list, got 0 for relation %s';
94
        throw new InvalidOperationException(sprintf($msg,get_class($foo)));
95
    }
96
}
97