Completed
Push — master ( 289dbc...0efbaa )
by Scott
02:23
created

ProductsQuery::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
1
<?php namespace Bedard\Shop\Classes;
2
3
use Bedard\Shop\Models\Category;
4
use Bedard\Shop\Models\Product;
5
use October\Rain\Database\Builder;
6
7
class ProductsQuery
8
{
9
    /**
10
     * @var \Bedard\Shop\Models\Category    The category to query products for.
11
     */
12
    protected $category;
13
14
    /**
15
     * @var array   Query parameters.
16
     */
17
    protected $params;
18
19
    /**
20
     * Construct
21
     *
22
     * @param \Bedard\Shop\Models\Category  $category
23
     * @param array                         $params
24
     */
25
    public function __construct(Category $category, array $params = [])
26
    {
27
        $this->category = $category;
28
        $this->params = $params;
29
    }
30
31
    /**
32
     * Execute a products query.
33
     *
34
     * @return \October\Rain\Database\Collection
35
     */
36
    public function get()
37
    {
38
        $query = Product::isEnabled();
39
        $this->applySelectStatements($query);
40
        $this->applyWhereStatements($query);
41
        $this->applyOrderByStatements($query);
42
43
        return $query->get();
44
    }
45
46
    /**
47
     * Apply a custom sort order to a products query.
48
     *
49
     * @param  \October\Rain\Database\Builder   $products
0 ignored issues
show
Bug introduced by
There is no parameter named $products. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
50
     * @return void
51
     */
52
    protected function applyCustomOrder(Builder &$query)
53
    {
54
        $order = '';
55
        foreach ($this->category->product_order as $index => $id) {
56
            $order .= "when {$id} then {$index} ";
57
        }
58
59
        $query->orderByRaw("case id {$order} else 'last' end");
60
    }
61
62
    /**
63
     * Apply an order by statement to the products query.
64
     *
65
     * @param  \October\Rain\Database\Builder   $products
0 ignored issues
show
Bug introduced by
There is no parameter named $products. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
66
     * @return void
67
     */
68
    protected function applyOrderByStatements(Builder &$query)
69
    {
70
        if (array_key_exists('products_sort_column', $this->params) &&
71
            array_key_exists('products_sort_direction', $this->params)) {
72
            $query->orderBy($this->params['products_sort_column'], $this->params['products_sort_direction']);
73
        } elseif ($this->category->isCustomSorted()) {
74
            $this->applyCustomOrder($query);
75
        } else {
76
            $query->orderBy($this->category->product_sort_column, $this->category->product_sort_direction);
77
        }
78
    }
79
80
    /**
81
     * Apply filters to a products query.
82
     *
83
     * @param  \October\Rain\Database\Builder   $query
84
     * @return void
85
     */
86
    protected function applyProductFilters(Builder &$query)
87
    {
88
        $query->where(function ($q) {
89
            foreach ($this->category->filters as $filter) {
90
                $right = $filter->getRightClause();
91
                $q->where($filter->left, $filter->comparator, $right);
92
            }
93
        });
94
    }
95
96
    /**
97
     * Apply select statements and join prices.
98
     *
99
     * @param  \October\Rain\Database\Builder   $query
100
     * @return void
101
     */
102
    protected function applySelectStatements(Builder &$query)
103
    {
104
        // apply select statements if neccessary
105
        if (array_key_exists('products_select', $this->params) && $this->params['products_select']) {
106
            $query->select($this->params['products_select']);
107
108
            // join the prices if neccessary
109
            if (in_array('price', $this->params['products_select'])) {
110
                $query->joinPrice();
111
            }
112
        }
113
    }
114
115
    /**
116
     * Restrict our query to products that should be visible in our category.
117
     *
118
     * @param  \October\Rain\Database\Builder   $query
119
     * @return void
120
     */
121
    protected function applyWhereStatements(Builder &$query)
122
    {
123
        // if the category is filtered, grab our products from those
124
        if ($this->category->isFiltered()) {
125
            $this->applyProductFilters($query);
126
        }
127
128
        // otherwise grab all products appearing in our category
129
        else {
130
            $query->appearingInCategory($this->category->id);
131
        }
132
    }
133
}
134