Completed
Push — master ( 38ef72...9181f6 )
by Nic
09:57
created

RecentVideosBlock::canCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * Class RecentVideosBlock
5
 *
6
 * @property int $Limit
7
 * @property int $VideoPageID
8
 * @method YouTubeIntegrationVideosPage $VideoPage
9
 */
10
class RecentVideosBlock extends Block
11
{
12
    /**
13
     * @var string
14
     */
15
    private static $singular_name = 'Recent Videos Block';
16
17
    /**
18
     * @var string
19
     */
20
    private static $plural_name = 'Recent Videos Blocks';
21
22
    /**
23
     * @var array
24
     */
25
    private static $db = array(
26
        'Limit' => 'Int',
27
    );
28
29
    /**
30
     * @var array
31
     */
32
    private static $has_one = array(
33
        'VideoPage' => 'YouTubeIntegrationVideosPage',
34
    );
35
36
    /**
37
     * @var array
38
     */
39
    private static $defaults = array(
40
        'Limit' => 3,
41
    );
42
43
    /**
44
     * @var
45
     */
46
    private $recent_videos;
47
48
    /**
49
     * @return FieldList
50
     */
51 View Code Duplication
    public function getCMSFields()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $fields = singleton('Block')->getCMSFields();
54
55
        $fields->addFieldsToTab('Root.Main', array(
56
            NumericField::create('Limit'),
57
        ));
58
59
        $fields->addFieldToTab(
60
            'Root.Main',
61
            DropdownField::create('VideoPageID', 'Featured Video Page', YouTubeIntegrationVideosPage::get()->map())
62
                ->setEmptyString('')
63
        );
64
65
        return $fields;
66
    }
67
68
    /**
69
     * @param null $member
70
     * @return bool
71
     */
72
    public function canCreate($member = null)
73
    {
74
        return parent::canCreate($member);
75
    }
76
77
    /**
78
     * @param null $member
79
     * @return bool
80
     */
81
    public function canView($member = null)
82
    {
83
        return parent::canView($member);
84
    }
85
86
    /**
87
     * @return mixed
88
     */
89
    public function getRecentVideos()
90
    {
91
        if (!$this->recent_videos) {
92
            $this->setRecentVideos();
93
        }
94
        return $this->recent_videos;
95
    }
96
97
    /**
98
     * @return $this
99
     */
100
    public function setRecentVideos()
101
    {
102
        $this->recent_videos = ($this->VideoPageID != 0)
103
            ? $this->VideoPage()->getVideosList()->limit($this->Limit)
0 ignored issues
show
Documentation Bug introduced by
The method VideoPage does not exist on object<RecentVideosBlock>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
104
            : SilverStripeYouTubeVideo::get()->limit($this->Limit);
105
        return $this;
106
    }
107
108
}