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.

YouTube_OauthService::saveToken()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 27
rs 8.8571
ccs 8
cts 8
cp 1
cc 3
eloc 10
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Craft;
4
5
/**
6
 * YouTube OAuth Service.
7
 *
8
 * Handles OAuth logics
9
 *
10
 * @author    Bob Olde Hampsink <[email protected]>
11
 * @copyright Copyright (c) 2015, Itmundi
12
 * @license   MIT
13
 *
14
 * @link      http://github.com/boboldehampsink
15
 */
16
class YouTube_OauthService extends BaseApplicationComponent
17
{
18
    /**
19
     * Holds a reference to the plugin class.
20
     *
21
     * @var YouTubePlugin
22
     */
23
    protected $plugin;
24
25
    /**
26
     * Holds the plugin settings.
27
     *
28
     * @var array
29
     */
30
    protected $settings;
31
32
    /**
33
     * Initialize service.
34
     *
35
     * @codeCoverageIgnore
36
     */
37
    public function init()
38
    {
39
        // Get plugin
40
        $this->plugin = craft()->plugins->getPlugin('youtube');
41
42
        // Get plugin settings
43
        $this->settings = $this->plugin->getSettings();
44
    }
45
46
    /**
47
     * Get OAuth token.
48
     *
49
     * @return OAuth_TokenModel
50
     */
51 1
    public function getToken()
52
    {
53
        // Get tokenId
54 1
        $tokenId = $this->settings->tokenId;
55
56
        // Return token
57 1
        return craft()->oauth->getTokenById($tokenId);
58
    }
59
60
    /**
61
     * Delete OAuth token.
62
     */
63 1
    public function deleteToken()
64
    {
65
        // Get token
66 1
        $token = $this->getToken();
67
68
        // Delete token
69 1
        if ($token) {
70 1
            craft()->oauth->deleteToken($token);
71
        }
72
73
        // Save plugin settings
74 1
        return craft()->plugins->savePluginSettings($this->plugin, array('tokenId' => null));
75
    }
76
77
    /**
78
     * Save OAuth Token.
79
     */
80 1
    public function saveToken($token)
81
    {
82
        // Get existing token
83 1
        $existingToken = $this->getToken();
84
85
        // Do we have a valid token?
86
        // @codeCoverageIgnoreStart
87
        if (!$token) {
88
            $token = new Oauth_TokenModel();
89
        }
90
        // @codeCoverageIgnoreEnd
91
92
        // Do we have a valid existing token
93 1
        if (isset($existingToken)) {
94 1
            $token->id = $existingToken->id;
95
        }
96
97
        // Set provider and handle
98 1
        $token->providerHandle = 'google';
99 1
        $token->pluginHandle = 'youtube';
100
101
        // Save token
102 1
        craft()->oauth->saveToken($token);
103
104
        // Save plugin settings
105 1
        return craft()->plugins->savePluginSettings($this->plugin, array('tokenId' => $token->id));
106
    }
107
}
108