Completed
Pull Request — master (#421)
by Robbie
02:06
created

BlogRecentPostsWidget   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 65
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCMSFields() 0 14 1
A getPosts() 0 12 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace SilverStripe\Blog\Widgets;
4
5
if (!class_exists('\\SilverStripe\\Widgets\\Model\\Widget')) {
6
    return;
7
}
8
9
use SilverStripe\Blog\Model\Blog;
10
use SilverStripe\Forms\DropdownField;
11
use SilverStripe\Forms\NumericField;
12
use SilverStripe\Widgets\Model\Widget;
13
14
/**
15
 * @method Blog Blog()
16
 *
17
 * @property int $NumberOfPosts
18
 */
19
class BlogRecentPostsWidget extends Widget
20
{
21
    /**
22
     * @var string
23
     */
24
    private static $title = 'Recent Posts';
0 ignored issues
show
Unused Code introduced by
The property $title is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    /**
27
     * @var string
28
     */
29
    private static $cmsTitle = 'Recent Posts';
0 ignored issues
show
Unused Code introduced by
The property $cmsTitle is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
    /**
32
     * @var string
33
     */
34
    private static $description = 'Displays a list of recent blog posts.';
0 ignored issues
show
Unused Code introduced by
The property $description is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * @var array
38
     */
39
    private static $db = array(
0 ignored issues
show
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
40
        'NumberOfPosts' => 'Int',
41
    );
42
43
    /**
44
     * @var array
45
     */
46
    private static $has_one = array(
0 ignored issues
show
Unused Code introduced by
The property $has_one is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
47
        'Blog' => Blog::class,
48
    );
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getCMSFields()
54
    {
55
        $this->beforeUpdateCMSFields(function ($fields) {
56
            /**
57
             * @var FieldList $fields
58
             */
59
            $fields->merge(array(
60
                DropdownField::create('BlogID', _t('BlogRecentPostsWidget.Blog', 'Blog'), Blog::get()->map()),
61
                NumericField::create('NumberOfPosts', _t('BlogRecentPostsWidget.NumberOfPosts', 'Number of Posts'))
62
            ));
63
        });
64
65
        return parent::getCMSFields();
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getPosts()
72
    {
73
        $blog = $this->Blog();
74
75
        if ($blog) {
76
            return $blog->getBlogPosts()
77
                ->sort('"PublishDate" DESC')
78
                ->limit($this->NumberOfPosts);
79
        }
80
81
        return array();
82
    }
83
}
84