Completed
Push — develop ( b91125...52c290 )
by Seth
03:00
created

CalendarContext   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 0
dl 0
loc 106
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 44 7
A getCanonicalUrl() 0 4 1
A getContext() 0 4 1
A getId() 0 4 1
A getVerificationUrl() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace smtech\CanvasICSSync\SyncIntoCanvas;
4
5
class CalendarContext
6
{
7
    /**
8
     * The canonical URL for this context in Canvas
9
     * @var string
10
     */
11
    protected $canonicalUrl;
12
13
    /**
14
     * The context for this calendar in Canvas (user, group, course)
15
     * @var CanvasContext
16
     */
17
    protected $context;
18
19
    /**
20
     * Unique ID for this Canvas context
21
     * @var int
22
     */
23
    protected $id;
24
25
    /**
26
     * URL to verify this context against API
27
     * @var string
28
     */
29
    protected $verificationUrl;
30
31
    /**
32
     * Compute the calendar context for the canvas object based on its URL
33
     *
34
     * @param string $canvasUrl URL to the context for a calendar on this
35
     *     Canvas instance
36
     * @throws Exception If `$canvasInstance` is not a URL on this Canvas
37
     *     instance
38
     * @throws Exception if `$canvasInstance` is not a URL to a recognizable
39
     *     calendar context
40
     */
41
    public function __construct($canvasUrl)
0 ignored issues
show
Coding Style introduced by
__construct 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...
42
    {
43
        /*
44
         * TODO: accept calendar2?contexts links too (they would be an intuitively obvious link to use, after all)
45
         */
46
        /*
47
         * FIXME: users aren't working
48
         */
49
        /*
50
         * TODO: it would probably be better to look up users by email address than URL
51
         */
52
        /* get the context (user, course or group) for the canvas URL */
53
        if (preg_match('%(https?://)?(' . parse_url($_SESSION[Constants::CANVAS_INSTANCE_URL], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) {
54
            $this->canonicalUrl = "https://{$match[2]}"; // https://stmarksschool.instructure.com/courses/953
55
56
            // course or account groups
57
            if (isset($match[9]) || isset($match[11])) {
58
                $this->context = CanvasContext::GROUP(); // used for context_code in events
59
                $this->id = ($match[9] > $match[11] ? $match[9] : $match[11]);
0 ignored issues
show
Documentation Bug introduced by
It seems like $match[9] > $match[11] ? $match[9] : $match[11] can also be of type string. However, the property $id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60
                $this->verificationUrl = "groups/{$this->id}"; // used once to look up the object to be sure it really exists
61
62
            // courses
63
            } elseif (isset($match[7])) {
64
                $this->context = CanvasContext::COURSE();
65
                $this->id = $match[7];
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $match[7] is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
66
                $this->verificationUrl = "courses/{$this->id}";
67
68
            // users
69
            } elseif (isset($match[5])) {
70
                $this->context = CanvasContext::USER();
71
                $this->id = $match[5];
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type integer, but $match[5] is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
72
                $this->verificationUrl = "users/{$this->id}/profile";
73
74
            // we're somewhere where we don't know where we are
75
            } else {
76
                throw new Exception(
77
                    "'$canvasUrl' is not a recognizable calendar context"
78
                );
79
            }
80
        }
81
        throw new Exception(
82
            "'$canvasUrl' is not recognized as a URL to a calendar context on this Canvas instance"
83
        );
84
    }
85
86
    public function getCanonicalUrl()
87
    {
88
        return $this->canonicalUrl;
89
    }
90
91
    public function getContext()
92
    {
93
        return $this->context;
94
    }
95
96
    public function getId()
97
    {
98
        return $this->id;
99
    }
100
101
    public function getVerificationUrl()
102
    {
103
        return $this->verificationUrl;
104
    }
105
106
    public function __toString()
107
    {
108
        return $this->getCanonicalUrl();
109
    }
110
}
111