Passed
Push — master ( b0b4c3...57268f )
by Yannick
09:19 queued 13s
created

LtiAssignmentGradesService::getResource()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 45
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 27
c 0
b 0
f 0
nc 32
nop 2
dl 0
loc 45
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Symfony\Component\HttpFoundation\JsonResponse;
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
7
8
/**
9
 * Class LtiAssignmentGradesService.
10
 */
11
class LtiAssignmentGradesService extends LtiAdvantageService
12
{
13
    const AGS_NONE = 'none';
14
    const AGS_SIMPLE = 'simple';
15
    const AGS_FULL = 'full';
16
17
    const SCOPE_LINE_ITEM = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem';
18
    const SCOPE_LINE_ITEM_READ = 'https://purl.imsglobal.org/spec/lti-ags/scope/lineitem.readonly';
19
    const SCOPE_RESULT_READ = 'https://purl.imsglobal.org/spec/lti-ags/scope/result.readonly';
20
    const SCOPE_SCORE_WRITE = 'https://purl.imsglobal.org/spec/lti-ags/scope/score';
21
22
    const TYPE_LINE_ITEM_CONTAINER = 'application/vnd.ims.lis.v2.lineitemcontainer+json';
23
    const TYPE_LINE_ITEM = 'application/vnd.ims.lis.v2.lineitem+json';
24
    const TYPE_RESULT_CONTAINER = 'application/vnd.ims.lis.v2.resultcontainer+json';
25
    const TYPE_SCORE = 'application/vnd.ims.lis.v1.score+json';
26
27
    /**
28
     * @return array
29
     */
30
    public function getAllowedScopes()
31
    {
32
        $scopes = [
33
            self::SCOPE_LINE_ITEM_READ,
34
            self::SCOPE_RESULT_READ,
35
            self::SCOPE_SCORE_WRITE,
36
        ];
37
38
        $toolServices = $this->tool->getAdvantageServices();
39
40
        if (self::AGS_FULL === $toolServices['ags']) {
41
            $scopes[] = self::SCOPE_LINE_ITEM;
42
        }
43
44
        return $scopes;
45
    }
46
47
    /**
48
     * @throws \Doctrine\ORM\ORMException
49
     * @throws \Doctrine\ORM\OptimisticLockException
50
     * @throws \Doctrine\ORM\TransactionRequiredException
51
     *
52
     * @return LtiAdvantageServiceResource
53
     */
54
    public static function getResource(Request $request, JsonResponse $response)
55
    {
56
        $parts = explode('/', $request->getPathInfo());
57
        $parts = array_filter($parts);
58
59
        $resource = null;
60
61
        if (count($parts) === 2 && 'lineitems' === $parts[2]) {
62
            $resource = new LtiLineItemsResource(
63
                $request->query->get('t'),
64
                $parts[1]
65
            );
66
        }
67
68
        if (count($parts) === 3 && 'lineitems' === $parts[2]) {
69
            $resource = new LtiLineItemResource(
70
                $request->query->get('t'),
71
                $parts[1],
72
                $parts[3]
73
            );
74
        }
75
76
        if (isset($parts[4]) && 'results' === $parts[4]) {
77
            $resource = new LtiResultsResource(
78
                $request->query->get('t'),
79
                $parts[1],
80
                $parts[3]
81
            );
82
        }
83
84
        if (isset($parts[4]) && 'scores' === $parts[4]) {
85
            $resource = new LtiScoresResource(
86
                $request->query->get('t'),
87
                $parts[1],
88
                $parts[3]
89
            );
90
        }
91
92
        if (!$resource) {
93
            throw new NotFoundHttpException('Line item resource not found.');
94
        }
95
96
        return $resource
97
            ->setRequest($request)
98
            ->setResponse($response);
99
    }
100
101
    /**
102
     * @param int $contextId
103
     * @param int $toolId
104
     *
105
     * @return string
106
     */
107
    public static function getLineItemsUrl($contextId, $toolId, array $extraParams = [])
108
    {
109
        $base = api_get_path(WEB_PLUGIN_PATH).'ims_lti/ags2.php';
110
        $resource = str_replace(
111
            'context_id',
112
            $contextId,
113
            LtiLineItemsResource::URL_TEMPLATE
114
        );
115
        $params = array_merge($extraParams, ['t' => $toolId]);
116
        $query = http_build_query($params);
117
118
        return "$base$resource?$query";
119
    }
120
121
    /**
122
     * @param int $contextId
123
     * @param int $lineItemId
124
     * @param int $toolId
125
     *
126
     * @return string
127
     */
128
    public static function getLineItemUrl($contextId, $lineItemId, $toolId)
129
    {
130
        $base = api_get_path(WEB_PLUGIN_PATH).'ims_lti/ags2.php';
131
        $resource = str_replace(
132
            ['context_id', 'line_item_id'],
133
            [$contextId, $lineItemId],
134
            LtiLineItemResource::URL_TEMPLATE
135
        );
136
        $query = http_build_query(['t' => $toolId]);
137
138
        return "$base$resource?$query";
139
    }
140
141
    /**
142
     * @param int $contextId
143
     * @param int $lineItemId
144
     * @param int $toolId
145
     *
146
     * @return string
147
     */
148
    public static function getResultsUrl($contextId, $lineItemId, $toolId, array $extraParams = [])
149
    {
150
        $lineItemUrl = self::getLineItemUrl($contextId, $lineItemId, $toolId);
151
        $query = http_build_query($extraParams);
152
153
        return "$lineItemUrl/results?$query";
154
    }
155
}
156