Passed
Push — master ( 9eced7...d82255 )
by Robbie
01:50
created

ChecShortcode::getBuyNowButton()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 2
dl 0
loc 17
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Robbie\SilverstripeChec;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Convert;
7
8
class ChecShortcode
9
{
10
    use Configurable;
11
12
    /**
13
     * Path to Chec embeddable checkout JavaScript file
14
     *
15
     * @config
16
     * @var string
17
     */
18
    private static $javascript_url = 'https://assets.chec-cdn.com/js/embed.js';
0 ignored issues
show
introduced by
The private property $javascript_url is not used, and could be removed.
Loading history...
19
20
    /**
21
     * Domain for hosted Chec checkout
22
     *
23
     * @config
24
     * @var string
25
     */
26
    private static $checkout_domain = 'https://checkout.chec.io';
0 ignored issues
show
introduced by
The private property $checkout_domain is not used, and could be removed.
Loading history...
27
28
    /**
29
     * ShortCode which adds a link to a Chec buy now popup window
30
     *
31
     * @param array $arguments
32
     * @param string $content
33
     * @return string
34
     */
35
    public static function getBuyNowButton($arguments, $content = null): string
36
    {
37
        if (isset($arguments['data-chec-product-id'])) {
38
            $id = Convert::raw2att($arguments['data-chec-product-id']);
39
            unset($arguments['data-chec-product-id']);
40
            $siteURL = self::config()->get('checkout_domain');
41
42
            $link = "<a href=\"$siteURL/{$id}\"";
43
            // Add all arguments as-is as we could potentially be receiving css class styles
44
            foreach ($arguments as $key => $val) {
45
                $val = Convert::raw2att($val);
46
                $link .= " {$key}=\"{$val}\" ";
47
            }
48
            $link .= ">{$content}</a>";
49
            return $link;
50
        }
51
        return '';
52
    }
53
}
54