DiscountCodeForm::doAddDiscount()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 16
c 3
b 0
f 1
dl 0
loc 27
rs 9.1111
cc 6
nc 4
nop 2
1
<?php
2
3
namespace SilverCommerce\Discounts\Forms;
4
5
use SilverStripe\Forms\Form;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\Forms\RequiredFields;
11
use SilverStripe\Control\RequestHandler;
12
use SilverCommerce\Discounts\DiscountFactory;
13
use SilverCommerce\OrdersAdmin\Model\Estimate;
14
use SilverCommerce\ShoppingCart\ShoppingCartFactory;
0 ignored issues
show
Bug introduced by
The type SilverCommerce\ShoppingCart\ShoppingCartFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * A simple form to add a discount to an estimate via the code.
18
 */
19
class DiscountCodeForm extends Form
20
{
21
    /**
22
     * ID of the estimate the discount will be applied to.
23
     *
24
     * @var Int
25
     */
26
    protected $estimate_id;
27
28
    public function __construct(
29
        RequestHandler $controller = null,
30
        $name = self::DEFAULT_NAME,
31
        Estimate $estimate
32
    ) {
33
        $this->estimate_id = $estimate->ID;
34
35
        $fields = FieldList::create(
36
            TextField::create(
37
                "DiscountCode",
38
                _t("ShoppingCart.DiscountCode", "Discount Code")
39
            )->setAttribute(
40
                "placeholder",
41
                _t("ShoppingCart.EnterDiscountCode", "Enter a discount code")
42
            )
43
        );
44
45
        $actions = FieldList::create(
46
            FormAction::create(
47
                'doAddDiscount',
48
                _t('ShoppingCart.Add', 'Add')
49
            )->addExtraClass('btn btn-info')
50
        );
51
52
        $validator = new RequiredFields(
53
            [
54
                "DiscountCode"
55
            ]
56
        );
57
58
        parent::__construct($controller, $name, $fields, $actions, $validator);
59
60
        $this->setTemplate(
61
            __NAMESPACE__ . 'Includes\\'. __CLASS__
62
        );
63
    }
64
65
    /**
66
     * retrieve the estimate via the id.
67
     *
68
     * @return Estimate
69
     */
70
    public function getEstimate()
71
    {
72
        return Estimate::get()->byID($this->estimate_id);
73
    }
74
75
    /**
76
     * Action that will find a discount based on the code
77
     *
78
     * @param type $data
79
     * @param type $form
0 ignored issues
show
Bug introduced by
The type SilverCommerce\Discounts\Forms\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
     */
81
    public function doAddDiscount($data, $form)
82
    {
83
        $code_to_search = $data['DiscountCode'];
84
        $cart = ShoppingCartFactory::create();
85
        $existing = $cart->getOrder()->Discounts();
86
        $limit = Config::inst()->get(ShoppingCartFactory::class, 'discount_limit');
87
88
        if ($limit && $existing->exists() && $existing->count() >= $limit) {
89
            $form->sessionMessage("Only ".$limit." discount code(s) can be used at a time.", 'bad');
90
            return $this->getRequestHandler()->redirectBack();
91
        }
92
93
        $discount = DiscountFactory::create()->getByIdent($code_to_search);
94
        
95
        if (!$discount) {
96
            $form->sessionMessage("The entered code is invalid.", 'bad');
97
        } else {
98
            $estimate = $this->getEstimate();
99
            // First check if the discount is already added (so we don't
100
            // query the DB if we don't have to).
101
            if (!$estimate->findDiscount($code_to_search)) {
102
                DiscountFactory::create()->generateAppliedDiscount($code_to_search, $estimate);
103
                $cart->save();
104
            }
105
        }
106
        
107
        return $this->getRequestHandler()->redirectBack();
108
    }
109
}
110