Passed
Push — master ( cc8d1f...e8ba3c )
by Nicolaas
12:28
created

CMSNicetiesTraitForCMSLinks::editLink()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 15
rs 9.8666

1 Method

Rating   Name   Duplication   Size   Complexity  
A CMSNicetiesTraitForCMSLinks::CMSAddLink() 0 3 1
1
<?php
2
3
namespace Sunnysideup\CMSNiceties\Traits;
4
5
use SilverStripe\Admin\ModelAdmin;
6
// use SilverStripe\Forms\GridField\GridFieldArchiveAction;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Core\Injector\Injector;
10
use SilverStripe\Forms\HTMLReadonlyField;
11
use Sunnysideup\CmsEditLinkField\Api\CMSEditLinkAPI;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\CmsEditLinkField\Api\CMSEditLinkAPI 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...
12
13
trait CMSNicetiesTraitForCMSLinks
14
{
15
    public function CMSEditLink(): string
16
    {
17
        if ($this instanceof SiteTree) {
18
            return parent::CMSEditLink();
19
        }
20
        return CMSEditLinkAPI::find_edit_link_for_object($this);
21
    }
22
23
    public function CMSEditLinkLimited(): string
24
    {
25
        return CMSEditLinkAPI::find_edit_link_for_object($this);
26
    }
27
28
    public function CMSEditLinkField(string $relName, string $name = ''): HTMLReadonlyField
29
    {
30
        $obj = $this->{$relName}();
31
        if (!$name) {
32
            $nameOptions = $this->fieldLabels();
0 ignored issues
show
Bug introduced by
It seems like fieldLabels() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

32
            /** @scrutinizer ignore-call */ 
33
            $nameOptions = $this->fieldLabels();
Loading history...
33
            $name = $nameOptions[$relName] ?? $nameOptions[$relName . 'ID'] ?? 'error';
34
        }
35
36
        if ($obj && $obj->exists()) {
37
            $value = '<a href="' . $obj->CMSEditLink() . '">' . $obj->getTitle() . '</a>';
38
        } else {
39
            $value = '<em>(none)</em>';
40
        }
41
42
        return HTMLReadonlyField::create(
43
            $relName . 'Link',
44
            $name,
45
            $value
46
        );
47
    }
48
49
    public function CMSAddLink(): string
50
    {
51
        return CMSEditLinkAPI::find_add_link_for_object($this->ClassName);
52
    }
53
54
    public function CMSListLink(): string
55
    {
56
        $controller = $this->myModelAdminController();
57
        if ($controller) {
58
            return Controller::join_links(
59
                $controller->Link(),
60
                $this->sanitisedClassName()
61
            );
62
        }
63
64
        return '404-cms-list-link-not-found';
65
    }
66
67
    /**
68
     * @return null|ModelAdmin
69
     */
70
    protected function myModelAdminController()
71
    {
72
        $modelAdminClassName = $this->Config()->get('primary_model_admin_class');
0 ignored issues
show
Bug introduced by
It seems like Config() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

72
        $modelAdminClassName = $this->/** @scrutinizer ignore-call */ Config()->get('primary_model_admin_class');
Loading history...
73
        $obj = null;
74
        if ($modelAdminClassName) {
75
            /** @var null|ModelAdmin $obj */
76
            $obj = Injector::inst()->get($modelAdminClassName);
77
        }
78
79
        return $obj;
80
    }
81
82
    /**
83
     * Sanitise a model class' name for inclusion in a link.
84
     */
85
    protected function sanitisedClassName(): string
86
    {
87
        $className = (string) $this->hasMethod('classNameForModelAdmin') ? $this->classNameForModelAdmin() : $this->ClassName;
0 ignored issues
show
Bug introduced by
It seems like classNameForModelAdmin() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

87
        $className = (string) $this->hasMethod('classNameForModelAdmin') ? $this->/** @scrutinizer ignore-call */ classNameForModelAdmin() : $this->ClassName;
Loading history...
Bug introduced by
It seems like hasMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

87
        $className = (string) $this->/** @scrutinizer ignore-call */ hasMethod('classNameForModelAdmin') ? $this->classNameForModelAdmin() : $this->ClassName;
Loading history...
88
89
        return str_replace('\\', '-', $className);
90
    }
91
}
92