Passed
Push — master ( b101db...a45b24 )
by Robbie
05:17
created

FileArchiveExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getArchiveFieldClass() 0 3 1
A getArchiveField() 0 23 3
A isArchiveFieldEnabled() 0 3 1
1
<?php
2
3
namespace SilverStripe\VersionedAdmin\Extensions;
4
5
use SilverStripe\Assets\AssetControlExtension;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Forms\GridField\GridFieldDataColumns;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\Security\Member;
11
use SilverStripe\VersionedAdmin\ArchiveAdmin;
12
use SilverStripe\VersionedAdmin\ArchiveViewProvider;
13
14
/**
15
 * Adds a archive view for Files
16
 */
17
class FileArchiveExtension extends DataExtension implements ArchiveViewProvider
18
{
19
    /**
20
     * @inheritDoc
21
    */
22
    public function getArchiveFieldClass()
23
    {
24
        return File::class;
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\Assets\File::class returns the type string which is incompatible with the return type mandated by SilverStripe\VersionedAd...:getArchiveFieldClass() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
25
    }
26
27
    /**
28
     * @inheritDoc
29
    */
30
    public function getArchiveField()
31
    {
32
        $listField = ArchiveAdmin::createArchiveGridField('Files', File::class);
33
34
        $listColumns = $listField->getConfig()->getComponentByType(GridFieldDataColumns::class);
35
        $listColumns->setDisplayFields([
0 ignored issues
show
Bug introduced by
The method setDisplayFields() does not exist on SilverStripe\Forms\GridField\GridFieldComponent. It seems like you code against a sub-type of SilverStripe\Forms\GridField\GridFieldComponent such as SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldDataColumns or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\Tests...ndlerTest\TestComponent or SilverStripe\Forms\GridField\GridFieldDetailForm. ( Ignorable by Annotation )

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

35
        $listColumns->/** @scrutinizer ignore-call */ 
36
                      setDisplayFields([
Loading history...
36
            'Name' => File::singleton()->fieldLabel('Name'),
37
            'appCategory' => _t(ArchiveAdmin::class . '.COLUMN_TYPE', 'Type'),
38
            'LastEdited.Ago' => _t(ArchiveAdmin::class . '.COLUMN_DATEARCHIVED', 'Date Archived'),
39
            'Parent.Name' => _t(ArchiveAdmin::class . '.COLUMN_ORIGIN', 'Origin'),
40
            'AuthorID' => _t(ArchiveAdmin::class . '.COLUMN_ARCHIVEDBY', 'Archived By'),
41
        ]);
42
        $listColumns->setFieldFormatting([
0 ignored issues
show
Bug introduced by
The method setFieldFormatting() does not exist on SilverStripe\Forms\GridField\GridFieldComponent. It seems like you code against a sub-type of SilverStripe\Forms\GridField\GridFieldComponent such as SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\GridField\GridFieldDataColumns or SilverStripe\Forms\GridField\GridFieldPrintButton or SilverStripe\Forms\Tests...ndlerTest\TestComponent or SilverStripe\Forms\GridField\GridFieldDetailForm. ( Ignorable by Annotation )

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

42
        $listColumns->/** @scrutinizer ignore-call */ 
43
                      setFieldFormatting([
Loading history...
43
            'appCategory' => function ($val, $item) {
44
                return ucfirst($val ?: $item->i18n_singular_name());
45
            },
46
            'AuthorID' => function ($val, $item) {
0 ignored issues
show
Unused Code introduced by
The parameter $item 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

46
            'AuthorID' => function ($val, /** @scrutinizer ignore-unused */ $item) {

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...
47
                $member = Member::get_by_id($val);
48
                return $member ? $member->Name : null;
0 ignored issues
show
introduced by
$member is of type SilverStripe\ORM\DataObject, thus it always evaluated to true.
Loading history...
49
            },
50
        ]);
51
52
        return $listField;
53
    }
54
55
    /**
56
     * The files archive is only useful if archived assets are stored
57
     * so this checks if this option is enabled
58
     *
59
     * @return boolean
60
    */
61
    public function isArchiveFieldEnabled()
62
    {
63
        return Config::inst()->get(AssetControlExtension::class, 'keep_archived_assets');
64
    }
65
}
66