TwitterAccount   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 12.5%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 134
ccs 7
cts 56
cp 0.125
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getCMSFields() 0 42 2
B getCMSActions() 0 27 3
A getCMSValidator() 0 4 1
A setAccessToken() 0 4 1
A getAccessToken() 0 4 1
A isAuthorised() 0 4 1
1
<?php
2
3
class TwitterAccount extends DataObject
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...
4
{
5
    public static $dependencies = [
6
        'twitterService' => '%$TwitterService',
7
    ];
8
9
    /**
10
     * @var TwitterService
11
     */
12
    public $twitterService;
13
14
    /**
15
     * @var array
16
     */
17
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
        'Title' => 'Varchar',
19
        'AccessToken' => 'Text',
20
    ];
21
22
    /**
23
     * @return FieldList
24
     */
25
    public function getCMSFields()
26
    {
27
        $fields = parent::getCMSFields();
28
29
        $fields->removeByName('AccessToken');
30
31
        if ($this->isAuthorised()) {
32
            $usernameField = LiteralField::create(
33
                'Title',
34
                '<p class="message good">' .
35
                    _t('Twitter.AccountAuthorisedMessage', 'This account has been authorised.') .
36
                '</p>' .
37
                '<div class="field">' .
38
                    '<label class="left">' .
39
                        _t('Twitter.FieldLabelTitle', 'Username') .
40
                    '</label>' .
41
                    '<div class="middleColumn" style="padding-top:8px;">' .
42
                        '<a ' .
43
                            "href='https://www.twitter.com/{$this->Title}' " .
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<TwitterAccount>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
                            'title="View on Twitter" ' .
45
                            'target="_blank">' .
46
                            $this->Title .
0 ignored issues
show
Documentation introduced by
The property Title does not exist on object<TwitterAccount>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
47
                        '</a>' .
48
                    '</div>' .
49
                '</div>'
50
            );
51
        } else {
52
            $usernameField = Textfield::create('Title', _t('Twitter.FieldLabelTitle', 'Username'));
53
            $usernameField->setDescription(
54
                _t(
55
                    'Twitter.FieldDescriptionTitle',
56
                    'The Twitter account you want to pull media from.'
57
                )
58
            );
59
        }
60
61
        $fields->addFieldToTab('Root.Main', $usernameField);
62
63
        $this->extend('updateCMSFields', $fields);
64
65
        return $fields;
66
    }
67
68
    /**
69
     * @return FieldList
70
     */
71
    public function getCMSActions()
72
    {
73
        $actions = parent::getCMSActions();
74
75
        if (!$this->getField('ID') || $this->isAuthorised()) {
76
            $this->extend('updateCMSActions', $actions);
77
            return $actions;
78
        }
79
80
        $token = $this->twitterService->getOAuthToken();
81
82
        // Set the token in session so it can be accessed throughout the OAuth flow.
83
        $this->twitterService->setSessionOAuthToken($token);
84
85
        $actions->push(
86
            LiteralField::create(
87
                'OAuthLink',
88
                '<a class="ss-ui-button" href="' . $this->twitterService->getAuthoriseUrl($token) . '">' .
89
                    _t('Twitter.ButtonLabelAuthoriseAccount', 'Authorise account') .
90
                '</a>'
91
            )
92
        );
93
94
        $this->extend('updateCMSActions', $actions);
95
96
        return $actions;
97
    }
98
99
    /**
100
     * @return RequiredFields
101
     */
102
    public function getCMSValidator()
103
    {
104
        return new RequiredFields('Title');
105
    }
106
107
    /**
108
     * Sets an OAuth token for the TwitterAccount.
109
     *
110
     * @param array $token
111
     */
112 1
    public function setAccessToken($token)
113
    {
114 1
        $this->setField('AccessToken', serialize($token));
115 1
    }
116
117
    /**
118
     * Gets the TwitterAccount's OAuth token.
119
     *
120
     * @return array
121
     */
122 1
    public function getAccessToken()
123
    {
124 1
        return unserialize($this->getField('AccessToken'));
125
    }
126
127
    /**
128
     * Checks if the TwitterAccount has been authorised via OAuth.
129
     *
130
     * @return boolean
131
     */
132 1
    public function isAuthorised()
133
    {
134 1
        return (bool) $this->getField('AccessToken');
135
    }
136
}
137