ConnectWidget::createClientUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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