Completed
Push — master ( f39c4d...b2e354 )
by Sam
03:35 queued 03:17
created

VersionedGridFieldItemRequest   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 183
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
C getFormActions() 0 64 8
B doArchive() 0 25 2
B doPublish() 0 29 2
A doUnpublish() 0 22 2
A setFormMessage() 0 10 2
1
<?php
2
3
namespace SilverStripe\ORM\Versioning;
4
5
use SilverStripe\Control\HTTPResponse;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Forms\Form;
8
use SilverStripe\Forms\FormAction;
9
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\ORM\ValidationResult;
12
13
/**
14
 * Provides versioned dataobject support to {@see GridFieldDetailForm_ItemRequest}
15
 *
16
 * @property GridFieldDetailForm_ItemRequest $owner
17
 */
18
class VersionedGridFieldItemRequest extends GridFieldDetailForm_ItemRequest
19
{
20
21
    protected function getFormActions()
22
    {
23
        $actions = parent::getFormActions();
24
25
        // Check if record is versionable
26
        /** @var Versioned|DataObject $record */
27
        $record = $this->getRecord();
28
        if (!$record || !$record->has_extension(Versioned::class)) {
0 ignored issues
show
Bug introduced by
The method has_extension does only exist in SilverStripe\ORM\DataObject, but not in SilverStripe\ORM\Versioning\Versioned.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
29
            return $actions;
30
        }
31
32
        // Save & Publish action
33
        if ($record->canPublish()) {
0 ignored issues
show
Bug introduced by
The method canPublish does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34
            // "publish", as with "save", it supports an alternate state to show when action is needed.
35
            $publish = FormAction::create(
36
                'doPublish',
37
                _t('VersionedGridFieldItemRequest.BUTTONPUBLISH', 'Publish')
38
            )
39
                ->setUseButtonTag(true)
40
                ->addExtraClass('ss-ui-action-constructive')
41
                ->setAttribute('data-icon', 'accept');
42
43
            // Insert after save
44
            if ($actions->fieldByName('action_doSave')) {
45
                $actions->insertAfter('action_doSave', $publish);
46
            } else {
47
                $actions->push($publish);
48
            }
49
        }
50
51
        // Unpublish action
52
        $isPublished = $record->isPublished();
0 ignored issues
show
Bug introduced by
The method isPublished does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
53
        if ($isPublished && $record->canUnpublish()) {
0 ignored issues
show
Bug introduced by
The method canUnpublish does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
54
            $actions->push(
55
                FormAction::create(
56
                    'doUnpublish',
57
                    _t('VersionedGridFieldItemRequest.BUTTONUNPUBLISH', 'Unpublish')
58
                )
59
                    ->setUseButtonTag(true)
60
                    ->setDescription(_t(
61
                        'VersionedGridFieldItemRequest.BUTTONUNPUBLISHDESC',
62
                        'Remove this record from the published site'
63
                    ))
64
                    ->addExtraClass('ss-ui-action-destructive')
65
            );
66
        }
67
68
        // Archive action
69
        if ($record->canArchive()) {
0 ignored issues
show
Bug introduced by
The method canArchive does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
            // Replace "delete" action
71
            $actions->removeByName('action_doDelete');
72
73
            // "archive"
74
            $actions->push(
75
                FormAction::create('doArchive', _t('VersionedGridFieldItemRequest.ARCHIVE', 'Archive'))
76
                    ->setDescription(_t(
77
                        'VersionedGridFieldItemRequest.BUTTONARCHIVEDESC',
78
                        'Unpublish and send to archive'
79
                    ))
80
                    ->addExtraClass('delete ss-ui-action-destructive')
81
            );
82
        }
83
        return $actions;
84
    }
85
86
    /**
87
     * Archive this versioned record
88
     *
89
     * @param array $data
90
     * @param Form $form
91
     * @return HTTPResponse
92
     */
93
    public function doArchive($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        /** @var Versioned|DataObject $record */
96
        $record = $this->getRecord();
97
        if (!$record->canArchive()) {
0 ignored issues
show
Bug introduced by
The method canArchive does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
98
            return $this->httpError(403);
99
        }
100
101
        // Record name before it's deleted
102
        $title = $record->Title;
103
            $record->doArchive();
0 ignored issues
show
Bug introduced by
The method doArchive does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
104
105
        $message = sprintf(
106
            _t('VersionedGridFieldItemRequest.Archived', 'Archived %s %s'),
107
            $record->i18n_singular_name(),
0 ignored issues
show
Bug introduced by
The method i18n_singular_name does only exist in SilverStripe\ORM\DataObject, but not in SilverStripe\ORM\Versioning\Versioned.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
108
            Convert::raw2xml($title)
109
        );
110
        $this->setFormMessage($form, $message);
111
112
        //when an item is deleted, redirect to the parent controller
113
        $controller = $this->getToplevelController();
114
        $controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh
115
116
        return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section
117
    }
118
119
    /**
120
     * Publish this versioned record
121
     *
122
     * @param array $data
123
     * @param Form $form
124
     * @return HTTPResponse
125
     */
126
    public function doPublish($data, $form)
127
    {
128
        /** @var Versioned|DataObject $record */
129
        $record = $this->getRecord();
130
        $isNewRecord = $record->ID == 0;
131
132
        // Check permission
133
        if (!$record->canPublish()) {
0 ignored issues
show
Bug introduced by
The method canPublish does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
134
            return $this->httpError(403);
135
        }
136
137
            // Initial save and reload
138
            $record = $this->saveFormIntoRecord($data, $form);
139
            $record->publishRecursive();
140
        $editURL = $this->Link('edit');
141
        $xmlTitle = Convert::raw2xml($record->Title);
142
        $link = "<a href=\"{$editURL}\">{$xmlTitle}</a>";
143
        $message = _t(
144
            'VersionedGridFieldItemRequest.Published',
145
            'Published {name} {link}',
146
            array(
147
                'name' => $record->i18n_singular_name(),
148
                'link' => $link
149
            )
150
        );
151
        $this->setFormMessage($form, $message);
152
153
        return $this->redirectAfterSave($isNewRecord);
154
    }
155
156
    /**
157
     * Delete this record from the live site
158
     *
159
     * @param array $data
160
     * @param Form $form
161
     * @return HTTPResponse
162
     */
163
    public function doUnpublish($data, $form)
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        /** @var Versioned|DataObject $record */
166
        $record = $this->getRecord();
167
        if (!$record->canUnpublish()) {
0 ignored issues
show
Bug introduced by
The method canUnpublish does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
168
            return $this->httpError(403);
169
        }
170
171
        // Record name before it's deleted
172
        $title = $record->Title;
173
            $record->doUnpublish();
0 ignored issues
show
Bug introduced by
The method doUnpublish does only exist in SilverStripe\ORM\Versioning\Versioned, but not in SilverStripe\ORM\DataObject.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
174
175
        $message = sprintf(
176
            _t('VersionedGridFieldItemRequest.Unpublished', 'Unpublished %s %s'),
177
            $record->i18n_singular_name(),
0 ignored issues
show
Bug introduced by
The method i18n_singular_name does only exist in SilverStripe\ORM\DataObject, but not in SilverStripe\ORM\Versioning\Versioned.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
178
            Convert::raw2xml($title)
179
        );
180
        $this->setFormMessage($form, $message);
181
182
        // Redirect back to edit
183
        return $this->redirectAfterSave(false);
184
    }
185
186
    /**
187
     * @param Form $form
188
     * @param string $message
189
     */
190
    protected function setFormMessage($form, $message)
191
    {
192
        $form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
193
        $controller = $this->getToplevelController();
194
        if ($controller->hasMethod('getEditForm')) {
195
            /** @var Form $backForm */
196
            $backForm = $controller->getEditForm();
197
            $backForm->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
198
        }
199
    }
200
}
201