Completed
Push — develop ( 55c38a...c1b735 )
by Nate
12:53
created

TokenProviderAttributeTrait::resolveProviderValue()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 17
cp 0
rs 8.9777
c 0
b 0
f 0
cc 6
nc 18
nop 2
crap 42
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/patron/domains/
7
 */
8
9
namespace flipbox\patron\queries;
10
11
use craft\helpers\Db;
12
use flipbox\craft\ember\helpers\QueryHelper;
13
use flipbox\patron\records\Provider as ProviderRecord;
14
use flipbox\patron\records\Token;
15
use League\OAuth2\Client\Provider\AbstractProvider;
16
use yii\db\Query;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
trait TokenProviderAttributeTrait
23
{
24
    /**
25
     * The provider(s) that the resulting must have.
26
     *
27
     * @var string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null
28
     */
29
    public $provider;
30
31
    /**
32
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
33
     * @return static The query object
34
     */
35
    public function setProvider($value)
36
    {
37
        $this->provider = $value;
38
        return $this;
39
    }
40
41
    /**
42
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
43
     * @return static The query object
44
     */
45
    public function provider($value)
46
    {
47
        return $this->setProvider($value);
48
    }
49
50
    /**
51
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
52
     * @return static The query object
53
     */
54
    public function setProviderId($value)
55
    {
56
        return $this->setProvider($value);
57
    }
58
59
    /**
60
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
61
     * @return static The query object
62
     */
63
    public function providerId($value)
64
    {
65
        return $this->setProvider($value);
66
    }
67
68
    /**
69
     * @param $value
70
     * @return array|string
71
     */
72
    protected function parseProviderValue($value)
73
    {
74
        return QueryHelper::prepareParam(
75
            $value,
76
            function (string $identifier) {
77
                $value = (new Query())
78
                    ->select(['id'])
79
                    ->from([ProviderRecord::tableName()])
80
                    ->where(['handle' => $identifier])
81
                    ->scalar();
82
                return empty($value) ? false : $value;
83
            }
84
        );
85
    }
86
87
    /**
88
     * @throws \ReflectionException
89
     */
90
    protected function applyProviderConditions()
91
    {
92
        if (empty($this->provider)) {
93
            return;
94
        }
95
96
        $this->andWhere(
0 ignored issues
show
Bug introduced by
It seems like andWhere() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
97
            Db::parseParam(Token::tableAlias() . '.providerId', $this->parseProviderValue($this->provider))
98
        );
99
    }
100
}
101