ConnectWidget   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 53.85%

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 6
dl 0
loc 46
ccs 7
cts 13
cp 0.5385
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createClientUrl() 0 8 2
A init() 0 9 2
A isConnected() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Widget;
13
14
use Yii;
15
use yii\authclient\ClientInterface;
16
use yii\authclient\widgets\AuthChoice;
17
use yii\authclient\widgets\AuthChoiceAsset;
18
use yii\base\InvalidParamException;
19
use yii\helpers\Html;
20
use yii\helpers\Url;
21
22
class ConnectWidget extends AuthChoice
23
{
24
    /**
25
     * @var array|null An array of user's accounts
26
     */
27
    public $accounts;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 4
    public function init()
33
    {
34 4
        AuthChoiceAsset::register(Yii::$app->view);
35 4
        if ($this->popupMode) {
36 4
            Yii::$app->view->registerJs("\$('#" . $this->getId() . "').authchoice();");
37
        }
38 4
        $this->options['id'] = $this->getId();
39 4
        echo Html::beginTag('div', $this->options);
40 4
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @throws InvalidParamException
46
     */
47
    public function createClientUrl($provider)
48
    {
49
        if ($this->isConnected($provider)) {
50
            return Url::to(['/user/settings/disconnect', 'id' => $this->accounts[$provider->getId()]->id]);
51
        }
52
53
        return parent::createClientUrl($provider);
54
    }
55
56
    /**
57
     * Checks if provider already connected to user.
58
     *
59
     * @param ClientInterface $provider
60
     *
61
     * @return bool
62
     */
63
    public function isConnected(ClientInterface $provider)
64
    {
65
        return null !== $this->accounts && isset($this->accounts[$provider->getId()]);
66
    }
67
}
68