Passed
Push — master ( 5c035c...305609 )
by Nicolaas
03:21
created

CheckoutPageExtensionController::onAfterInit()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 39
c 4
b 0
f 0
dl 0
loc 59
rs 6.9666
cc 12
nc 10
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Sunnysideup\EcommerceGoogleAnalytics;
4
5
use SilverStripe\Control\Director;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Core\Extension;
8
use SilverStripe\View\Requirements;
9
use Sunnysideup\Ecommerce\Config\EcommerceConfig;
10
use Sunnysideup\Ecommerce\Model\Money\EcommerceCurrency;
11
use Sunnysideup\Ecommerce\Pages\Product;
12
13
class CheckoutPageExtensionController extends Extension
14
{
15
    /**
16
     * Standard SS method.
17
     * Runs after the Page::init method is called.
18
     */
19
    public function onAfterInit()
20
    {
21
        $owner = $this->owner;
22
        if ($owner->EnableGoogleAnalytics && $owner->currentOrder && (Director::isLive() || isset($_GET['testanalytics']))) {
23
            $var = EcommerceConfig::get(CheckoutPageDataExtension::class, 'google_analytics_variable');
24
            if ($var) {
25
                $currencyUsedObject = $owner->currentOrder->CurrencyUsed();
26
                if ($currencyUsedObject) {
27
                    $currencyUsedString = $currencyUsedObject->Code;
28
                }
29
                if (empty($currencyUsedString)) {
30
                    $currencyUsedString = EcommerceCurrency::default_currency_code();
31
                }
32
                $orderItems = $owner->currentOrder->OrderItems();
33
                $items = '';
34
                if (Config::inst()->get(CheckoutPageExtensionController::class, 'include_product_items')) {
35
                    foreach ($orderItems as $orderItem) {
36
                        $product = Product::get_by_id($orderItem->BuyableID);
37
                        $sku = $product->InternalItemID ?: $product->ID;
38
                        $topParent = $product->TopParentGroup();
39
                        $catetory = 'Unknown';
0 ignored issues
show
Unused Code introduced by
The assignment to $catetory is dead and can be removed.
Loading history...
40
                        if($topParent) {
41
42
                        }
43
                        $orderItemName = preg_replace("#\r|\n#", '', (string) $orderItem->TableTitle());
44
                        $category = preg_replace("#\r|\n#", '', (string) $category);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $category seems to be defined later in this foreach loop on line 44. Are you sure it is defined here?
Loading history...
45
                        $items .=
46
                            'ga(
47
                                \'ecommerce:addItem\',
48
                                {
49
                                    \'id\': \'' . $owner->currentOrder->ID . '\',
50
                                    \'name\': \'' . $orderItemName . '\',
51
                                    \'sku\': \'' . $sku . '\',
52
                                    \'category\': \'' . $category . '\',
53
                                    \'price\': \'' . $orderItem->CalculatedTotal . '\',
54
                                    \'quantity\': \'' . $orderItem->Quantity . '\',
55
                                }
56
                            );';
57
                    }
58
                }
59
                $js = '
60
                jQuery("#OrderForm_OrderForm").on(
61
                    "submit",
62
                    function(e){
63
                        console.log(e);
64
                        ' . $var . '(\'require\', \'ecommerce\');
65
                        ' . $var . '(
66
                            \'ecommerce:addTransaction\',
67
                            {
68
                                \'id\': \'' . $owner->currentOrder->ID . '\',
69
                                \'revenue\': \'' . $owner->currentOrder->getSubTotal() . '\',
70
                                \'currency\': \'' . $currencyUsedString . '\'
71
                            }
72
                        );
73
                        ' . $items . '
74
                        ' . $var . '(\'ecommerce:send\');
75
                    }
76
                );';
77
                Requirements::customScript($js, 'GoogleAnalyticsEcommerce');
78
            }
79
        }
80
    }
81
}
82