Completed
Push — develop ( f2bd12...f57c6e )
by Seth
06:57
created

CanvasAPIviaLTI::onConfigure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 4
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
3
/**
4
 * A Tool Provider that can handle LTI requests
5
 *
6
 * @author Seth Battis <[email protected]>
7
 **/	
8
class CanvasAPIviaLTI extends LTI_Tool_Provider {
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...
9
	
10
	/**
11
	 * Handle launch requests, which start the application running
12
	 **/
13
	public function onLaunch() {
0 ignored issues
show
Coding Style introduced by
onLaunch uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
14
		global $metadata; // FIXME:0 grown-ups don't program like this issue:17
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
15
		global $sql; // FIXME:0 grown-ups don't program like this issue:17
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
16
				
17
		/* is this user in a role that can use this app? */
18
		if ($this->user->isAdmin()) {
19
			
20
			/* set up any needed session variables */
21
	        $_SESSION['consumer_key'] = $this->consumer->getKey();
22
	        $_SESSION['resource_id'] = $this->resource_link->getId();
23
	        $_SESSION['user_consumer_key'] = $this->user->getResourceLink()->getConsumer()->getKey();
24
	        $_SESSION['user_id'] = $this->user->getId();
25
	        $_SESSION['isStudent'] = $this->user->isLearner();
26
	        $_SESSION['isContentItem'] = FALSE;	   
27
			
28
			/* do we have an admin API access token? */
29
			$haveToken = true;
30
			if (empty($metadata['CANVAS_API_TOKEN'])) {
31
				
32
				/* ...if not, do we have a user API access token for this user? */
33
				$userToken = new UserAPIToken($_SESSION['user_consumer_key'], $_SESSION['user_id'], $sql);			
34
				if (empty($userToken->getToken())) {
35
					
36
					/* ...if this user has no token, let's start by getting one */
37
					$haveToken = false;
38
					$this->redirectURL = "{$metadata['APP_URL']}/lti/token_request.php?oauth=request";
39
				} else {
40
					
41
					/* ...but if the user does have a token, rock on! */
42
					$_SESSION['isUserToken'] = true;
43
					$_SESSION['apiToken'] = $userToken->getToken();
44
					//$_SESSION['apiUrl'] = $userToken->getAPIUrl();
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
				}
46
			} else {
47
				
48
				/* ...if we have an admin API token, rock on! */
49
				$_SESSION['isUserToken'] = false;
50
				$_SESSION['apiToken'] = $metadata['CANVAS_API_TOKEN'];
51
				//$_SESSION['apiUrl'] = $metadata['CANVAS_API_URL'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
			}
53
			$_SESSION['apiUrl'] = 'https://' . $this->user->getResourceLink()->settings['custom_canvas_api_domain'] . '/api/v1';
54
			
55
	        /* pass control off to the app */
56
	        if ($haveToken) {
57
		        $this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=launch";
58
	        }
59
60
		/* ...otherwise set an appropriate error message and fail */
61
		} else {
62
			$this->reason = 'Invalid role';
63
			$this->isOK = false;
64
		}
65
	}
66
	
67
	/**
68
	 * Handle errors created while processing the LTI request
69
	 **/
70
	public function onError() {
71
		global $metadata; // FIXME:0 grown-ups don't program like this issue:17
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
72
		
73
		$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=error&reason={$this->reason}";
74
	}
75
	
76
	/**
77
	 * Handle dashboard requests (coming in LTI v2.0, I guess)
78
	 **/
79
	public function onDashboard() {
80
		global $metadata; // FIXME:0 grown-ups don't program like this issue:17
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
81
		
82
		$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=dashboard";
83
	}
84
	
85
	/**
86
	 * Handle configure requests (coming in LTI v2.0, I guess)
87
	 **/
88
	public function onConfigure() {
89
		global $metadata; // FIXME:0 grown-ups don't program like this issue:17
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
90
		
91
		$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=configure";
92
	}
93
	
94
	/**
95
	 * Handle content-item requests (that is we're a tool provider that adds a button in the content editor)
96
	 **/
97
	public function onContentItem() {
98
		global $metadata; // FIXME:0 grown-ups don't program like this issue:17
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
99
		
100
		$this->redirectURL = "{$metadata['APP_URL']}/app.php?lti-request=content-item";
101
	}
102
}
103
104
/**
105
 * Exceptions thrown by CanvasAPIviaLTI
106
 *
107
 * @author Seth Battis <[email protected]>
108
 **/
109
class CanvasAPIviaLTI_Exception extends Exception {
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...
110
	const MISSING_SECRETS_FILE = 1;
111
	const INVALID_SECRETS_FILE = 2;
112
	const MYSQL_CONNECTION = 3;
113
	const LAUNCH_REQUEST = 4;
114
}
115
116
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...