TwitterAdminRequestHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 26
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ItemEditForm() 0 14 3
1
<?php
2
3
class TwitterAdmin extends ModelAdmin
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 string
16
     */
17
    private static $url_segment = 'twitter';
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 $url_segment 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
19
    /**
20
     * @var string
21
     */
22
    private static $menu_icon = 'silverstripe-twitter/images/twitter-logo.png';
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 $menu_icon 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...
23
24
    /**
25
     * @var string
26
     */
27
    private static $menu_title = 'Twitter';
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 $menu_title 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...
28
29
    /**
30
     * @var array
31
     */
32
    private static $managed_models = [
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 $managed_models 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...
33
        'TwitterAccount',
34
    ];
35
36
    /**
37
     * @var array
38
     */
39
    private static $allowed_actions = [
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 $allowed_actions 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...
40
        'ImportForm',
41
        'SearchForm',
42
        'OAuth',
43
    ];
44
45
    /**
46
     * @param int $id
47
     *
48
     * @param FieldList $fields
49
     *
50
     * @return Form
51
     */
52
    public function getEditForm($id = null, $fields = null)
53
    {
54
        $form = parent::getEditForm($id, $fields);
55
56
        $gridFieldName = $this->sanitiseClassName($this->modelClass);
57
        $gridFieldConfig = $form->Fields()->fieldByName($gridFieldName)->getConfig();
58
59
        $gridFieldConfig->removeComponentsByType('GridFieldPrintButton');
60
        $gridFieldConfig->removeComponentsByType('GridFieldExportButton');
61
        $gridFieldConfig
62
            ->getComponentByType('GridFieldAddNewButton')
63
            ->setButtonName(_t('Twitter.ButtonLabelAddAccount', 'Add account'));
64
        $gridFieldConfig
65
            ->getComponentByType('GridFieldDetailForm')
66
            ->setItemRequestClass('TwitterAdminRequestHandler');
67
68
        return $form;
69
    }
70
71
    /**
72
     * Handles failed OAuth attempts.
73
     *
74
     * @param string $url
75
     *
76
     * @param Form $form
77
     *
78
     * @return Controller
79
     */
80
    public function handleOAuthError($url, $form, $message = null)
81
    {
82
        $message = $message ? $message : _t(
83
            'Twitter.MessageOAuthErrorResponse',
84
            'Unable to authorise account. Please try again.'
85
        );
86
87
        $form->sessionMessage($message, 'bad');
88
89
        user_error($message, E_USER_WARNING);
90
91
        return Controller::curr()->redirect($url);
92
    }
93
94
    /**
95
     * OAuth callback handler.
96
     *
97
     * @param SS_HTTPRequest $request
98
     */
99
    public function OAuth($request)
100
    {
101
        $denied = $request->getVar('denied');
102
        $token = $request->getVar('oauth_token');
103
        $verifier = $request->getVar('oauth_verifier');
104
105
        $sessionRequestToken = $this->twitterService->getSessionOAuthToken();
106
107
        $this->twitterService->clearSessionOAuthToken();
108
109
        $form = $this->getEditForm();
110
111
        if (
112
            $denied ||
113
            !$token ||
114
            !$verifier ||
115
            !$sessionRequestToken ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $sessionRequestToken of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
116
            !array_key_exists('oauth_token', $sessionRequestToken) ||
117
            !array_key_exists('oauth_token_secret', $sessionRequestToken) ||
118
            $token !== $sessionRequestToken['oauth_token']
119
        ) {
120
            return $this->handleOAuthError($this->Link(), $form);
121
        }
122
123
        $accessToken = $this->twitterService->getAccessToken(
124
            $sessionRequestToken['oauth_token'],
125
            $sessionRequestToken['oauth_token_secret'],
126
            $verifier
127
        );
128
129
        if (!$accessToken) {
130
            return $this->handleOAuthError($this->Link(), $form);
131
        }
132
133
        $twitterAccount = TwitterAccount::get()
134
            ->filter(['Title' => $accessToken['screen_name']])
135
            ->first();
136
137
        if (!$twitterAccount) {
138
            return $this->handleOAuthError($this->Link(), $form);
139
        }
140
141
        $twitterAccount->setAccessToken($accessToken);
142
        $twitterAccount->write();
143
144
        $form->sessionMessage(_t('Twitter.MessageOAuthSuccess', 'Successfully authorised your account.'), 'good');
145
146
        return Controller::curr()->redirect($this->Link());
147
    }
148
}
149
150
class TwitterAdminRequestHandler extends GridFieldDetailForm_ItemRequest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
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...
151
{
152
    private static $allowed_actions = [
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 $allowed_actions 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...
153
        'edit',
154
        'view',
155
        'ItemEditForm'
156
    ];
157
158
    /**
159
     * @return Form
160
     */
161
    public function ItemEditForm()
162
    {
163
        $form = parent::ItemEditForm();
164
165
        $formActions = $form->Actions();
166
167
        if ($actions = $this->record->getCMSActions()) {
168
            foreach ($actions as $action) {
169
                $formActions->push($action);
170
            }
171
        }
172
173
        return $form;
174
    }
175
}
176