Completed
Pull Request — master (#219)
by Christopher
28:45 queued 09:21
created

MetadataKeyMethodNamesTrait::polyglotRkKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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