ComplexCategory::getCurrentOption()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 18
rs 9.6111
1
<?php
2
3
namespace SilverCommerce\ComplexCategory;
4
5
use Category;
0 ignored issues
show
Bug introduced by
The type Category 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...
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Config\Config;
8
9
class ComplexCategory extends Category
10
{
11
    private static $table_name = 'ComplexCategory';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
12
13
    private static $description = "A category that allows sorting and limiting of products";
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
14
15
    /**
16
     * Get the current search query from the current request
17
     * 
18
     * @return array
19
     */
20
    public function getQuery()
21
    {
22
        $query = Controller::curr()->getRequest()->getVar("s");
23
24
        if (!$query || ($query && !is_array($query))) {
25
            $query = [];
26
        }
27
28
        return $query;
29
    }
30
31
    /**
32
     * Return the current option from the URL
33
     * 
34
     * @param array  $options An array of options to check
35
     * @param string $type    Either "sort" or "limit"
36
     * 
37
     * @return string 
38
     */
39
    public function getCurrentOption($options, $type = "sort")
40
    {
41
        $query = $this->getQuery();
42
        $return = "";
43
44
        if (isset($query[$type])) {
45
            $i = 0;
46
            foreach ($options as $key => $value) {
47
                if ($query[$type] == $i
48
                && $key != ComplexCategoryController::DEFAULT_SORT) {
49
                    $return = $key;
50
                    break;
51
                }
52
                $i++;
53
            }
54
        }
55
56
        return $return;
57
    }
58
59
    /**
60
     * Get a list of all products from this category and it's children
61
     * categories.
62
     *
63
     * @return ArrayList
0 ignored issues
show
Bug introduced by
The type SilverCommerce\ComplexCategory\ArrayList 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...
64
     */
65
    public function AllProducts($sort = [])
66
    {
67
        $products = parent::AllProducts($sort);
68
69
        $sort_options = Config::inst()->get(
70
            ComplexCategoryController::class,
71
            "sort_options"
72
        );
73
74
        $sort = $this->getCurrentOption($sort_options, "sort");
75
76
        if (!empty($sort)) {
77
            $products = $products->sort($sort);
78
        }
79
80
        return $products;
81
    }
82
}