SitewideContentTaxonomy::enabled()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\SiteWideContentReport\Model;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Extension;
7
use SilverStripe\Taxonomy\TaxonomyTerm;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Taxonomy\TaxonomyTerm was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
/**
10
 * Provides taxonomy integration for sitewide content report.
11
 *
12
 * Requires https://github.com/silverstripe-labs/silverstripe-taxonomy
13
 *
14
 * Class SitewideContentTaxonomy
15
 * @package SilverStripe\SiteWideContentReport\Model
16
 */
17
class SitewideContentTaxonomy extends Extension
18
{
19
    /**
20
     * Name of field to get tags from.
21
     *
22
     * @config
23
     *
24
     * @var string
25
     */
26
    private static $tag_field = 'Terms';
0 ignored issues
show
introduced by
The private property $tag_field is not used, and could be removed.
Loading history...
27
28
    /**
29
     * Update columns to include taxonomy details.
30
     *
31
     * @param string $itemType (i.e 'Pages' or 'Files')
32
     * @param array  $columns  Columns
33
     */
34
    public function updateColumns($itemType, &$columns)
35
    {
36
        if ($itemType !== 'Pages') {
37
            return;
38
        }
39
40
        // Check if pages has the tags field
41
        if (!self::enabled()) {
42
            return;
43
        }
44
45
        // Set column
46
        $field = Config::inst()->get(__CLASS__, 'tag_field');
47
        $columns['Terms'] = [
48
            'printonly' => true, // Hide on page report
49
            'title' => _t('SilverStripe\\SiteWideContentReport\\SitewideContentReport.Tags', 'Tags'),
50
            'datasource' => function ($record) use ($field) {
51
                $tags = $record->$field()->column('Name');
52
53
                return implode(', ', $tags);
54
            },
55
        ];
56
    }
57
58
    /**
59
     * Check if this field is enabled.
60
     *
61
     * @return bool
62
     */
63
    public static function enabled()
64
    {
65
        if (!class_exists(TaxonomyTerm::class)) {
66
            return false;
67
        }
68
69
        // Check if pages has the tags field
70
        $field = Config::inst()->get(__CLASS__, 'tag_field');
71
72
        return singleton('Page')->hasMethod($field);
73
    }
74
}
75