Completed
Push — master ( b108ff...181a9d )
by Nate
10:40 queued 07:25
created

UserAssociationQuery::prepare()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 12
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\db;
10
11
use craft\helpers\Db;
12
use flipbox\craft\sortable\associations\db\SortableAssociationQuery;
13
use flipbox\organizations\records\UserAssociation;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 *
19
 * @method UserAssociation one($db = null)
20
 * @method UserAssociation[] all($db = null)
21
 * @method UserAssociation[] getCachedResult($db = null)
22
 */
23
class UserAssociationQuery extends SortableAssociationQuery
24
{
25
    /**
26
     * The sort order attribute
27
     */
28
    const SORT_ORDER_ATTRIBUTE = null;
29
30
    /**
31
     * @var int|int[]|false|null The source Id(s). Prefix Ids with "not " to exclude them.
32
     */
33
    public $userId;
34
35
    /**
36
     * @var int|int[]|false|null The target Id(s). Prefix Ids with "not " to exclude them.
37
     */
38
    public $organizationId;
39
40
    /**
41
     * @inheritdoc
42
     */
43 3
    public function init()
44
    {
45 3
        parent::init();
46
47 3
        if ($this->from === null) {
48 3
            $this->from([
49 3
                UserAssociation::tableName() . ' ' . UserAssociation::tableAlias()
50
            ]);
51
        }
52 3
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57
    protected function fixedOrderColumn(): string
58
    {
59
        return 'id';
60
    }
61
62
    /**
63
     * @inheritdoc
64
     * return static
65
     */
66
    public function organization($value)
67
    {
68
        return $this->organizationId($value);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     * return static
74
     */
75
    public function organizationId($value)
76
    {
77
        $this->organizationId = $value;
78
        return $this;
79
    }
80
81
    /**
82
     * @inheritdoc
83
     * return static
84
     */
85
    public function user($value)
86
    {
87
        return $this->userId($value);
88
    }
89
90
    /**
91
     * @inheritdoc
92
     * return static
93
     */
94
    public function userId($value)
95
    {
96
        $this->userId = $value;
97
        return $this;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function prepare($builder)
104
    {
105
        if ($this->userId !== null) {
106
            $this->andWhere(Db::parseParam('userId', $this->userId));
107
        }
108
109
        if ($this->organizationId !== null) {
110
            $this->andWhere(Db::parseParam('organizationId', $this->organizationId));
111
        }
112
113
        return parent::prepare($builder);
114
    }
115
}
116