EcommerceVoteDecorator::removeallecommercevotes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
3
/**
4
 *@author nicolaas[at]sunnysideup.co.nz
5
 *
6
 *
7
 *
8
 **/
9
10
11
class EcommerceVoteDecorator extends Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
    public static $allowed_actions = array(
14
        "addecommercevote" => true,
15
        "removeallecommercevotes" => true
16
    );
17
18
19
20
    public function addecommercevote()
21
    {
22
        $id = intval(Director::URLParam("ID"));
0 ignored issues
show
Deprecated Code introduced by
The method Director::urlParam() has been deprecated with message: 3.0 Use SS_HTTPRequest->latestParam()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
        if ($id) {
24
            if ($page = DataObject::get_by_id("SiteTree", $id)) {
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
25
                $ecommerceVote = new EcommerceVote();
26
                $ecommerceVote->PageID = $id;
0 ignored issues
show
Documentation introduced by
The property PageID does not exist on object<EcommerceVote>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
27
                $ecommerceVote->write();
28
                if (Director::is_ajax()) {
29
                    return "voted";
30
                } else {
31
                    Director::redirectBack();
0 ignored issues
show
Deprecated Code introduced by
The method Director::redirectBack() has been deprecated with message: 2.5 Use Controller->redirectBack()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
32
                    return;
33
                }
34
            }
35
        }
36
        if (Director::is_ajax()) {
37
            return "vote ERROR";
38
        } else {
39
            Director::redirectBack();
0 ignored issues
show
Deprecated Code introduced by
The method Director::redirectBack() has been deprecated with message: 2.5 Use Controller->redirectBack()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
40
            return;
41
        }
42
    }
43
44
45
    public function TopEcommerceVotes($numberOfEntries = 5)
46
    {
47
        $sqlQuery = new SQLQuery(
48
            $select = "SiteTree.ID MyPageID, COUNT(EcommerceVote.ID) c",
49
            $from = array('SiteTree INNER JOIN EcommerceVote ON SiteTree.ID = EcommerceVote.PageID'),
50
            $where = "",
51
            $orderby = "c DESC",
52
            $groupby = "SiteTree.ID",
53
            $having = "",
54
            $limit = "0, $numberOfEntries"
55
        );
56
        $results = $sqlQuery->execute();
57
        if ($results) {
58
            $stage = Versioned::current_stage();
59
            $baseClass = "SiteTree";
60
            $stageTable = ($stage == 'Stage') ? $baseClass : "{$baseClass}_{$stage}";
0 ignored issues
show
Unused Code introduced by
$stageTable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
            $dataObjectSet = new DataObjectSet();
62
            foreach ($results as $result) {
63
                $page = DataObject::get_by_id("SiteTree", $result["MyPageID"]);
64
                $page->EcommerceVoteCounter =  $result["c"];
65
                $dataObjectSet->push($page);
66
            }
67
            return $dataObjectSet;
68
        }
69
    }
70
71
    public function EcommerceVoteTopFive()
72
    {
73
        return $this->TopEcommerceVotes(5);
74
    }
75
76
    public function removeallecommercevotes()
77
    {
78
        if ($m = Member::currentUser()) {
79
            if ($m->isAdmin()) {
80
                DB::query("DELETE FROM EcommerceVote");
81
                die("all votes have been deleted");
0 ignored issues
show
Coding Style Compatibility introduced by
The method removeallecommercevotes() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
82
            }
83
        }
84
        die("you have to be logged in as an administrator");
0 ignored issues
show
Coding Style Compatibility introduced by
The method removeallecommercevotes() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
85
    }
86
}
87