CatalogCategoryController::getProduct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
3
namespace Dynamic\ProductCatalog\Page;
4
5
use Dynamic\ProductCatalog\ORM\CatalogProduct;
6
use SilverStripe\Control\HTTPRequest;
7
8
class CatalogCategoryController extends \PageController
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $allowed_actions = array(
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
14
        'view',
15
    );
16
17
    /**
18
     * @var
19
     */
20
    private $product;
21
22
    /**
23
     * @param HTTPRequest|null $request
24
     *
25
     * @return mixed
26
     */
27
    public function getProduct(HTTPRequest $request = null)
28
    {
29
        if (!$this->product) {
30
            $this->setProduct($this->getProductFromRequest($request));
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type SilverStripe\Control\HTTPRequest; however, parameter $request of Dynamic\ProductCatalog\P...getProductFromRequest() does only seem to accept null, 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

30
            $this->setProduct($this->getProductFromRequest(/** @scrutinizer ignore-type */ $request));
Loading history...
31
        }
32
33
        return $this->product;
34
    }
35
36
    /**
37
     * @param null $request
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $request is correct as it would always require null to be passed?
Loading history...
38
     *
39
     * @return bool|\SilverStripe\ORM\DataObject
40
     */
41
    public function getProductFromRequest($request = null)
42
    {
43
        if (!$request) {
0 ignored issues
show
introduced by
$request is of type null, thus it always evaluated to false.
Loading history...
44
            $request = $this->getRequest();
45
        }
46
47
        $productID = $request->param('ID');
48
        if ($productID) {
49
            $product = CatalogProduct::get()->filter('URLSegment', $productID)->first();
50
51
            return $product;
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * @param $product
59
     *
60
     * @return $this
61
     */
62
    public function setProduct($product)
63
    {
64
        $this->product = $product;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param HTTPRequest $request
71
     *
72
     * @return \SilverStripe\ORM\FieldType\DBHTMLText
73
     */
74
    public function view(HTTPRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

74
    public function view(/** @scrutinizer ignore-unused */ HTTPRequest $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
    {
76
        $product = $this->getProduct();
77
        if (!$product) {
78
            return $this->httpError(404, 'The product you\'re looking for isn\'t available.');
79
        }
80
81
        return $this->customise(array(
82
            'Title' => $product->getTitle(),
83
            'Product' => $product,
84
            'MetaTags' => $product->MetaTags(),
85
            'Breadcrumbs' => $product->Breadcrumbs(),
86
        ))->renderWith(array('Dynamic\ProductCatalog\Page\Product', 'Page'));
87
    }
88
}
89