Passed
Pull Request — master (#20)
by Jason
01:32
created

Foxy::FormActionURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
class Foxy
6
{
7
    /**
8
     * @var string
9
     */
10
    private static $keyPrefix = 'dYnm1c';
11
12
    /**
13
     * @param int $length
14
     * @param int $count
15
     *
16
     * @return string
17
     */
18
    public static function setStoreKey($length = 54, $count = 0)
19
    {
20
        $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' . strtotime('now');
21
        $strLength = strlen($charset);
22
        $str = '';
23
        while ($count < $length) {
24
            $str .= $charset[mt_rand(0, $strLength - 1)];
25
            ++$count;
26
        }
27
        return self::getKeyPrefix() . substr(base64_encode($str), 0, $length);
28
    }
29
30
    /**
31
     * @return mixed|null
32
     * @throws \SilverStripe\ORM\ValidationException
33
     */
34
    public static function getStoreKey()
35
    {
36
        $config = Setting::current_foxy_setting();
37
        if ($config->StoreKey) {
38
            return $config->StoreKey;
39
        }
40
        return false;
41
    }
42
43
    /**
44
     * @return mixed|null
45
     * @throws \SilverStripe\ORM\ValidationException
46
     */
47
    public static function getStoreDomain()
48
    {
49
        $config = Setting::current_foxy_setting();
50
        if ($config->CustomSSL) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomSSL does not exist on Dynamic\Foxy\Model\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
51
            if ($config->RemoteDomain) {
0 ignored issues
show
Bug Best Practice introduced by
The property RemoteDomain does not exist on Dynamic\Foxy\Model\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
52
                return $config->RemoteDomain;
53
            }
54
        } else {
55
            if ($config->StoreDomain) {
56
                return $config->StoreDomain;
57
            }
58
        }
59
        return false;
60
    }
61
62
    /**
63
     * @return null|string
64
     * @throws \SilverStripe\ORM\ValidationException
65
     */
66
    public static function store_name_warning()
67
    {
68
        $warning = null;
69
        if (!self::getStoreDomain()) {
70
            $warning = 'Must define FoxyCart Store Name or Store Remote Domain in your site settings in the cms';
71
        }
72
        return $warning;
73
    }
74
75
    /**
76
     * @return string
77
     * @throws \SilverStripe\ORM\ValidationException
78
     */
79
    public static function FormActionURL()
80
    {
81
        $config = Setting::current_foxy_setting();
82
        if ($config->CustomSSL) {
0 ignored issues
show
Bug Best Practice introduced by
The property CustomSSL does not exist on Dynamic\Foxy\Model\Setting. Since you implemented __get, consider adding a @property annotation.
Loading history...
83
            return sprintf('https://%s/cart', self::getStoreDomain());
84
        } else {
85
            return sprintf('https://%s.foxycart.com/cart', self::getStoreDomain());
86
        }
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public static function getKeyPrefix()
93
    {
94
        return self::$keyPrefix;
95
    }
96
97
    /**
98
     * @param null $productCode
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $productCode is correct as it would always require null to be passed?
Loading history...
99
     * @param null $optionName
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $optionName is correct as it would always require null to be passed?
Loading history...
100
     * @param null $optionValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $optionValue is correct as it would always require null to be passed?
Loading history...
101
     * @param string $method
102
     * @param bool $output
103
     * @param bool $urlEncode
104
     *
105
     * @return null|string
106
     */
107
    public static function getGeneratedValue(
108
        $productCode = null,
109
        $optionName = null,
110
        $optionValue = null,
111
        $method = 'name',
112
        $output = false,
113
        $urlEncode = false
114
    ) {
115
        $optionName = ($optionName !== null) ? preg_replace('/\s/', '_', $optionName) : $optionName;
0 ignored issues
show
introduced by
The condition $optionName !== null is always false.
Loading history...
116
        $helper = new FoxyValidation();
117
118
        return $helper::fc_hash_value($productCode, $optionName, $optionValue, $method, $output, $urlEncode);
119
    }
120
}
121