CartPage   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 74
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 27 2
A requireDefaultRecords() 0 15 3
A find_link() 0 7 2
1
<?php
2
3
namespace SilverShop\Page;
4
5
use Page;
0 ignored issues
show
Bug introduced by
The type Page 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...
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Forms\DropdownField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\TreeDropdownField;
11
use SilverStripe\ORM\DB;
12
13
/**
14
 * View and edit the cart in a full page.
15
 * Visitor can continue shopping, or proceed to checkout.
16
 */
17
class CartPage extends Page
18
{
19
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
20
        'CheckoutPage' => CheckoutPage::class,
21
        'ContinuePage' => SiteTree::class,
22
    ];
23
24
    private static $icon = 'silvershop/core: client/dist/images/icons/cart.gif';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
25
26
    private static $table_name = 'SilverShop_CartPage';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
27
28
    /**
29
     * Returns the link to the checkout page on this site
30
     *
31
     * @param boolean $urlSegment If set to TRUE, only returns the URLSegment field
32
     *
33
     * @return string Link to checkout page
34
     */
35
    public static function find_link($urlSegment = false, $action = false, $id = false)
36
    {
37
        $base = CartPageController::config()->url_segment;
38
        if ($page = self::get()->first()) {
39
            $base = $page->Link();
40
        }
41
        return Controller::join_links($base, $action, $id);
42
    }
43
44
    public function getCMSFields()
45
    {
46
        $this->beforeUpdateCMSFields(
47
            function (FieldList $fields) {
48
                if ($checkouts = CheckoutPage::get()) {
49
                    $fields->addFieldToTab(
50
                        'Root.Links',
51
                        DropdownField::create(
52
                            'CheckoutPageID',
53
                            $this->fieldLabel('CheckoutPage'),
54
                            $checkouts->map("ID", "Title")
55
                        )
56
                    );
57
                }
58
59
                $fields->addFieldToTab(
60
                    'Root.Links',
61
                    TreeDropdownField::create(
62
                        'ContinuePageID',
63
                        $this->fieldLabel('ContinuePage'),
64
                        SiteTree::class
65
                    )
66
                );
67
            }
68
        );
69
70
        return parent::getCMSFields();
71
    }
72
73
    /**
74
     * This module always requires a page model.
75
     */
76
    public function requireDefaultRecords()
77
    {
78
        parent::requireDefaultRecords();
79
        if (!self::get()->exists() && $this->config()->create_default_pages) {
80
            $page = self::create()->update(
81
                [
82
                    'Title' => 'Shopping Cart',
83
                    'URLSegment' => CartPageController::config()->url_segment,
84
                    'ShowInMenus' => 0,
85
                ]
86
            );
87
            $page->write();
88
            $page->publishSingle();
89
            $page->flushCache();
90
            DB::alteration_message('Cart page created', 'created');
91
        }
92
    }
93
}
94