Completed
Pull Request — master (#361)
by
unknown
03:09
created

SessionStatusWidget::getAvailableItems()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 1
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 Da\User\Model\SessionHistory;
15
use Da\User\Traits\ContainerAwareTrait;
16
use Yii;
17
use yii\base\InvalidConfigException;
18
use yii\base\InvalidParamException;
19
use yii\base\Widget;
20
use yii\helpers\ArrayHelper;
21
22
class SessionStatusWidget extends Widget
23
{
24
    use ContainerAwareTrait;
25
26
    /**
27
     * @var SessionHistory
28
     */
29
    public $model;
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @throws InvalidConfigException
35
     */
36
    public function init()
37
    {
38
        parent::init();
39
        if (!$this->model instanceof SessionHistory) {
40
            throw new InvalidConfigException(
41
                __CLASS__ . '::$userId should be instanceof ' . SessionHistory::class
42
            );
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     *
49
     * @throws InvalidParamException
50
     */
51
    public function run()
52
    {
53
        if ($this->model->getIsActive()) {
54
            if ($this->model->session_id === Yii::$app->session->id) {
55
                $value = Yii::t('usuario', 'Current');
56
            } else {
57
                $value = Yii::t('usuario', 'Active');
58
            }
59
        } else {
60
            $value = Yii::t('usuario', 'Inactive');
61
        }
62
63
        return $value;
64
    }
65
66
    /**
67
     * Returns available auth items to be attached to the user.
68
     *
69
     * @param int|null type of auth items or null to return all
70
     *
71
     * @return array
72
     */
73
    protected function getAvailableItems($type = null)
74
    {
75
        return ArrayHelper::map(
76
            $this->getAuthManager()->getItems($type),
0 ignored issues
show
Documentation Bug introduced by
The method getAuthManager does not exist on object<Da\User\Widget\SessionStatusWidget>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
77
            'name',
78
            function ($item) {
79
                return empty($item->description)
80
                    ? $item->name
81
                    : $item->name . ' (' . $item->description . ')';
82
            }
83
        );
84
    }
85
}
86