Completed
Push — master ( 0b8c6e...20e866 )
by Dmitry
12:39
created

PayButtonComment::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\widgets;
4
5
use yii\base\Event;
6
use yii\base\Widget;
7
8
class PayButtonComment extends Widget
9
{
10
    /**
11
     * @var Event
12
     */
13
    protected $event;
14
15
    /**
16
     * @var array
17
     */
18
    public $commentViews = [];
19
20
    /**
21
     * PayButtonCommentHandler constructor
22
     * @param Event $event The original event, triggered by [[hiqdev\yii2\merchant\widgets\PayButton]]
23
     * @param array $config
24
     */
25
    function __construct(Event $event, $config = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        parent::__construct($config);
28
29
        $this->event = $event;
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function init()
36
    {
37
        parent::init();
38
39
        $this->commentViews = array_merge($this->getDefaultCommentViews(), $this->commentViews);
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function run()
46
    {
47
        return $this->renderComment();
48
    }
49
50
    /**
51
     * Array of default comment views
52
     * @return array
53
     */
54
    protected function getDefaultCommentViews()
55
    {
56
        return ['paypal_usd' => 'paypal'];
57
    }
58
59
    /**
60
     * Returns the view name for the specified $merchant
61
     * @param string $merchant
62
     * @return string|null
63
     * @see commentViews
64
     */
65
    public function getCommentView($merchant)
66
    {
67
        return isset($this->commentViews[$merchant]) ? $this->commentViews[$merchant] : null;
68
    }
69
70
    /**
71
     * Method renders comment from the view, specified in
72
     * @return string
73
     */
74
    protected function renderComment()
75
    {
76
        $merchant = $this->getMerchantName();
77
78
        if (($view = $this->getCommentView($merchant)) === null) {
79
            return '';
80
        }
81
82
        return $this->render($view, [
83
            'merchant' => $merchant,
84
            'widget' => $this,
85
            'event' => $this->event,
86
        ]);
87
    }
88
89
    /**
90
     * Method provides the merchant name
91
     * @return string
92
     */
93
    protected function getMerchantName()
94
    {
95
        return $this->event->sender->getMerchantName();
96
    }
97
98
    /**
99
     * @return string the view path that may be prefixed to a relative view name.
100
     */
101
    public function getViewPath()
102
    {
103
        return parent::getViewPath() . DIRECTORY_SEPARATOR . 'payButtonComments';
104
    }
105
}
106