Passed
Pull Request — master (#20)
by Matthew
01:54
created

SalsifyIDExtension::updateSettingsFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 1
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\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...
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Forms\DatetimeField;
9
use SilverStripe\Forms\FieldList;
10
use SilverStripe\Forms\FormAction;
11
use SilverStripe\Forms\TextField;
12
use SilverStripe\ORM\DataExtension;
13
14
/**
15
 * Class FileExtension
16
 *
17
 * @property string SalsifyID
18
 * @property string SalsifyUpdatedAt
19
 *
20
 * @property-read \SilverStripe\ORM\DataObject|\Dyanmic\Salsify\ORM\SalsifyIDExtension $owner
21
 */
22
class SalsifyIDExtension extends DataExtension
23
{
24
25
    /**
26
     * @var array
27
     */
28
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
29
        'SalsifyID' => 'Varchar(255)',
30
        'SalsifyUpdatedAt' => 'Varchar(255)'
31
    ];
32
33
    /**
34
     * @param \SilverStripe\Forms\FieldList $fields
35
     */
36
    protected function updateFields(FieldList $fields)
37
    {
38
        $salsifyID = $fields->dataFieldByName('SalsifyID');
39
        if (!$salsifyID) {
0 ignored issues
show
introduced by
$salsifyID is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
40
            $fields->push($salsifyID = TextField::create('SalsifyID'));
41
        }
42
43
        $salsifyUpdatedAt = $fields->dataFieldByName('SalsifyUpdatedAt');
44
        if (!$salsifyUpdatedAt) {
0 ignored issues
show
introduced by
$salsifyUpdatedAt is of type SilverStripe\Forms\FormField, thus it always evaluated to true.
Loading history...
45
            $fields->push($salsifyUpdatedAt = DatetimeField::create('SalsifyUpdatedAt'));
46
        }
47
48
        if ($this->owner->SalsifyID) {
49
            $salsifyID->setReadonly(true);
50
        }
51
        $salsifyUpdatedAt->setReadonly(true);
52
    }
53
54
    /**
55
     * @param \SilverStripe\Forms\FieldList $fields
56
     */
57
    public function updateCMSFields(FieldList $fields)
58
    {
59
        if ($this->owner instanceof SiteTree) {
60
            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...
61
        }
62
63
        $this->updateFields($fields);
64
        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...
65
    }
66
67
    /**
68
     * @param \SilverStripe\Forms\FieldList $fields
69
     */
70
    public function updateSettingsFields(FieldList $fields) {
71
        $this->updateFields($fields);
72
    }
73
74
    /**
75
     * @param \SilverStripe\Forms\FieldList $actions
76
     */
77
    public function updateCMSActions(FieldList $actions)
78
    {
79
        parent::updateCMSActions($actions);
80
81
        if (!$this->owner->SalsifyID) {
82
            return;
83
        }
84
85
        $controller = Controller::curr();
86
        if ($controller instanceof LeftAndMain && $controller->canFetchSalsify()) {
87
            /** @var FormAction $action */
88
            $action = FormAction::create('salsifyFetch', 'Re-fetch Salsify')
89
                ->addExtraClass('btn-primary font-icon-sync')
90
                ->setUseButtonTag(true);
91
92
            $actions->push($action);
93
        }
94
    }
95
}
96