Completed
Push — master ( 3828c1...52ff87 )
by Dmitry
05:44
created

ClientSellerLink::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\widgets;
13
14
use hipanel\base\Model;
15
use Yii;
16
use yii\base\InvalidConfigException;
17
use yii\helpers\Html;
18
19
class ClientSellerLink extends \yii\base\Widget
20
{
21
    /**
22
     * @var Model
23
     */
24
    public $model;
25
26
    public $clientAttribute   = 'client';
27
    public $clientIdAttribute = 'client_id';
28
    public $sellerAttribute   = 'seller';
29
    public $sellerIdAttribute = 'seller_id';
30
31
    public function init()
32
    {
33
        if ($this->model === null) {
34
            throw new InvalidConfigException('Property "model" must be set');
35
        }
36
    }
37
38
    public function run()
39
    {
40
        if ($this->getClient() === 'anonym') {
41
            $result = Html::tag('b', 'anonym');
42
        } elseif ($this->getClientId() === Yii::$app->user->id || Yii::$app->user->can('support')) {
43
            $result = Html::a($this->getClient(), ['@client/view', 'id' => $this->getClientId()]);
44
        } else {
45
            $result = $this->getClient();
46
        }
47
48
        if (Yii::$app->user->can('support') && $this->getSeller() !== false) {
49
            $result .= ' / ' . Html::a($this->getSeller(), ['@client/view', 'id' => $this->getSellerId()]);
50
        }
51
52
        return $result;
53
    }
54
55
    public function getClient()
56
    {
57
        return $this->getValue($this->clientAttribute);
58
    }
59
60
    public function getClientId()
61
    {
62
        return $this->getValue($this->clientIdAttribute);
63
    }
64
65
    public function getSeller()
66
    {
67
        return $this->getValue($this->sellerAttribute);
68
    }
69
70
    public function getSellerId()
71
    {
72
        return $this->getValue($this->sellerIdAttribute);
73
    }
74
75
    public function getValue($attribute)
76
    {
77
        return $attribute === false ? false : $this->model->{$attribute};
78
    }
79
}
80