Passed
Pull Request — master (#63)
by Jason
02:30
created

FoxyHelper   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 65
c 2
b 0
f 0
dl 0
loc 234
rs 10
wmc 25

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setCustomSSL() 0 5 1
A __construct() 0 4 1
A setProductClasses() 0 23 5
A setStoreCartURL() 0 5 1
A getCustomSSL() 0 7 2
A StoreURL() 0 7 2
A getStoreCartURL() 0 7 2
A getProuductClasses() 0 7 2
A setStoreSecret() 0 5 1
A getMaxQuantity() 0 7 2
A getStoreSecret() 0 7 2
A FormActionURL() 0 3 1
A setMaxQuantity() 0 5 1
A getProducts() 0 12 2
1
<?php
2
3
namespace Dynamic\Foxy\Model;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Extensible;
7
use SilverStripe\Core\Injector\Injectable;
8
9
class FoxyHelper extends \FoxyCart_Helper
10
{
11
    use Configurable;
12
    use Injectable;
13
    use Extensible;
14
15
    /**
16
     * @var
17
     */
18
    private static $secret;
0 ignored issues
show
introduced by
The private property $secret is not used, and could be removed.
Loading history...
19
20
    /**
21
     * @var
22
     */
23
    protected static $cart_url;
24
25
    /**
26
     * @var
27
     */
28
    private static $custom_ssl;
0 ignored issues
show
introduced by
The private property $custom_ssl is not used, and could be removed.
Loading history...
29
30
    /**
31
     * @var
32
     */
33
    private static $max_quantity = 10;
0 ignored issues
show
introduced by
The private property $max_quantity is not used, and could be removed.
Loading history...
34
35
    /**
36
     * @var array
37
     */
38
    private static $product_classes = [];
0 ignored issues
show
introduced by
The private property $product_classes is not used, and could be removed.
Loading history...
39
40
    /**
41
     * @var
42
     */
43
    private $foxy_secret;
44
45
    /**
46
     * @var
47
     */
48
    private $foxy_cart_url;
49
50
    /**
51
     * @var
52
     */
53
    private $foxy_custom_ssl;
54
55
    /**
56
     * @var
57
     */
58
    private $foxy_max_quantity;
59
60
    /**
61
     * @var
62
     */
63
    private $foxy_product_classes;
64
65
    /**
66
     * @return mixed
67
     */
68
    public function getStoreSecret()
69
    {
70
        if (!$this->foxy_secret) {
71
            $this->setStoreSecret();
72
        }
73
74
        return $this->foxy_secret;
75
    }
76
77
    /**
78
     * @return $this
79
     */
80
    public function setStoreSecret()
81
    {
82
        $this->foxy_secret = $this->config()->get('secret');
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return mixed
89
     */
90
    public function getStoreCartURL()
91
    {
92
        if (!$this->foxy_cart_url) {
93
            $this->setStoreCartURL();
94
        }
95
96
        return $this->foxy_cart_url;
97
    }
98
99
    /**
100
     * @return $this
101
     */
102
    public function setStoreCartURL()
103
    {
104
        $this->foxy_cart_url = $this->config()->get('cart_url');
105
106
        return $this;
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function getCustomSSL()
113
    {
114
        if (!$this->foxy_custom_ssl) {
115
            $this->setCustomSSL();
116
        }
117
118
        return $this->foxy_custom_ssl;
119
    }
120
121
    /**
122
     * @return $this
123
     */
124
    public function setCustomSSL()
125
    {
126
        $this->foxy_custom_ssl = $this->config()->get('custom_ssl');
127
128
        return $this;
129
    }
130
131
    /**
132
     * @return mixed
133
     */
134
    public function getMaxQuantity()
135
    {
136
        if (!$this->foxy_max_quantity) {
137
            $this->setMaxQuantity();
138
        }
139
140
        return $this->foxy_max_quantity;
141
    }
142
143
    /**
144
     * @return $this
145
     */
146
    public function setMaxQuantity()
147
    {
148
        $this->foxy_max_quantity = $this->config()->get('max_quantity');
149
150
        return $this;
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function getProuductClasses()
157
    {
158
        if (!$this->foxy_product_classes) {
159
            $this->setProductClasses();
160
        }
161
162
        return $this->foxy_product_classes;
163
    }
164
165
    /**
166
     * @return $this
167
     */
168
    public function setProductClasses()
169
    {
170
        $productClasses = $this->config()->get('product_classes');
171
172
        if (empty($productClasses)) {
173
            $this->foxy_product_classes = [];
174
            return $this;
175
        } elseif (!is_array($productClasses)) {
176
            $productClasses = [$productClasses];
177
        }
178
179
        if ($this->config()->get('include_product_subclasses')) {
180
            $productClasses = array_reduce($productClasses, function (array $list, $productClass) {
181
                foreach (ClassInfo::subclassesFor($productClass) as $key => $class) {
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\ClassInfo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
182
                    $list[$key] = $class;
183
                }
184
                $this->foxy_product_classes =  $list;
185
                return $this;
186
            }, []);
187
        }
188
189
        $this->foxy_product_classes = $productClasses;
190
        return $this;
191
    }
192
193
    /**
194
     * @return |null
0 ignored issues
show
Documentation Bug introduced by
The doc comment |null at position 0 could not be parsed: Unknown type name '|' at position 0 in |null.
Loading history...
195
     */
196
    public function getProducts()
197
    {
198
        $productClasses = $this->getProuductClasses();
199
        $products = null;
200
201
        if (!empty($productClasses)) {
202
            $products = SiteTree::get()->filter('ClassName', $productClasses);
0 ignored issues
show
Bug introduced by
The type Dynamic\Foxy\Model\SiteTree was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
203
        }
204
205
        $this->owner->extend('updateProducts', $products);
0 ignored issues
show
Bug Best Practice introduced by
The property owner does not exist on Dynamic\Foxy\Model\FoxyHelper. Did you maybe forget to declare it?
Loading history...
206
207
        return $products;
208
    }
209
210
    /**
211
     * FoxyHelper constructor.
212
     *
213
     * Set the private statics $secret and $cart_url in FoxyCart_Helper
214
     *
215
     * @throws \SilverStripe\ORM\ValidationException
216
     */
217
    public function __construct()
218
    {
219
        self::setCartURL($this->getStoreCartURL());
220
        self::setSecret($this->getStoreSecret());
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public static function StoreURL()
227
    {
228
        $helper = FoxyHelper::create();
229
        if ($helper->getCustomSSL()) {
230
            return sprintf('https://%s/', $helper->getStoreCartURL());
231
        } else {
232
            return sprintf('https://%s.foxycart.com/', $helper->getStoreCartURL());
233
        }
234
    }
235
236
    /**
237
     * @return string
238
     * @throws \SilverStripe\ORM\ValidationException
239
     */
240
    public static function FormActionURL()
241
    {
242
        return self::StoreURL() . 'cart';
243
    }
244
}
245