Completed
Push — develop ( 0fe9d6...4e1ddc )
by Seth
03:00
created

CalendarContext::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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?://)?(' .
54
            /*
55
             * FIXME not clear that we should be using $_SESSION
56
             */
57
            parse_url($_SESSION[CANVAS_INSTANCE_URL], PHP_URL_HOST) . '/((about/(\d+))|(courses/(\d+)(/groups/(\d+))?)|(accounts/\d+/groups/(\d+))))%', $canvasUrl, $match)) {
58
            $this->canonicalUrl = "https://{$match[2]}"; // https://stmarksschool.instructure.com/courses/953
59
60
            // course or account groups
61
            if (isset($match[9]) || isset($match[11])) {
62
                $this->context = CanvasContext::GROUP(); // used for context_code in events
63
                $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...
64
                $this->verificationUrl = "groups/{$this->id}"; // used once to look up the object to be sure it really exists
65
66
            // courses
67
            } elseif (isset($match[7])) {
68
                $this->context = CanvasContext::COURSE();
69
                $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...
70
                $this->verificationUrl = "courses/{$this->id}";
71
72
            // users
73
            } elseif (isset($match[5])) {
74
                $this->context = CanvasContext::USER();
75
                $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...
76
                $this->verificationUrl = "users/{$this->id}/profile";
77
78
            // we're somewhere where we don't know where we are
79
            } else {
80
                throw new Exception(
81
                    "'$canvasUrl' is not a recognizable calendar context"
82
                );
83
            }
84
        }
85
        throw new Exception(
86
            "'$canvasUrl' is not recognized as a URL to a calendar context on this Canvas instance"
87
        );
88
    }
89
90
    public function getCanonicalUrl()
91
    {
92
        return $this->canonicalUrl;
93
    }
94
95
    public function getContext()
96
    {
97
        return $this->context;
98
    }
99
100
    public function getId()
101
    {
102
        return $this->id;
103
    }
104
105
    public function getVerificationUrl()
106
    {
107
        return $this->verificationUrl;
108
    }
109
}
110