Passed
Push — master ( 7ee2c4...735a45 )
by Roman
04:03
created

PopulateShopTask::populateInternationalZone()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverShop\Tasks;
4
5
use Page;
6
use SilverShop\Page\AccountPage;
7
use SilverShop\Page\CartPage;
8
use SilverShop\Page\CheckoutPage;
9
use SilverShop\Page\Product;
10
use SilverShop\Page\ProductCategory;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\BuildTask;
13
use SilverStripe\Dev\FixtureFactory;
14
use SilverStripe\Dev\YamlFixture;
15
use SilverStripe\ORM\DB;
16
use SilverStripe\SiteConfig\SiteConfig;
17
18
/**
19
 * Populate shop task
20
 *
21
 * @todo Ideally this task should make use of Spyc, and a single Pages yml file
22
 * instead of the YamlFixture class, which is intended for testing.
23
 *
24
 * @package    shop
25
 * @subpackage tasks
26
 */
27
class PopulateShopTask extends BuildTask
28
{
29
    protected $title = 'Populate Shop';
30
31
    protected $description = 'Creates dummy account page, products, checkout page, terms page.';
32
33
    public function run($request)
34
    {
35
        $this->extend('beforePopulate');
36
37
        $factory = Injector::inst()->create(FixtureFactory::class);
38
39
        $parentid = 0;
40
41
        $fixtureDir = realpath(__DIR__ . '/../../tests/php/Fixtures');
42
43
        //create products
44
        if (!Product::get()->count()) {
45
            $fixture = YamlFixture::create($fixtureDir . '/dummyproducts.yml');
0 ignored issues
show
Bug introduced by
$fixtureDir . '/dummyproducts.yml' of type string is incompatible with the type array expected by parameter $args of SilverStripe\Dev\YamlFixture::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

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