Completed
Push — master ( 171614...5fca4d )
by Alex
19s queued 11s
created

MetadataKeyMethodNamesTrait::polyglotRkKey()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 21
rs 9.3554
cc 5
nc 5
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
                $key =  $rel->{$this->checkMethodNameList($rel, ['getForeignKeyName', 'getForeignKey'])}();
26
                break;
27
            case $rel instanceof BelongsToMany:
28
                $key = $rel->getForeignPivotKeyName();
29
                break;
30
            case $rel instanceof HasOneOrMany:
31
                $key = $rel->getForeignKeyName();
32
                break;
33
            case $rel instanceof HasManyThrough:
34
                $key =  $rel->getQualifiedFarKeyName();
35
                break;
36
            default:
37
                $msg = sprintf('Unknown Relationship Type %s', get_class($rel));
38
                throw new InvalidOperationException($msg);
39
        }
40
        $segments = explode('.', $key);
41
        return end($segments);
42
    }
43
44
    /**
45
     * @param Relation $rel
46
     * @return mixed
47
     * @throws InvalidOperationException
48
     */
49
    protected function polyglotRkKey(Relation $rel)
50
    {
51
        switch (true) {
52
            case $rel instanceof BelongsTo:
53
                $key = $rel->{$this->checkMethodNameList($rel, ['getOwnerKey', 'getOwnerKeyName'])}();
54
                break;
55
            case $rel instanceof BelongsToMany:
56
                $key = $rel->getRelatedPivotKeyName();
57
                break;
58
            case $rel instanceof HasOneOrMany:
59
                $key = $rel->{$this->checkMethodNameList($rel, ['getLocalKeyName', 'getQualifiedParentKeyName'])}();
60
                break;
61
            case $rel instanceof HasManyThrough:
62
                $key = $rel->getQualifiedParentKeyName();
63
                break;
64
            default:
65
                $msg = sprintf('Unknown Relationship Type %s', get_class($rel));
66
                throw new InvalidOperationException($msg);
67
        }
68
        $segments = explode('.', $key);
69
        return end($segments);
70
    }
71
72
    /**
73
     * @param Relation $rel
74
     * @return mixed
75
     * @throws InvalidOperationException
76
     */
77
    protected function polyglotThroughKey(Relation $rel)
78
    {
79
        $key = $rel->{$this->checkMethodNameList($rel, ['getThroughKey', 'getQualifiedFirstKeyName'])}();
80
        $segments = explode('.', $key);
81
        return end($segments);
82
    }
83
84
    /**
85
     * @param  Model $model
86
     * @return array
87
     */
88
    protected function getModelClassMethods(Model $model)
89
    {
90
        // TODO: Handle case when Mock::class not present
91
        return array_diff(
92
            get_class_methods($model),
93
            get_class_methods(\Illuminate\Database\Eloquent\Model::class),
94
            get_class_methods(Mock::class),
95
            get_class_methods(MetadataTrait::class)
96
        );
97
    }
98
99
    /**
100
     * @param  Relation                  $foo
101
     * @param  array                     $methodList
102
     * @throws InvalidOperationException
103
     * @return string
104
     */
105
    protected function checkMethodNameList(Relation $foo, array $methodList)
106
    {
107
        foreach ($methodList as $methodName) {
108
            if (method_exists($foo, $methodName)) {
109
                return $methodName;
110
            }
111
        }
112
        $msg = 'Expected at least 1 element in related-key list, got 0 for relation %s';
113
        throw new InvalidOperationException(sprintf($msg, get_class($foo)));
114
    }
115
}
116