Completed
Pull Request — 2.0 (#506)
by Roman
18:39
created

PopulateShopTask::run()   D

Complexity

Conditions 10
Paths 65

Size

Total Lines 135
Code Lines 92

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 135
ccs 0
cts 99
cp 0
rs 4.8196
cc 10
eloc 92
nc 65
nop 1
crap 110

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
/**
4
 * Populate shop task
5
 *
6
 * @todo       Ideally this task should make use of Spyc, and a single Pages yml file
7
 * instead of the YamlFixture class, which is intended for testing.
8
 *
9
 * @package    shop
10
 * @subpackage tasks
11
 */
12
class PopulateShopTask extends BuildTask
13
{
14
    protected $title       = "Populate Shop";
15
16
    protected $description = 'Creates dummy account page, products, checkout page, terms page.';
17
18
    public function run($request)
0 ignored issues
show
Complexity introduced by
This operation has 320 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
19
    {
20
21
        if ($request->getVar('createintzone')) {
22
            $this->populateInternationalZone();
23
            DB::alteration_message('Created an international zone', 'created');
24
            return;
25
        }
26
        $this->extend("beforePopulate");
27
28
        $factory = Injector::inst()->create('FixtureFactory');
29
30
        $parentid = 0;
31
32
        //create products
33
        if (!DataObject::get_one('Product')) {
34
            $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/dummyproducts.yml");
35
            $fixture->writeInto($factory);//needs to be a data model
36
37
            $shoppage = ProductCategory::get()->filter('URLSegment', 'shop')->first();
38
            $parentid = $shoppage->ID;
39
40
            $categoriestopublish = array(
41
                'products',
42
                'electronics',
43
                'apparel',
44
                'entertainment',
45
                'music',
46
                'movies',
47
                'drama',
48
                'toys',
49
                'food',
50
                'books',
51
                'jewellery',
52
                'furniture',
53
                'kitchen',
54
                'bedroom',
55
                'stationery',
56
            );
57
            foreach ($categoriestopublish as $categoryname) {
58
                $factory->get("ProductCategory", $categoryname)->publish('Stage', 'Live');
59
            }
60
            $productstopublish = array(
61
                'mp3player',
62
                'hdtv',
63
                'socks',
64
                'tshirt',
65
                'beachball',
66
                'hoop',
67
                'kite',
68
                'genericmovie',
69
                'lemonchicken',
70
                'ring',
71
                'book',
72
                'lamp',
73
                'paper',
74
                'pens',
75
            );
76
            foreach ($productstopublish as $productname) {
77
                $factory->get("Product", $productname)->publish('Stage', 'Live');
78
            }
79
            DB::alteration_message('Created dummy products and categories', 'created');
80
        } else {
81
            echo "<p style=\"color:orange;\">Products and categories were not created because some already exist.</p>";
82
        }
83
84
        //cart page
85
        if (!$page = DataObject::get_one('CartPage')) {
86
            $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/Cart.yml");
87
            $fixture->writeInto($factory);
88
            $page = $factory->get("CartPage", "cart");
89
            $page->ParentID = $parentid;
90
            $page->writeToStage('Stage');
91
            $page->publish('Stage', 'Live');
92
            DB::alteration_message('Cart page created', 'created');
93
        }
94
95
        //checkout page
96
        if (!$page = DataObject::get_one('CheckoutPage')) {
97
            $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/Checkout.yml");
98
            $fixture->writeInto($factory);
99
            $page = $factory->get("CheckoutPage", "checkout");
100
            $page->ParentID = $parentid;
101
            $page->writeToStage('Stage');
102
            $page->publish('Stage', 'Live');
103
            DB::alteration_message('Checkout page created', 'created');
104
        }
105
106
        //account page
107
        if (!DataObject::get_one('AccountPage')) {
108
            $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/Account.yml");
109
            $fixture->writeInto($factory);
110
            $page = $factory->get("AccountPage", "account");
111
            $page->ParentID = $parentid;
112
            $page->writeToStage('Stage');
113
            $page->publish('Stage', 'Live');
114
            DB::alteration_message('Account page \'Account\' created', 'created');
115
        }
116
117
        //terms page
118
        if (!$termsPage = DataObject::get_one('Page', "\"URLSegment\" = 'terms-and-conditions'")) {
119
            $fixture = new YamlFixture(SHOP_DIR . "/tests/fixtures/pages/TermsConditions.yml");
120
            $fixture->writeInto($factory);
121
            $page = $factory->get("Page", "termsconditions");
122
            $page->ParentID = $parentid;
123
            $page->writeToStage('Stage');
124
            $page->publish('Stage', 'Live');
125
            //set terms page id in config
126
            $config = SiteConfig::current_site_config();
127
            $config->TermsPageID = $page->ID;
128
            $config->write();
129
            DB::alteration_message("Terms and conditions page created", 'created');
130
        }
131
132
        //countries config - removes some countries
133
        $siteconfig = SiteConfig::current_site_config();
134
        if (empty($siteconfig->AllowedCountries)) {
135
            $siteconfig->AllowedCountries =
136
                "AF,AL,DZ,AS,AD,AO,AG,AR,AM,AU,AT,AZ,BS,BH,
137
			BD,BB,BY,BE,BZ,BJ,BT,BO,BA,BW,BR,BN,BG,BF,BI,
138
			KH,CM,CA,CV,CF,TD,CL,CN,CO,KM,CG,CR,CI,HR,CU,
139
			CY,CZ,DK,DJ,DM,DO,EC,EG,SV,GQ,ER,EE,ET,FJ,FI,
140
			FR,GA,GM,GE,DE,GH,GR,GD,GT,GN,GW,GY,HT,HN,HK,
141
			HU,IS,IN,ID,IR,IQ,IE,IL,IT,JM,JP,JO,KZ,KE,KI,
142
			KP,KR,KW,KG,LA,LV,LB,LS,LR,LY,LI,LT,LU,MG,MW,
143
			MY,MV,ML,MT,MH,MR,MU,MX,FM,MD,MC,MN,MS,MA,MZ,
144
			MM,NA,NR,NP,NL,NZ,NI,NE,NG,NO,OM,PK,PW,PA,PG,
145
			PY,PE,PH,PL,PT,QA,RO,RU,RW,KN,LC,VC,WS,SM,ST,
146
			SA,SN,SC,SL,SG,SK,SI,SB,SO,ZA,ES,LK,SD,SR,SZ,
147
			SE,CH,SY,TJ,TZ,TH,TG,TO,TT,TN,TR,TM,TV,UG,UA,
148
			AE,GB,US,UY,UZ,VU,VE,VN,YE,YU,ZM,ZW";
149
            $siteconfig->write();
150
        }
151
        $this->extend("afterPopulate");
152
    }
153
154
    public function populateInternationalZone()
155
    {
156
        $zone = Zone::create(
157
            array(
158
                'Name' => 'International',
159
            )
160
        );
161
        $zone->write();
162
163
        if ($countries = SiteConfig::current_site_config()->getCountriesList()) {
164
            foreach ($countries as $iso => $country) {
165
                $region = ZoneRegion::create(
166
                    array(
167
                        'Country' => $iso,
168
                        'ZoneID'  => $zone->ID,
169
                    )
170
                );
171
                $region->write();
172
            }
173
        }
174
    }
175
}
176