TokenProviderAttributeTrait::setProvider()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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/patron/domains/
7
 */
8
9
namespace flipbox\patron\queries;
10
11
use craft\db\QueryAbortedException;
12
use craft\helpers\Db;
13
use flipbox\craft\ember\helpers\QueryHelper;
14
use flipbox\patron\helpers\ProviderHelper;
15
use flipbox\patron\records\Provider as ProviderRecord;
16
use flipbox\patron\records\Token;
17
use League\OAuth2\Client\Provider\AbstractProvider;
18
use yii\db\Query;
19
20
/**
21
 * @author Flipbox Factory <[email protected]>
22
 * @since 1.0.0
23
 */
24
trait TokenProviderAttributeTrait
25
{
26
    /**
27
     * The provider(s) that the resulting must have.
28
     *
29
     * @var string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null
30
     */
31
    public $provider;
32
33
    /**
34
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
35
     * @return static The query object
36
     */
37
    public function setProvider($value)
38
    {
39
        $this->provider = $value;
40
        return $this;
41
    }
42
43
    /**
44
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
45
     * @return static The query object
46
     */
47
    public function provider($value)
48
    {
49
        return $this->setProvider($value);
50
    }
51
52
    /**
53
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
54
     * @return static The query object
55
     */
56
    public function setProviderId($value)
57
    {
58
        return $this->setProvider($value);
59
    }
60
61
    /**
62
     * @param string|string[]|int|int[]|ProviderRecord|ProviderRecord[]|AbstractProvider|AbstractProvider[]|null $value
63
     * @return static The query object
64
     */
65
    public function providerId($value)
66
    {
67
        return $this->setProvider($value);
68
    }
69
70
    /**
71
     * @param $value
72
     * @return array|string
73
     * @throws QueryAbortedException
74
     * @throws \ReflectionException
75
     */
76
    protected function parseProviderValue($value)
77
    {
78
        if ($value instanceof AbstractProvider) {
79
            $value = ProviderHelper::lookupId($value);
80
        }
81
82
        $return = QueryHelper::prepareParam(
83
            $value,
84
            function (string $identifier) {
85
                $value = (new Query())
86
                    ->select(['id'])
87
                    ->from([ProviderRecord::tableName()])
88
                    ->where(['handle' => $identifier])
89
                    ->scalar();
90
                return empty($value) ? false : $value;
91
            }
92
        );
93
94
        if ($return !== null && empty($return)) {
95
            throw new QueryAbortedException();
96
        }
97
98
        return $return;
99
    }
100
101
    /**
102
     * @throws QueryAbortedException
103
     * @throws \ReflectionException
104
     */
105
    protected function applyProviderConditions()
106
    {
107
        if (empty($this->provider)) {
108
            return;
109
        }
110
111
        $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...
112
            Db::parseParam(Token::tableAlias() . '.providerId', $this->parseProviderValue($this->provider))
113
        );
114
    }
115
}
116