ContactDetails::ContactDetailsForm()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.8666
1
<?php
2
3
namespace SilverShop\Checkout\Step;
4
5
use SilverShop\Cart\ShoppingCart;
6
use SilverShop\Checkout\CheckoutComponentConfig;
7
use SilverShop\Checkout\Component\CustomerDetails;
8
use SilverShop\Forms\CheckoutForm;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Security\Security;
13
14
class ContactDetails extends CheckoutStep
15
{
16
    /**
17
     * Whether or not this step should be skipped if user is logged in
18
     *
19
     * @config
20
     * @var    bool
21
     */
22
    private static $skip_if_logged_in = false;
23
24
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
25
        'contactdetails',
26
        'ContactDetailsForm',
27
    ];
28
29
    public function contactdetails()
30
    {
31
        $form = $this->ContactDetailsForm();
32
        if (ShoppingCart::curr()
33
            && self::config()->skip_if_logged_in
34
        ) {
35
            if (Security::getCurrentUser()) {
36
                if ($form->getValidator()->validate()) {
37
                    return Controller::curr()->redirect($this->NextStepLink());
38
                } else {
39
                    $form->clearMessage();
40
                }
41
            }
42
        }
43
44
        return [
45
            'OrderForm' => $form,
46
        ];
47
    }
48
49
    public function ContactDetailsForm()
50
    {
51
        $cart = ShoppingCart::curr();
52
        if (!$cart) {
0 ignored issues
show
introduced by
$cart is of type SilverShop\Model\Order, thus it always evaluated to true.
Loading history...
53
            return false;
54
        }
55
        $config = CheckoutComponentConfig::create(ShoppingCart::curr());
56
        $config->addComponent(CustomerDetails::create());
57
        $form = CheckoutForm::create($this->owner, 'ContactDetailsForm', $config);
58
        $form->setRedirectLink($this->NextStepLink());
59
        $form->setActions(
60
            FieldList::create(
61
                FormAction::create('checkoutSubmit', _t('SilverShop\Checkout\Step\CheckoutStep.Continue', 'Continue'))
62
            )
63
        );
64
        $this->owner->extend('updateContactDetailsForm', $form);
65
66
        return $form;
67
    }
68
}
69