Completed
Pull Request — master (#71)
by John
02:22
created

ElementalVersionedExtension   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 95
Duplicated Lines 32.63 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 5
dl 31
loc 95
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A updateSummaryFields() 0 4 1
A canView() 11 11 4
A canEdit() 10 10 4
A canDelete() 10 10 4
A canCreate() 0 6 3
A doUnpublish() 0 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
use \Heyday\VersionedDataObjects\VersionedDataObject;
4
5
/**
6
 * @package elemental
7
 * Has to extend VersionedDataObject rather than suppliment so that we can overwrite the canXs
8
 * Otherwise the Versioned canXs fail when these check suceed, the fail overides
9
 */
10
class ElementalVersionedExtension extends VersionedDataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
13
    /**
14
     * @param array $fields
15
     */
16
    public function updateSummaryFields(&$fields)
17
    {
18
        unset($fields['CMSPublishedState']);
19
    }
20
21
    /**
22
     * Basic permissions, defaults to page perms where possible
23
     */
24 View Code Duplication
    public function canView($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        if ($page = $this->owner->getPage()) {
27
28
            return $page->canView($member);
29
        }
30
31
        if(Director::is_cli()) return true;
32
33
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
34
    }
35
36
    /**
37
     * Basic permissions, defaults to page perms where possible
38
     */
39 View Code Duplication
    public function canEdit($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        if ($page = $this->owner->getPage()) {
42
            return $page->canEdit($member);
43
        }
44
45
        if(Director::is_cli()) return true;
46
47
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
48
    }
49
50
    /**
51
     * Basic permissions, defaults to page perms where possible
52
     * Uses archive not delete so that current stage is respected
53
     * i.e if a widget is not published, then it can be deleted by someone who
54
     * doesn't have publishing permissions
55
     */
56 View Code Duplication
    public function canDelete($member = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        if ($page = $this->owner->getPage()) {
59
            return $page->canArchive($member);
60
        }
61
62
        if(Director::is_cli()) return true;
63
64
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
65
    }
66
67
    /**
68
     * Basic permissions, defaults to page perms where possible
69
     */
70
    public function canCreate($member = null)
71
    {
72
        if(Director::is_cli()) return true;
73
74
        return (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) ? true : null;
75
    }
76
77
    /**
78
     * Handles unpublishing as VersionedDataObjects doesn't
79
     * Modelled on SiteTree::doUnpublish
80
     * Has to be applied here, rather than BaseElement so that goes against Widget
81
     */
82
    public function doUnpublish() {
83
        if(!$this->owner->ID) return false;
84
85
        $this->owner->extend('onBeforeUnpublish');
86
87
        $origStage = Versioned::get_reading_mode();
88
        Versioned::set_reading_mode('Stage.Live');
89
90
        // This way our ID won't be unset
91
        $clone = clone $this->owner;
92
        $clone->delete();
93
94
        Versioned::set_reading_mode($origStage);
95
96
        $virtualLinkedElements = $this->owner->getPublishedVirtualLinkedElements();
97
        if ($virtualLinkedElements) foreach($virtualLinkedElements as $vle) $vle->doUnpublish();
98
99
        $this->owner->extend('onAfterUnpublish');
100
101
        return true;
102
    }
103
104
}
105