Evernote::Authorize()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 3
nop 0
1
<?php
2
3
class Evernote
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
    /** @var  string */
6
    protected $token;
7
8
    /** @var  boolean */
9
    protected $sandbox;
10
11
    /** @var  boolean */
12
    protected $china;
13
14
    /** @var  string */
15
    protected $key;
16
17
    /** @var  string */
18
    protected $secret;
19
20
21
    /**
22
     * @param string|null $token
23
     */
24
    public function __construct($token = null)
25
    {
26
        $EvernoteSettings = EvernoteSettings::current_Evernote_settings();
27
        if(empty($EvernoteSettings)) {
28
            user_error(
29
                _t(
30
                    'NO_EVERNOTE_ACTIVE',
31
                    'No Evernote Settings Available. Please setup the Evernote configuration.'
32
                ),
33
                E_USER_ERROR
34
            );
35
        }
36
        $this->token = $token;
37
        $this->sandbox = boolval(($EvernoteSettings->Sandbox) ?: true);
38
        $this->china = boolval(($EvernoteSettings->China) ?: false);
39
        $this->key = ($EvernoteSettings->APIKey) ?: '';
40
        $this->secret = ($EvernoteSettings->APISecret) ?: '';
41
    }
42
43
    /**
44
     * @return null|string
45
     */
46
    public function Authorize()
47
    {
48
        $oauth_handler = new \Evernote\Auth\OauthHandler($this->sandbox, false, $this->china);
49
        try {
50
            $oauth_data = $oauth_handler->authorize($this->key, $this->secret, $this->getCallbackUrl());
51
            $this->token = $oauth_data['oauth_token'];
52
            $ret = $this->token;
53
54
        } catch (\Evernote\Exception\AuthorizationDeniedException $e) {
55
            //If the user decline the authorization, an exception is thrown.
56
            $ret = null;
57
        }
58
59
        return $ret;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    private function getCallbackUrl()
66
    {
67
        $thisUrl = Director::absoluteBaseURL().'evernote-auth/callback';
68
69
        return $thisUrl;
70
    }
71
72
    /**
73
     * @param $token
74
     * @return array
75
     */
76
    public function notebookList($token)
77
    {
78
        $client = new \Evernote\Client($token, $this->sandbox, null, null, $this->china);
79
80
        $notebooks = array();
0 ignored issues
show
Unused Code introduced by
$notebooks is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
82
        $notebooks = $client->listNotebooks();
83
        return $notebooks;
84
    }
85
86
}