FileArchiveExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 28
c 2
b 1
f 1
dl 0
loc 62
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getArchiveFieldClass() 0 3 1
A getArchiveField() 0 38 2
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\ORM\FieldType\DBDatetime;
11
use SilverStripe\Versioned\GridFieldRestoreAction;
12
use SilverStripe\VersionedAdmin\ArchiveAdmin;
13
use SilverStripe\VersionedAdmin\Forms\GridField\GridFieldFileRestoreAction;
14
use SilverStripe\VersionedAdmin\Interfaces\ArchiveViewProvider;
15
16
/**
17
 * Adds a archive view for Files
18
 */
19
class FileArchiveExtension extends DataExtension implements ArchiveViewProvider
20
{
21
    /**
22
     * @inheritDoc
23
    */
24
    public function getArchiveFieldClass()
25
    {
26
        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...
27
    }
28
29
    /**
30
     * @inheritDoc
31
    */
32
    public function getArchiveField()
33
    {
34
        $listField = ArchiveAdmin::createArchiveGridField('Files', File::class);
35
36
        $list = $listField->getList();
37
        // Paginator reports all records even if some can't be viewed, so we filter them out here
38
        $list = $list->filterByCallback(function ($item) {
0 ignored issues
show
Bug introduced by
The method filterByCallback() does not exist on SilverStripe\ORM\SS_List. It seems like you code against a sub-type of said class. However, the method does not exist in SilverStripe\ORM\Sortable or SilverStripe\ORM\Limitable. Are you sure you never get one of those? ( Ignorable by Annotation )

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

38
        /** @scrutinizer ignore-call */ 
39
        $list = $list->filterByCallback(function ($item) {
Loading history...
39
            return $item->canView();
40
        });
41
        $listField->setList($list);
42
        $listConfig = $listField->getConfig();
43
        $listConfig->removeComponentsByType(GridFieldRestoreAction::class);
44
        $listConfig->addComponent(new GridFieldFileRestoreAction());
45
46
        $listColumns = $listField->getConfig()->getComponentByType(GridFieldDataColumns::class);
47
        $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

47
        $listColumns->/** @scrutinizer ignore-call */ 
48
                      setDisplayFields([
Loading history...
48
            'Name' => File::singleton()->fieldLabel('Name'),
49
            'appCategory' => _t('SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_TYPE', 'Type'),
50
            'Versions.first.LastEdited' => _t(
51
                'SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_DATEARCHIVED',
52
                'Date Archived'
53
            ),
54
            'Parent.Name' => _t('SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_ORIGIN', 'Origin'),
55
            'Versions.first.Author.Name' => _t(
56
                'SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_ARCHIVEDBY',
57
                'Archived By'
58
            )
59
        ]);
60
        $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

60
        $listColumns->/** @scrutinizer ignore-call */ 
61
                      setFieldFormatting([
Loading history...
61
            'appCategory' => function ($val, $item) {
62
                return ucfirst($val ?: $item->i18n_singular_name());
63
            },
64
            'Versions.first.LastEdited' => 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

64
            'Versions.first.LastEdited' => 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...
65
                return DBDatetime::create_field('Datetime', $val)->Ago();
66
            },
67
        ]);
68
69
        return $listField;
70
    }
71
72
    /**
73
     * The files archive is only useful if archived assets are stored
74
     * so this checks if this option is enabled
75
     *
76
     * @return boolean
77
    */
78
    public function isArchiveFieldEnabled()
79
    {
80
        return Config::inst()->get(AssetControlExtension::class, 'keep_archived_assets');
81
    }
82
}
83