HomeController::ChartData()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
nc 4
nop 0
1
<?php
2
3
/**
4
 * The home page controller.
5
 */
6
class HomeController extends SiteController
7
{
8
9
    private static $popular_blacklist = array(
10
        'silverstripe/framework',
11
        'silverstripe/cms',
12
        'silverstripe/sqlite3',
13
        'silverstripe/postgresql',
14
        'silverstripe/reports',
15
        'silverstripe/siteconfig',
16
        'silverstripe-themes/simple'
17
    );
18
19
    public static $allowed_actions = array(
20
        'index'
21
    );
22
23
    public function index()
24
    {
25
        return $this->renderWith(array('Home', 'Page'));
26
    }
27
28
    public function Title()
29
    {
30
        return 'Home';
31
    }
32
33
    public function Link()
34
    {
35
        return Director::baseURL();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Director::baseURL(); (array|integer|double|string|boolean) is incompatible with the return type of the parent method Controller::Link of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
36
    }
37
38
    /**
39
     * @param int $limit
40
     * @return DataList|SS_Limitable default to Relative addons
41
     */
42
    public function PopularAddons($limit = 10)
43
    {
44
        return self::RelativePopularAddons($limit);
45
    }
46
47
    /**
48
     * @param int $limit
49
     * @return DataList|SS_Limitable List sorted by absolute downloads
50
     */
51
    public static function AbsolutePopularAddons($limit = 10)
52
    {
53
        return Addon::get()
54
            ->sort('Downloads', 'DESC')
55
            ->exclude('Name', self::config()->popular_blacklist)
56
            ->limit($limit);
57
    }
58
59
    public static function RelativePopularAddons($limit = 10)
60
    {
61
        $addons = Addon::get()
62
            ->exclude(array('Name' => self::config()->popular_blacklist));
63
        $list = ArrayList::create($addons->toArray());
64
        /** @var ArrayList $addons */
65
        $addons = $list->sort('relativePopularity DESC');
66
        $addons = $addons->limit($limit);
67
        foreach ($addons as $addon) {
68
            $addon->Score = $addon->relativePopularityFormatted() . ' per day';
69
        }
70
71
        return $addons;
72
    }
73
74
    public function NewestAddons($limit = 10)
75
    {
76
        return Addon::get()->sort('Released', 'DESC')->limit($limit);
77
    }
78
79
    public function RandomAddons($limit = 10)
80
    {
81
        return Addon::get()->sort(DB::getConn()->random(), 'DESC')->limit($limit);
0 ignored issues
show
Deprecated Code introduced by
The method DB::getConn() has been deprecated with message: since version 4.0 Use DB::get_conn instead

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...
82
    }
83
84
    public function NewestVersions($limit = 10)
85
    {
86
        return AddonVersion::get()
87
            ->filter('Development', false)
88
            ->sort('Released', 'DESC')
89
            ->limit($limit);
90
    }
91
92
    public function ChartData()
93
    {
94
        $chartData = array();
95
        $list = ArrayList::create(array());
96
97
        $sqlQuery = new SQLQuery();
0 ignored issues
show
Deprecated Code introduced by
The class SQLQuery has been deprecated with message: since version 4.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
98
        $sqlQuery->setFrom('Addon');
99
        $sqlQuery->setSelect('DATE(Created) as Created');
100
        $sqlQuery->selectField('COUNT(*)', 'CountInOneDay');
101
        $sqlQuery->addWhere('"Created" >= DATE_SUB(NOW(), INTERVAL 30 DAY)');
102
        $sqlQuery->addGroupBy('DATE(Created)');
103
104
        $result = $sqlQuery->execute();
105
106
        if (count($result)) {
107
            foreach ($result as $row) {
108
                $date = date('j M Y', strtotime($row['Created']));
109
                if (!isset($chartData[$date])) {
110
                    $chartData[$date] = $row['CountInOneDay'];
111
                }
112
            }
113
        }
114
115
        if (count($chartData)) {
116
            foreach ($chartData as $x => $y) {
117
                $list->push(ArrayData::create(array(
118
                    'XValue' => $x,
119
                    'YValue' => $y
120
                )));
121
            }
122
        }
123
124
        return $list;
125
    }
126
}
127