Passed
Pull Request — master (#20)
by Jason
02:23
created

Foxy::getStoreKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
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->StoreDomain) {
51
            return $config->StoreDomain;
52
        }
53
        return false;
54
    }
55
56
    /**
57
     * @return null|string
58
     * @throws \SilverStripe\ORM\ValidationException
59
     */
60
    public static function store_name_warning()
61
    {
62
        $warning = null;
63
        if (!self::getStoreDomain()) {
64
            $warning = 'Must define FoxyCart Store Name or Store Remote Domain in your site settings in the cms';
65
        }
66
        return $warning;
67
    }
68
69
    /**
70
     * @return string
71
     * @throws \SilverStripe\ORM\ValidationException
72
     */
73
    public static function FormActionURL()
74
    {
75
        $config = Setting::current_foxy_setting();
76
        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...
77
            return sprintf('https://%s/cart', self::getStoreDomain());
0 ignored issues
show
Bug introduced by
It seems like self::getStoreDomain() can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

77
            return sprintf('https://%s/cart', /** @scrutinizer ignore-type */ self::getStoreDomain());
Loading history...
78
        } else {
79
            return sprintf('https://%s.foxycart.com/cart', self::getStoreDomain());
80
        }
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public static function getKeyPrefix()
87
    {
88
        return self::$keyPrefix;
89
    }
90
91
    /**
92
     * @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...
93
     * @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...
94
     * @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...
95
     * @param string $method
96
     * @param bool $output
97
     * @param bool $urlEncode
98
     *
99
     * @return null|string
100
     */
101
    public static function getGeneratedValue(
102
        $productCode = null,
103
        $optionName = null,
104
        $optionValue = null,
105
        $method = 'name',
106
        $output = false,
107
        $urlEncode = false
108
    ) {
109
        $optionName = ($optionName !== null) ? preg_replace('/\s/', '_', $optionName) : $optionName;
0 ignored issues
show
introduced by
The condition $optionName !== null is always false.
Loading history...
110
        $helper = new FoxyValidation();
111
112
        return $helper::fc_hash_value($productCode, $optionName, $optionValue, $method, $output, $urlEncode);
113
    }
114
}
115