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

ProviderAttribute::getProviderRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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