FoxyLinkGeneratorController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 55
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 29 7
A getURLSegment() 0 3 1
1
<?php
2
3
namespace Dynamic\Foxy\Controller;
4
5
use Dynamic\Foxy\Form\AddToCartForm;
6
use Dynamic\Foxy\Model\FoxyHelper;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\ORM\ValidationException;
9
10
/**
11
 * Class FoxyLinkGeneratorController
12
 */
13
class FoxyLinkGeneratorController extends Controller
14
{
15
    /**
16
     *
17
     */
18
    const URLSEGMENT = 'foxylinkgenerator'; // phpcs:ignore PSR12.Properties.ConstantVisibility.NotFound
19
20
    /**
21
     * @var array
22
     */
23
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
24
        'index',
25
    ];
26
27
    /**
28
     * @return string
29
     */
30
    public function getURLSegment()
31
    {
32
        return self::URLSEGMENT;
33
    }
34
35
    /**
36
     * @return string
37
     * @throws ValidationException
38
     */
39
    public function index()
40
    {
41
        $request = $this->getRequest();
42
43
        if (!$request->getVar('code')) {
44
            return 'Code is required';
45
        }
46
47
        if (!$request->getVar('price') && $request->getVar('price') != 0) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $request->getVar('price') of type mixed|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
48
            return 'Price is required';
49
        }
50
51
        if (!$request->getVar('name')) {
52
            return 'Name is required';
53
        }
54
55
        $code = $request->getVar('code');
56
        $props = [];
57
        foreach ($request->getVars() as $var => $val) {
58
            $props[$var] = AddToCartForm::getGeneratedValue($code, $var, $val, 'value');
59
        }
60
61
        $post_url = '';
62
        foreach ($props as $key => $value) {
63
            $post_url .= $key . '=' . $value . '&';
64
        }
65
        $post_url = rtrim($post_url, '&');
66
67
        echo FoxyHelper::StoreURL() . '/cart?' . $post_url;
68
    }
69
}
70