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

ProviderMutator::internalResolveProvider()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 12
cp 0
rs 9.2408
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 30
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 Craft;
12
use flipbox\ember\helpers\ObjectHelper;
13
use flipbox\patron\Patron;
14
use flipbox\patron\records\Provider;
15
16
/**
17
 * @property int|null $providerId
18
 * @property Provider|null $provider
19
 *
20
 * @author Flipbox Factory <[email protected]>
21
 * @since 1.0.0
22
 */
23
trait ProviderMutator
24
{
25
    /**
26
     * @var Provider|null
27
     */
28
    private $provider;
29
30
    /**
31
     * Set associated providerId
32
     *
33
     * @param $id
34
     * @return $this
35
     */
36
    public function setProviderId(int $id)
37
    {
38
        $this->providerId = $id;
39
        return $this;
40
    }
41
42
    /**
43
     * Get associated providerId
44
     *
45
     * @return int|null
46
     */
47
    public function getProviderId()
48
    {
49
        if (null === $this->providerId && null !== $this->provider) {
50
            $this->providerId = $this->provider->id;
51
        }
52
53
        return $this->providerId;
54
    }
55
56
    /**
57
     * Associate a provider
58
     *
59
     * @param mixed $provider
60
     * @return $this
61
     */
62
    public function setProvider($provider = null)
63
    {
64
        $this->provider = null;
65
66
        if (null === ($provider = $this->internalResolveProvider($provider))) {
67
            $this->provider = $this->providerId = null;
68
        } else {
69
            $this->providerId = $provider->id;
70
            $this->provider = $provider;
71
        }
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return Provider|null
78
     */
79
    public function getProvider()
80
    {
81
        if ($this->provider === null) {
82
            $provider = $this->resolveProvider();
83
            $this->setProvider($provider);
84
            return $provider;
85
        }
86
87
        $providerId = $this->providerId;
88
        if ($providerId !== null &&
89
            $providerId !== $this->provider->id
90
        ) {
91
            $this->provider = null;
92
            return $this->getProvider();
93
        }
94
95
        return $this->provider;
96
    }
97
98
    /**
99
     * @return Provider|null
100
     */
101
    protected function resolveProvider()
102
    {
103
        if ($model = $this->resolveProviderFromId()) {
104
            return $model;
105
        }
106
107
        return null;
108
    }
109
110
    /**
111
     * @return Provider|null
112
     */
113
    private function resolveProviderFromId()
114
    {
115
        if (null === $this->providerId) {
116
            return null;
117
        }
118
119
        return Patron::getInstance()->manageProviders()->get($this->providerId);
120
    }
121
122
    /**
123
     * @param $provider
124
     * @return Provider|null
125
     */
126
    protected function internalResolveProvider($provider = null)
127
    {
128
        if ($provider instanceof Provider) {
129
            return $provider;
130
        }
131
132
        if (is_numeric($provider) || is_string($provider)) {
133
            return Patron::getInstance()->manageProviders()->find($provider);
134
        }
135
136
        try {
137
            $object = Craft::createObject(Provider::class, [$provider]);
138
        } catch (\Exception $e) {
139
            $object = new Provider();
140
            ObjectHelper::populate(
141
                $object,
142
                $provider
143
            );
144
        }
145
146
        /** @var Provider $object */
147
        return $object;
148
    }
149
}
150