Passed
Push — master ( 96d942...a9437e )
by Matthew
07:44
created

SalsifyIDExtension::onBeforeWrite()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dyanmic\Salsify\ORM;
4
5
use SilverStripe\Admin\LeftAndMain;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\LeftAndMain 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...
6
use SilverStripe\CMS\Forms\SiteTreeURLSegmentField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Forms\SiteTreeURLSegmentField 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...
7
use SilverStripe\CMS\Model\SiteTree;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Model\SiteTree 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
use SilverStripe\Control\Controller;
9
use SilverStripe\Forms\DatetimeField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\TextField;
13
use SilverStripe\ORM\DataExtension;
14
15
/**
16
 * Class FileExtension
17
 *
18
 * @property string SalsifyID
19
 * @property string SalsifyUpdatedAt
20
 *
21
 * @property-read \SilverStripe\ORM\DataObject|\Dyanmic\Salsify\ORM\SalsifyIDExtension $owner
22
 */
23
class SalsifyIDExtension extends DataExtension
24
{
25
26
    /**
27
     * @var array
28
     */
29
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
30
        'SalsifyID' => 'Varchar(255)',
31
        'SalsifyUpdatedAt' => 'Varchar(255)'
32
    ];
33
34
    /**
35
     * @param \SilverStripe\Forms\FieldList $fields
36
     */
37
    protected function updateFields(FieldList $fields)
38
    {
39
        $salsifyID = $fields->fieldByName('SalsifyID');
40
        if (!$salsifyID) {
0 ignored issues
show
introduced by
$salsifyID is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
41
            $fields->push($salsifyID = TextField::create('SalsifyID'));
42
        }
43
44
        $salsifyUpdatedAt = $fields->fieldByName('SalsifyUpdatedAt');
45
        if (!$salsifyUpdatedAt) {
0 ignored issues
show
introduced by
$salsifyUpdatedAt is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
46
            $fields->push($salsifyUpdatedAt = DatetimeField::create('SalsifyUpdatedAt'));
47
        }
48
49
        if ($this->owner->SalsifyID) {
50
            $salsifyID->setTemplate(SiteTreeURLSegmentField::class)->addExtraClass('urlsegment');
51
        }
52
        $salsifyUpdatedAt->setReadonly(true);
53
    }
54
55
    /**
56
     * @param \SilverStripe\Forms\FieldList $fields
57
     */
58
    public function updateCMSFields(FieldList $fields)
59
    {
60
        if ($this->owner instanceof SiteTree) {
61
            return parent::updateCMSFields($fields);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::updateCMSFields($fields) targeting SilverStripe\ORM\DataExtension::updateCMSFields() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
        }
63
64
        $this->updateFields($fields);
65
        return parent::updateCMSFields($fields);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::updateCMSFields($fields) targeting SilverStripe\ORM\DataExtension::updateCMSFields() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
66
    }
67
68
    /**
69
     * @param \SilverStripe\Forms\FieldList $fields
70
     */
71
    public function updateSettingsFields(FieldList $fields)
72
    {
73
        $this->updateFields($fields);
74
    }
75
76
    /**
77
     * @param \SilverStripe\Forms\FieldList $actions
78
     */
79
    public function updateCMSActions(FieldList $actions)
80
    {
81
        parent::updateCMSActions($actions);
82
83
        if (!$this->owner->SalsifyID) {
84
            return;
85
        }
86
87
        $controller = Controller::curr();
88
        if ($controller instanceof LeftAndMain && $controller->canFetchSalsify()) {
89
            /** @var FormAction $action */
90
            $action = FormAction::create('salsifyFetch', 'Re-fetch Salsify')
91
                ->addExtraClass('btn-primary font-icon-sync')
92
                ->setUseButtonTag(true);
93
94
            $actions->push($action);
95
        }
96
    }
97
98
    /**
99
     *
100
     */
101
    public function onBeforeWrite()
102
    {
103
        parent::onBeforeWrite();
104
105
        $this->owner->SalsifyID = trim($this->owner->SalsifyID);
106
    }
107
}
108