Passed
Pull Request — master (#18)
by Robbie
03:29
created

PageControlledPolicy   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A updateCMSFields() 0 14 3
A getCacheAge() 0 4 2
1
<?php
2
3
namespace SilverStripe\ControllerPolicy;
4
5
use SilverStripe\Forms\FieldList;
6
use SilverStripe\Forms\LiteralField;
7
use SilverStripe\Forms\TextField;
8
use SilverStripe\ORM\DataExtension;
9
use SilverStripe\Security\Permission;
10
use SilverStripe\Security\Security;
11
12
/**
13
 * This extension leverages the CachingPolicy's ability to customise the max-age per originator.
14
 * The configuration option is surfaced to the CMS UI. The extension needs to be added
15
 * to the object related to the policed controller.
16
 */
17
class PageControlledPolicy extends DataExtension
18
{
19
    /**
20
     * @var array
21
     */
22
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        'MaxAge' => 'Varchar'
24
    ];
25
26
    /**
27
     * Extension point for the CachingPolicy.
28
     *
29
     * @param int $cacheAge
30
     */
31
    public function getCacheAge($cacheAge)
0 ignored issues
show
Unused Code introduced by
The parameter $cacheAge is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

31
    public function getCacheAge(/** @scrutinizer ignore-unused */ $cacheAge)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
    {
33
        if ($this->owner->MaxAge != '') {
34
            return (int)($this->owner->MaxAge * 60);
35
        }
36
    }
37
38
    /**
39
     * @param  FieldList $fields
40
     */
41
    public function updateCMSFields(FieldList $fields)
42
    {
43
        // Only admins are allowed to modify this.
44
        $member = Security::getCurrentUser();
45
        if (!$member || !Permission::checkMember($member, 'ADMIN')) {
0 ignored issues
show
introduced by
The condition ! $member || ! SilverStr...ember($member, 'ADMIN') can never be false.
Loading history...
46
            return;
47
        }
48
49
        $fields->addFieldsToTab('Root.Caching', [
50
            LiteralField::create('Instruction', '<p>The following field controls the length of time the page will ' .
51
                'be cached for. You will not be able to see updates to this page for at most the specified ' .
52
                'amount of minutes. Leave empty to set back to the default configured for your site. Set ' .
53
                'to 0 to explicitly disable caching for this page.</p>'),
54
            TextField::create('MaxAge', 'Custom cache timeout [minutes]')
55
        ]);
56
    }
57
}
58