PayButtonComment::getViewPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
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-2019, 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
            'paxum_*' => 'paxum',
69
        ];
70
    }
71
72
    /**
73
     * Returns the view name for the specified $merchant.
74
     * @param string $merchant
75
     * @return string|null
76
     * @see commentViews
77
     */
78
    public function getCommentView($merchant)
79
    {
80
        foreach ($this->commentViews as $pattern => $view) {
81
            if (fnmatch($pattern, $merchant)) {
82
                return $view;
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * Method renders comment from the view, specified in.
91
     * @return string
92
     */
93
    protected function renderComment()
94
    {
95
        $merchant = $this->getMerchantName();
96
97
        if (($view = $this->getCommentView($merchant)) === null) {
98
            return '';
99
        }
100
101
        return $this->render($view, [
102
            'merchant' => $merchant,
103
            'widget' => $this,
104
            'event' => $this->event,
105
        ]);
106
    }
107
108
    /**
109
     * Method provides the merchant name.
110
     * @return string
111
     */
112
    protected function getMerchantName()
113
    {
114
        return $this->event->sender->getMerchantName();
115
    }
116
117
    /**
118
     * @return string the view path that may be prefixed to a relative view name
119
     */
120
    public function getViewPath()
121
    {
122
        return parent::getViewPath() . DIRECTORY_SEPARATOR . 'payButtonComments';
123
    }
124
125
    /**
126
     * @return PayButton
127
     */
128
    public function getPayButton()
129
    {
130
        return $this->event->sender;
131
    }
132
}
133