Completed
Pull Request — master (#147)
by Simon
02:05
created

HomeController::RelativePopularAddons()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
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-themes/simple'
15
    );
16
17
    public static $allowed_actions = array(
18
        'index'
19
    );
20
21
    public function index()
22
    {
23
        return $this->renderWith(array('Home', 'Page'));
24
    }
25
26
    public function Title()
27
    {
28
        return 'Home';
29
    }
30
31
    public function Link()
32
    {
33
        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...
34
    }
35
36
    /**
37
     * @param int $limit
38
     * @return DataList|SS_Limitable default to Relative addons
39
     */
40
    public function PopularAddons($limit = 10)
41
    {
42
        return self::RelativePopularAddons($limit);
43
    }
44
45
    /**
46
     * @param int $limit
47
     * @return DataList|SS_Limitable List sorted by absolute downloads
48
     */
49
    public static function AbsolutePopularAddons($limit = 10)
50
    {
51
        return Addon::get()
52
            ->sort('Downloads', 'DESC')
53
            ->exclude('Name', self::config()->popular_blacklist)
54
            ->limit($limit);
55
    }
56
57
    public static function RelativePopularAddons($limit = 10)
58
    {
59
        $addons = Addon::get()
60
            ->exclude(array('Name' => self::config()->popular_blacklist));
61
        $list = ArrayList::create($addons->toArray());
62
        /** @var ArrayList $addons */
63
        $addons = $list->sort('relativePopularity DESC');
64
        $addons = $addons->limit($limit);
65
        foreach ($addons as $addon) {
66
            $addon->Score = $addon->relativePopularityFormatted() . ' per day';
67
        }
68
69
        return $addons;
70
    }
71
72
    public function NewestAddons($limit = 10)
73
    {
74
        return Addon::get()->sort('Released', 'DESC')->limit($limit);
75
    }
76
77
    public function RandomAddons($limit = 10)
78
    {
79
        return Addon::get()->sort(DB::getConn()->random(), 'DESC')->limit($limit);
80
    }
81
82
    public function NewestVersions($limit = 10)
83
    {
84
        return AddonVersion::get()
85
            ->filter('Development', false)
86
            ->sort('Released', 'DESC')
87
            ->limit($limit);
88
    }
89
90
    public function ChartData()
91
    {
92
        $chartData = array();
93
        $list = ArrayList::create(array());
94
95
        $sqlQuery = new SQLQuery();
96
        $sqlQuery->setFrom('Addon');
97
        $sqlQuery->setSelect('DATE(Created) as Created');
98
        $sqlQuery->selectField('COUNT(*)', 'CountInOneDay');
99
        $sqlQuery->addWhere('"Created" >= DATE_SUB(NOW(), INTERVAL 30 DAY)');
100
        $sqlQuery->addGroupBy('DATE(Created)');
101
102
        $result = $sqlQuery->execute();
103
104
        if (count($result)) {
105
            foreach ($result as $row) {
106
                $date = date('j M Y', strtotime($row['Created']));
107
                if (!isset($chartData[$date])) {
108
                    $chartData[$date] = $row['CountInOneDay'];
109
                }
110
            }
111
        }
112
113
        if (count($chartData)) {
114
            foreach ($chartData as $x => $y) {
115
                $list->push(ArrayData::create(array(
116
                    'XValue' => $x,
117
                    'YValue' => $y
118
                )));
119
            }
120
        }
121
122
        return $list;
123
    }
124
}
125