Completed
Push — master ( bb4762...ee0943 )
by Roman
15:21
created

GridFieldPaymentStatusIndicator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 9
dl 0
loc 56
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnContent() 0 19 3
A getURLHandlers() 0 6 1
A handleCheckPaymentPending() 0 12 2
1
<?php
2
3
namespace Bummzack\SsOmnipayUI\GridField;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\HTTPRequest;
7
use SilverStripe\Forms\GridField\GridField;
8
use SilverStripe\Forms\GridField\GridField_URLHandler;
9
use SilverStripe\Omnipay\Model\Payment;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\View\ArrayData;
12
use SilverStripe\View\Requirements;
13
use SilverStripe\View\SSViewer;
14
15
class GridFieldPaymentStatusIndicator extends GridFieldPaymentAction implements GridField_URLHandler
16
{
17
    /**
18
     *
19
     * @param GridField $gridField
20
     * @param DataObject $record
21
     * @param string $columnName
22
     * @return string|null - the HTML for the column
23
     */
24
    public function getColumnContent($gridField, $record, $columnName)
25
    {
26
        if (!($record instanceof Payment)) {
27
            return null;
28
        }
29
30
        Requirements::css('bummzack/silverstripe-omnipay-ui: client/dist/css/omnipay-ui-cms.css');
31
        Requirements::javascript('bummzack/silverstripe-omnipay-ui: client/dist/javascript/omnipay-ui-cms.js');
32
33
        if (preg_match('/Pending(Capture|Void|Refund)/', $record->Status)) {
34
            return SSViewer::execute_template('PaymentPendingIndicator', ArrayData::create(array(
35
                'StatusLink' => Controller::join_links($gridField->Link('checkPaymentPending')),
36
                'PaymentID' => $record->ID,
37
                'Timeout' => 2000
38
            )));
39
        }
40
41
        return null;
42
    }
43
44
    public function getURLHandlers($gridField)
45
    {
46
        return array(
47
            'checkPaymentPending' => 'handleCheckPaymentPending'
48
        );
49
    }
50
51
    /**
52
     * Accepts a list of ids in form of comma separated string via GET parameter. If any of these payments is no longer
53
     * pending, this method returns true, false otherwise.
54
     * @param $gridField
55
     * @param HTTPRequest|null $request
56
     * @return bool
57
     */
58
    public function handleCheckPaymentPending($gridField, HTTPRequest $request = null)
0 ignored issues
show
Unused Code introduced by
The parameter $gridField is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
    {
60
        if (!$request) {
61
            return false;
62
        }
63
64
        $ids = preg_split('/[^\d]+/', $request->getVar('ids'));
65
        return Payment::get()
66
            ->filter('ID', $ids)
67
            ->exclude('Status', array('PendingVoid', 'PendingCapture', 'PendingRefund'))
68
            ->count() > 0;
69
    }
70
}
71