Completed
Push — master ( 8d56c2...eec4fb )
by Dmitry
17:13
created

PayButtonComment::getPayButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\widgets;
12
13
use hiqdev\yii2\merchant\widgets\PayButton;
14
use yii\base\Event;
15
use yii\base\Widget;
16
17
class PayButtonComment extends Widget
18
{
19
    /**
20
     * @var Event
21
     */
22
    protected $event;
23
24
    /**
25
     * @var array
26
     */
27
    public $commentViews = [];
28
29
    /**
30
     * PayButtonCommentHandler constructor.
31
     * @param Event $event The original event, triggered by [[hiqdev\yii2\merchant\widgets\PayButton]]
32
     * @param array $config
33
     */
34
    public function __construct(Event $event, $config = [])
35
    {
36
        parent::__construct($config);
37
38
        $this->event = $event;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function init()
45
    {
46
        parent::init();
47
48
        $this->commentViews = array_merge($this->getDefaultCommentViews(), $this->commentViews);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function run()
55
    {
56
        return $this->renderComment();
57
    }
58
59
    /**
60
     * Array of default comment views.
61
     * @return array
62
     */
63
    protected function getDefaultCommentViews()
64
    {
65
        return [
66
            'paypal_*' => 'paypal',
67
            'bitpay_*' => 'bitpay',
68
        ];
69
    }
70
71
    /**
72
     * Returns the view name for the specified $merchant.
73
     * @param string $merchant
74
     * @return string|null
75
     * @see commentViews
76
     */
77
    public function getCommentView($merchant)
78
    {
79
        foreach ($this->commentViews as $pattern => $view) {
80
            if (fnmatch($pattern, $merchant)) {
81
                return $view;
82
            }
83
        }
84
85
        return null;
86
    }
87
88
    /**
89
     * Method renders comment from the view, specified in.
90
     * @return string
91
     */
92
    protected function renderComment()
93
    {
94
        $merchant = $this->getMerchantName();
95
96
        if (($view = $this->getCommentView($merchant)) === null) {
97
            return '';
98
        }
99
100
        return $this->render($view, [
101
            'merchant' => $merchant,
102
            'widget' => $this,
103
            'event' => $this->event,
104
        ]);
105
    }
106
107
    /**
108
     * Method provides the merchant name.
109
     * @return string
110
     */
111
    protected function getMerchantName()
112
    {
113
        return $this->event->sender->getMerchantName();
114
    }
115
116
    /**
117
     * @return string the view path that may be prefixed to a relative view name
118
     */
119
    public function getViewPath()
120
    {
121
        return parent::getViewPath() . DIRECTORY_SEPARATOR . 'payButtonComments';
122
    }
123
124
    /**
125
     * @return PayButton
126
     */
127
    public function getPayButton()
128
    {
129
        return $this->event->sender;
130
    }
131
}
132