GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

YouTubeController::actionConnect()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 52
rs 8.9408
cc 4
eloc 27
nc 6
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Craft;
4
5
/**
6
 * YouTube Controller.
7
 *
8
 * @author    Bob Olde Hampsink <[email protected]>
9
 * @copyright Copyright (c) 2015, Itmundi
10
 * @license   MIT
11
 *
12
 * @link      http://github.com/boboldehampsink
13
 */
14
class YouTubeController extends BaseController
15
{
16
    /**
17
     * Connect.
18
     */
19
    public function actionConnect()
20
    {
21
        // Get referer
22
        $referer = craft()->httpSession->get('youtube.referer');
23
24
        // If not set, set it
25
        if (!$referer) {
26
            $referer = craft()->request->getUrlReferrer();
27
            craft()->httpSession->add('youtube.referer', $referer);
28
        }
29
30
        // Set YouTube OAuth options
31
        $options = array(
32
            'plugin' => 'youtube',
33
            'provider' => 'google',
34
            'scope' => array(
35
                'https://www.googleapis.com/auth/userinfo.profile',
36
                'https://www.googleapis.com/auth/userinfo.email',
37
                'https://www.googleapis.com/auth/youtube',
38
                'https://www.googleapis.com/auth/youtube.upload',
39
            ),
40
            'authorizationOptions' => array(
41
                'access_type' => 'offline',
42
                'approval_prompt' => 'force',
43
            ),
44
        );
45
46
        // Connect
47
        if ($response = craft()->oauth->connect($options)) {
48
            if ($response['success']) {
49
50
                // Get token
51
                $token = $response['token'];
52
53
                // Save token
54
                craft()->youTube_oauth->saveToken($token);
55
56
                // Session notice
57
                craft()->userSession->setNotice(Craft::t('Connected.'));
58
            } else {
59
                // Session notice
60
                craft()->userSession->setError(Craft::t($response['errorMsg']));
61
            }
62
        } else {
63
            // session error
64
            craft()->userSession->setError(Craft::t('Couldn’t connect'));
65
        }
66
67
        // Redirect
68
        craft()->httpSession->remove('youtube.referer');
69
        $this->redirect($referer);
70
    }
71
72
    /**
73
     * Disconnect.
74
     */
75
    public function actionDisconnect()
76
    {
77
        // Delete token
78
        craft()->youTube_oauth->deleteToken();
79
80
        // Set notice
81
        craft()->userSession->setNotice(Craft::t('Disconnected.'));
82
83
        // Redirect
84
        $redirect = craft()->request->getUrlReferrer();
85
        $this->redirect($redirect);
86
    }
87
}
88