Completed
Push — develop ( 356f72...fcce06 )
by Nate
05:55
created

Users::associations()   A

Complexity

Conditions 5
Paths 7

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 7
nop 3
crap 30
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\services;
10
11
use Craft;
12
use craft\elements\db\UserQuery;
13
use craft\elements\User as UserElement;
14
use craft\helpers\ArrayHelper;
15
use flipbox\ember\services\traits\elements\Accessor;
16
use flipbox\organizations\db\OrganizationQuery;
17
use flipbox\organizations\Organizations as OrganizationPlugin;
18
use yii\base\Component;
19
use yii\base\InvalidConfigException;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 1.0.0
24
 *
25
 * @method UserElement parentFind($identifier)
26
 * @method UserElement create($config = [])
27
 * @method UserElement get($identifier)
28
 * @method UserQuery getQuery($criteria = [])
29
 */
30
class Users extends Component
31
{
32
    use Accessor {
33
        find as parentFind;
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39 33
    public function init()
40
    {
41 33
        $settings = OrganizationPlugin::getInstance()->getSettings();
42 33
        $this->cacheDuration = $settings->usersCacheDuration;
43 33
        $this->cacheDependency = $settings->usersCacheDependency;
44
45 33
        parent::init();
46 33
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 3
    public static function elementClass(): string
52
    {
53 3
        return UserElement::class;
54
    }
55
56
    /**
57
     * @param $identifier
58
     * @return array
59
     */
60 3
    protected function identifierCondition($identifier): array
61
    {
62
        $base = [
63 3
            'status' => null
64
        ];
65
66 3
        if (is_array($identifier)) {
67 3
            return array_merge($base, $identifier);
68
        }
69
70 3
        if (!is_numeric($identifier) && is_string($identifier)) {
71 3
            $base['where'] = [
72 3
                'or',
73 3
                ['username' => $identifier],
74 3
                ['email' => $identifier]
75
            ];
76
        } else {
77 3
            $base['id'] = $identifier;
78
        }
79
80 3
        return $base;
81
    }
82
83
    /**
84
     * @param string $user
85
     * @return UserElement|null
86
     * @throws InvalidConfigException
87
     */
88 9
    public function resolve($user = 'CURRENT_USER')
89
    {
90 9
        if (is_array($user) &&
91 9
            null !== ($id = ArrayHelper::getValue($user, 'id'))
92
        ) {
93 3
            return $this->find($id);
94
        }
95
96 6
        if (null !== ($object = $this->find($user))) {
97 3
            return $object;
98
        }
99
100 3
        return $this->create($user);
101
    }
102
103
    /*******************************************
104
     * FIND
105
     *******************************************/
106
107
    /**
108
     * @param mixed $identifier
109
     * @return UserElement|null
110
     */
111 6
    public function find($identifier = 'CURRENT_USER')
112
    {
113 6
        if ('CURRENT_USER' === $identifier) {
114 3
            return Craft::$app->getUser()->getIdentity();
115
        }
116
117 3
        return $this->parentFind($identifier);
118
    }
119
120
    /**
121
     * @param OrganizationQuery $query
122
     * @param UserElement $user
123
     * @return bool
124
     * @throws \Exception
125
     *
126
     * @deprecated
127
     */
128 3
    public function saveAssociations(
129
        OrganizationQuery $query,
130
        UserElement $user
131
    ): bool {
132 3
        return OrganizationPlugin::getInstance()->getUserOrganizations()->saveAssociations($user, $query);
133
    }
134
135
    /**
136
     * @param OrganizationQuery $query
137
     * @param UserElement $user
138
     * @return bool
139
     * @throws \Exception
140
     *
141
     * @deprecated
142
     */
143 3
    public function dissociate(
144
        OrganizationQuery $query,
145
        UserElement $user
146
    ): bool {
147 3
        return OrganizationPlugin::getInstance()->getUserOrganizations()->dissociate($user, $query);
148
    }
149
150
    /**
151
     * @param OrganizationQuery $query
152
     * @param UserElement $user
153
     * @return bool
154
     * @throws \Exception
155
     *
156
     * @deprecated
157
     */
158 3
    public function associate(
159
        OrganizationQuery $query,
160
        UserElement $user
161
    ): bool {
162 3
        return OrganizationPlugin::getInstance()->getUserOrganizations()->associate($user, $query);
163
    }
164
}
165