Completed
Pull Request — master (#304)
by Jason
11:27
created

FoxyStripeSiteConfig::requireDefaultRecords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
ccs 11
cts 13
cp 0.8462
cc 3
eloc 10
nc 3
nop 0
crap 3.0327
1
<?php
2
3
use Dynamic\FoxyStripe\Model\FoxyStripeClient;
4
5
class FoxyStripeSiteConfig extends DataExtension{
6
7
	private static $db = array(
8
	    'StoreTitle' => 'Varchar(255)',
9
		'StoreName' => 'Varchar(255)',
10
        'StoreURL' => 'Varchar(255)',
11
		'StoreKey' => 'Varchar(60)',
12
        'ReceiptURL' => 'Varchar(255)',
13
        'StoreEmail' => 'Varchar(255)',
14
        'FromEmail' => 'Varchar(255)',
15
		'MultiGroup' => 'Boolean',
16
		'ProductLimit' => 'Int',
17
		//'CartValidation' => 'Boolean',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
18
		'MaxQuantity' => 'Int',
19
        'client_id' => 'Varchar(255)',
20
        'client_secret' => 'Varchar(255)',
21
        'access_token' => 'Varchar(255)',
22
        'refresh_token' => 'Varchar(255)',
23
	);
24
25
    // Set Default values
26
    private static $defaults = array(
27
        'ProductLimit' => 10
28
    );
29
30 1
	public function updateCMSFields(FieldList $fields){
31
32
        // set TabSet names to avoid spaces from camel case
33 1
        $fields->addFieldToTab('Root', new TabSet('FoxyStripe', 'FoxyStripe'));
34
35
        // settings tab
36 1
        $fields->addFieldsToTab('Root.FoxyStripe.Settings', array(
37
            // Store Details
38 1
			HeaderField::create('StoreDetails', _t('FoxyStripeSiteConfig.StoreDetails', 'Store Settings'), 3),
39 1
			LiteralField::create('DetailsIntro', _t(
40 1
				'FoxyStripeSiteConfig.DetailsIntro',
41
                '<p>Maps to data in your <a href="https://admin.foxycart.com/admin.php?ThisAction=EditStore" target="_blank">FoxyCart store settings</a>.'
42 1
            )),
43 1
            TextField::create('StoreTitle')
44 1
                ->setTitle(_t('FoxyStripeSiteConfig.StoreTitle', 'Store Name'))
45 1
                ->setDescription(_t('FoxyStripeSiteConfig.StoreTitleDescription', 'The name of your store as you\'d like it displayed to your customers')),
46 1
			TextField::create('StoreName')
47 1
				->setTitle(_t('FoxyStripeSiteConfig.StoreName', 'Store Domain'))
48 1
				->setDescription(_t('FoxyStripeSiteConfig.StoreNameDescription', 'This is a unique FoxyCart subdomain for your cart, checkout, and receipt')),
49 1
            TextField::create('StoreURL')
50 1
                ->setTitle(_t('FoxyStripeSiteConfig.StoreURL', 'Store URL'))
51 1
                ->setDescription(_t('FoxyStripeSiteConfig.StoreURLDescription', 'The URL of your online store')),
52 1
            TextField::create('ReceiptURL')
53 1
                ->setTitle(_t('FoxyStripeSiteConfig.ReceiptURL', 'Receipt URL'))
54 1
                ->setDescription(_t('FoxyStripeSiteConfig.ReceiptURLDescription', 'By default, FoxyCart sends customers back to the page referrer after completing a purchase. Instead, you can set a specific URL here')),
55 1
            TextField::create('StoreEmail')
56 1
                ->setTitle(_t('FoxyStripeSiteConfig.StoreEmail', 'Store Email'))
57 1
                ->setDescription(_t('FoxyStripeSiteConfig.StoreEmailDescription', 'This is the email address of your store. By default, this will be the from address for your store receipts. ')),
58 1
            TextField::create('FromEmail')
59 1
                ->setTitle(_t('FoxyStripeSiteConfig.FromEmail', 'From Email'))
60 1
                ->setDescription(_t('FoxyStripeSiteConfig.FromEmailDescription', 'Used for when you want to specify a different from email than your store\'s email address')),
61
62
			// Advanced Settings
63
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
			HeaderField::create('AdvanceHeader', _t('FoxyStripeSiteConfig.AdvancedHeader', 'Advanced Settings'), 3),
65
			LiteralField::create('AdvancedIntro', _t(
66
                'FoxyStripeSiteConfig.AdvancedIntro',
67
				'<p>Maps to data in your <a href="https://admin.foxycart.com/admin.php?ThisAction=EditAdvancedFeatures" target="_blank">FoxyCart advanced store settings</a>.</p>'
68
			)),
69
			ReadonlyField::create('DataFeedLink', _t('FoxyStripeSiteConfig.DataFeedLink', 'FoxyCart DataFeed URL'), self::getDataFeedLink())
70
				->setDescription(_t('FoxyStripeSiteConfig.DataFeedLinkDescription', 'copy/paste to FoxyCart')),
71
			CheckboxField::create('CartValidation')
72
				->setTitle(_t('FoxyStripeSiteConfig.CartValidation', 'Enable Cart Validation'))
73
				->setDescription(_t(
74
                    'FoxyStripeSiteConfig.CartValidationDescription',
75
                    'You must <a href="https://admin.foxycart.com/admin.php?ThisAction=EditAdvancedFeatures#use_cart_validation" target="_blank">enable cart validation</a> in the FoxyCart admin.'
76
            )),
77
			ReadonlyField::create('StoreKey')
78
				->setTitle(_t('FoxyStripeSiteConfig.StoreKey', 'FoxyCart API Key'))
79
				->setDescription(_t('FoxyStripeSiteConfig.StoreKeyDescription', 'copy/paste to FoxyCart')),
80
			ReadonlyField::create('SSOLink', _t('FoxyStripeSiteConfig.SSOLink', 'Single Sign On URL'), self::getSSOLink())
81
				->setDescription(_t('FoxyStripeSiteConfig.SSOLinkDescription', 'copy/paste to FoxyCart'))
82
            */
83 1
        ));
84
85
        // configuration warning
86 1 View Code Duplication
		if(FoxyCart::store_name_warning()!==null){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

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

Loading history...
87 1
			$fields->insertBefore(LiteralField::create(
88 1
                "StoreSubDomainHeaderWarning",
89 1
                _t(
90 1
                    'FoxyStripeSiteConfig.StoreSubDomainHeadingWarning',
91
                    "<p class=\"message error\">Store sub-domain must be entered in the <a href=\"/admin/settings/\">site settings</a></p>"
92 1
                )
93 1
            ), 'StoreDetails');
0 ignored issues
show
Documentation introduced by
'StoreDetails' is of type string, but the function expects a object<FormField>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
94 1
		}
95
96
        // products tab
97 1
		$fields->addFieldsToTab('Root.FoxyStripe.Products', array(
98 1
			HeaderField::create('ProductHeader', _t('FoxyStripeSiteConfig.ProductHeader', 'Products'), 3),
99 1
			CheckboxField::create('MultiGroup')
100 1
				->setTitle(_t('FoxyStripeSiteConfig.MultiGroup', 'Multiple Groups'))
101 1
				->setDescription(_t(
102 1
                    'FoxyStripeSiteConfig.MultiGroupDescription',
103
                    'Allows products to be shown in multiple Product Groups'
104 1
                )),
105 1
			HeaderField::create('ProductGroupHD', _t('FoxyStripeSiteConfig.ProductGroupHD', 'Product Groups'), 3),
106 1
			NumericField::create('ProductLimit')
107 1
				->setTitle(_t('FoxyStripeSiteConfig.ProductLimit', 'Products per Page'))
108 1
				->setDescription(_t(
109 1
                    'FoxyStripeSiteConfig.ProductLimitDescription',
110
                    'Number of Products to show per page on a Product Group'
111 1
                )),
112 1
			HeaderField::create('ProductQuantityHD', _t('FoxyStripeSiteConfig.ProductQuantityHD', 'Product Form Max Quantity'), 3),
113 1
			NumericField::create('MaxQuantity')
114 1
				->setTitle(_t('FoxyStripeSiteConfig.MaxQuantity', 'Max Quantity'))
115 1
				->setDescription(_t(
116 1
                    'FoxyStripeSiteConfig.MaxQuantityDescription',
117
                    'Sets max quantity for product form dropdown (add to cart form - default 10)'
118 1
                ))
119 1
		));
120
121
        // categories tab
122 1
		$fields->addFieldsToTab('Root.FoxyStripe.Categories', array(
123 1
			HeaderField::create('CategoryHD', _t('FoxyStripeSiteConfig.CategoryHD', 'FoxyStripe Categories'), 3),
124 1
			LiteralField::create('CategoryDescrip', _t(
125 1
                'FoxyStripeSiteConfig.CategoryDescrip',
126
                '<p>FoxyCart Categories offer a way to give products additional behaviors that cannot be accomplished by product options alone, including category specific coupon codes, shipping and handling fees, and email receipts. <a href="https://wiki.foxycart.com/v/2.0/categories" target="_blank">Learn More</a></p><p>Categories you\'ve created in FoxyStripe must also be created in your <a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories" target="_blank">FoxyCart Categories</a> admin panel.</p>'
127 1
            )),
128 1
			GridField::create(
129 1
                'ProductCategory',
130 1
                _t('FoxyStripeSiteConfig.ProductCategory', 'FoxyCart Categories'),
131 1
                ProductCategory::get(),
132 1
                GridFieldConfig_RecordEditor::create()
133 1
            )
134 1
		));
135
136
        // option groups tab
137 1
		$fields->addFieldsToTab('Root.FoxyStripe.Groups', array(
138 1
			HeaderField::create('OptionGroupsHead', _t('FoxyStripeSiteConfig', 'Product Option Groups'), 3),
139 1
			LiteralField::create('OptionGroupsDescrip', _t(
140 1
                'FoxyStripeSiteConfig.OptionGroupsDescrip',
141
                '<p>Product Option Groups allow you to name a set of product options.</p>'
142 1
            )),
143 1
			GridField::create(
144 1
                'OptionGroup',
145 1
                _t('FoxyStripeSiteConfig.OptionGroup', 'Product Option Groups'),
146 1
                OptionGroup::get(),
147 1
                GridFieldConfig_RecordEditor::create()
148 1
            )
149 1
		));
150
151
		// api tab
152 1
        $fields->addFieldsToTab('Root.FoxyStripe.API', [
153 1
            HeaderField::create('APIHD', 'FoxyCart API Settings', 3),
154 1
            TextField::create('client_id', 'FoxyCart Client ID'),
155 1
            TextField::create('client_secret', 'FoxyCart Client Secret'),
156 1
            TextField::create('access_token', 'FoxyCart Access Token'),
157 1
            TextField::create('refresh_token', 'FoxyCart Refresh Token'),
158 1
        ]);
159
160 1
	}
161
162 1
    private static function getSSOLink() {
163 1
        return Director::absoluteBaseURL()."foxystripe/sso/";
164
    }
165
166 1
    private static function getDataFeedLink() {
167 1
        return Director::absoluteBaseURL()."foxystripe/";
168
    }
169
170
    // generate key on install
171 3
    public function requireDefaultRecords() {
172
173 3
        parent::requireDefaultRecords();
174
175 3
        $siteConfig = SiteConfig::current_site_config();
176
177 3
        if(!$siteConfig->StoreKey) {
178 3
            $key = FoxyCart::setStoreKey();
179 3
            while(!ctype_alnum($key)){
180
                $key = FoxyCart::setStoreKey();
181
            }
182 3
            $siteConfig->StoreKey = $key;
183 3
            $siteConfig->write();
184 3
            DB::alteration_message($siteConfig->ClassName.": created FoxyCart Store Key " . $key, 'created');
185 3
        }
186 3
    }
187
188
    /**
189
     * @return array
190
     */
191 1
    public function getDataMap()
192
    {
193
        return [
194 1
            'store_name' => $this->owner->StoreTitle,
195 1
            'store_domain' => $this->owner->StoreName,
196 1
            'store_url' => $this->owner->StoreURL,
197 1
            'receipt_continue_url' => $this->owner->ReceiptURL,
198 1
            'store_email' => $this->owner->StoreEmail,
199 1
            'from_email' => $this->owner->FromEmail,
200
            //'store_postal_code' => $_POST['store_postal_code'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
201
            //'store_country' => $_POST['store_country'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
            //'store_state' => $_POST['store_state'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203 1
            'use_webhook' => 1,
204 1
            'webhook_url' => $this->getDataFeedLink(),
205 1
            'webhook_key' => $this->owner->StoreKey,
206 1
            'use_cart_validation' => 1,
207 1
            'use_single_sign_on' => 1,
208 1
            'single_sign_on_url' => $this->getSSOLink(),
209 1
            'customer_password_hash_type' => 'sha1_salted_suffix',
210 1
            'customer_password_hash_config' => 40,
211 1
        ];
212
    }
213
214
    /**
215
     * if StoreTitle is empty, grab values from FoxyCart
216
     *
217
     * example of 2 way sync for future reference
218
     */
219 41
    public function onBeforeWrite()
220
    {
221 41
        parent::onBeforeWrite();
222
223 41
        if ($this->owner->ID && !$this->owner->StoreTitle && $this->owner->access_token) {
224
            if ($fc = new FoxyStripeClient()) {
225
                $client = $fc->getClient();
226
                $errors = [];
227
228
                $result = $client->get($fc->getCurrentStore());
229
                $this->owner->StoreTitle = $result['store_name'];
230
231
                $errors = array_merge($errors, $client->getErrors($result));
232
                if (count($errors)) {
233
                    \SS_Log::log('FoxyStripeSiteConfig::onBeforeWrite errors - ' . json_encode($errors), \SS_Log::WARN);
234
                }
235
            }
236
        }
237 41
    }
238
239
    /**
240
     * push updated data to FoxyCart
241
     */
242 41
    public function onAfterWrite()
243
    {
244 41
        parent::onAfterWrite();
245
246 41
        if ($this->owner->isChanged() && $this->owner->access_token) {
247
            if ($fc = new FoxyStripeClient()) {
248
                $fc->updateStore($this->getDataMap());
249
            }
250
        }
251 41
    }
252
}
253