Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

ManageTokens::saveEnvironments()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 18
cp 0
rs 9.0648
c 0
b 0
f 0
cc 5
nc 8
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\services;
10
11
use flipbox\ember\helpers\ArrayHelper;
12
use flipbox\ember\services\traits\records\Accessor;
13
use flipbox\patron\db\TokenQuery;
14
use flipbox\patron\Patron;
15
use flipbox\patron\records\Token;
16
use League\OAuth2\Client\Provider\AbstractProvider;
17
use League\OAuth2\Client\Token\AccessToken;
18
use yii\base\Component;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 *
24
 * @method Token create(array $attributes = [])
25
 * @method TokenQuery getQuery($config = [])
26
 * @method Token parentFind($identifier)
27
 * @method Token get($identifier)
28
 * @method Token findByCondition($condition = [])
29
 * @method Token getByCondition($condition = [])
30
 * @method Token findByCriteria($criteria = [])
31
 * @method Token getByCriteria($criteria = [])
32
 * @method Token[] findAllByCondition($condition = [])
33
 * @method Token[] getAllByCondition($condition = [])
34
 * @method Token[] findAllByCriteria($criteria = [])
35
 * @method Token[] getAllByCriteria($criteria = [])
36
 */
37
class ManageTokens extends Component
38
{
39
    use Accessor {
40
        find as parentFind;
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public static function recordClass(): string
47
    {
48
        return Token::class;
49
    }
50
51
    /**
52
     * @param array $config
53
     * @return array
54
     */
55
    protected function prepareQueryConfig($config = [])
56
    {
57
        if (!is_array($config)) {
58
            $config = ArrayHelper::toArray($config, [], true);
59
        }
60
61
        // Allow disabled when managing
62
        if (!array_key_exists('enabled', $config)) {
63
            $config['enabled'] = null;
64
        }
65
66
        // Allow all environments when managing
67
        if (!array_key_exists('environment', $config)) {
68
            $config['environment'] = null;
69
        }
70
71
        return $config;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function find($identifier)
78
    {
79
        if ($identifier instanceof AccessToken) {
80
            return $this->findByAccessToken($identifier);
81
        }
82
83
        if ($identifier instanceof AbstractProvider) {
84
            return $this->findByProvider($identifier);
85
        }
86
87
        return $this->parentFind($identifier);
88
    }
89
90
    /*******************************************
91
     * FIND/GET BY ACCESS TOKEN
92
     *******************************************/
93
94
    /**
95
     * @param AccessToken $accessToken
96
     * @return Token|null
97
     */
98
    public function findByAccessToken(AccessToken $accessToken)
99
    {
100
        return $this->findByCriteria(['accessToken' => $accessToken->getToken()]);
101
    }
102
103
    /*******************************************
104
     * FIND/GET BY PROVIDER
105
     *******************************************/
106
107
    /**
108
     * @param AbstractProvider $provider
109
     * @return Token|null
110
     */
111
    public function findByProvider(AbstractProvider $provider)
112
    {
113
        if (null === ($providerId = Patron::getInstance()->getProviders()->getId($provider))) {
114
            return null;
115
        }
116
117
        return $this->findByCriteria(['providerId' => $providerId]);
118
    }
119
120
    /*******************************************
121
     * STATES
122
     *******************************************/
123
124
    /**
125
     * @param Token $record
126
     * @return bool
127
     */
128
    public function disable(Token $record)
129
    {
130
        $record->enabled = false;
131
        return $record->save(true, ['enabled']);
132
    }
133
134
    /**
135
     * @param Token $model
136
     * @return bool
137
     */
138
    public function enable(Token $model)
139
    {
140
        $model->enabled = true;
141
        return $model->save(true, ['enabled']);
142
    }
143
}
144