Completed
Push — develop ( 856b14...45ab37 )
by Nate
17:44
created

ProviderAttribute   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 5
dl 0
loc 67
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getProviderId() 0 10 3
A resolveProviderFromRelation() 0 14 3
A getProviderRecord() 0 7 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/patron/license
6
 * @link       https://www.flipboxfactory.com/software/patron/
7
 */
8
9
namespace flipbox\patron\records\traits;
10
11
use flipbox\ember\records\traits\ActiveRecord;
12
use flipbox\patron\Patron;
13
use flipbox\patron\records\Provider;
14
use yii\db\ActiveQueryInterface;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
trait ProviderAttribute
21
{
22
    use ActiveRecord,
23
        ProviderRules,
24
        ProviderMutator {
25
            resolveProvider as parentResolveProvider;
26
    }
27
28
    /**
29
     * Get associated providerId
30
     *
31
     * @return int|null
32
     */
33
    public function getProviderId()
34
    {
35
        $id = $this->getAttribute('providerId');
36
        if (null === $id && null !== $this->provider) {
37
            $id = $this->provider->id;
38
            $this->setAttribute('providerId', $id);
39
        }
40
41
        return $id;
42
    }
43
44
    /**
45
     * @return Provider|null
46
     */
47
    protected function resolveProvider()
48
    {
49
        if ($model = $this->resolveProviderFromRelation()) {
50
            return $model;
51
        }
52
53
        return $this->parentResolveProvider();
0 ignored issues
show
Bug introduced by
The method parentResolveProvider() does not exist on flipbox\patron\records\traits\ProviderAttribute. Did you maybe mean resolveProvider()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
    }
55
56
    /**
57
     * @return Provider|null
58
     */
59
    private function resolveProviderFromRelation()
60
    {
61
        if (false === $this->isRelationPopulated('providerRecord')) {
62
            return null;
63
        }
64
65
        /** @var Provider $record */
66
        $record = $this->getRelation('providerRecord');
67
        if (null === $record) {
68
            return null;
69
        }
70
71
        return Patron::getInstance()->manageProviders()->find($record->id);
72
    }
73
74
    /**
75
     * Returns the associated provider record.
76
     *
77
     * @return ActiveQueryInterface
78
     */
79
    protected function getProviderRecord(): ActiveQueryInterface
80
    {
81
        return $this->hasOne(
82
            Provider::class,
83
            ['id' => 'providerId']
84
        );
85
    }
86
}
87