Passed
Push — 1.11.x ( 080d0e...e78a37 )
by Angel Fernando Quiroz
10:16
created

Lti13Database::findDeployment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 10
1
<?php
2
/* For license terms, see /license.txt */
3
4
use ChamiloSession as Session;
5
use Packback\Lti1p3\Interfaces;
6
use Packback\Lti1p3\LtiDeployment;
7
use Packback\Lti1p3\LtiRegistration;
8
9
class Lti13Database implements Interfaces\Database
10
{
11
    public function findRegistrationByIssuer($iss, $clientId = null)
12
    {
13
        $ltiCustomers = $this->getLtiConnection();
14
        if (empty($ltiCustomers[$iss])) {
15
            return false;
16
        }
17
18
        if (!isset($clientId)) {
19
            $clientId = $ltiCustomers[$iss]['client_id'];
20
        }
21
22
        return LtiRegistration::new()
23
            ->setAuthLoginUrl($ltiCustomers[$iss]['auth_login_url'])
24
            ->setAuthTokenUrl($ltiCustomers[$iss]['auth_token_url'])
25
            ->setClientId($clientId)
26
            ->setKeySetUrl($ltiCustomers[$iss]['key_set_url'])
27
            ->setKid($ltiCustomers[$iss]['kid'])
28
            ->setIssuer($iss)
29
            ->setToolPrivateKey($this->getPrivateKey());
30
    }
31
32
    public function findDeployment($iss, $deploymentId, $clientId = null)
33
    {
34
        $issSession = Session::read('iss');
35
        if (!in_array($deploymentId, $issSession[$iss]['deployment'])) {
36
            return false;
37
        }
38
39
        return LtiDeployment::new()->setDeploymentId($deploymentId);
40
    }
41
42
    private function getLtiConnection(): array
43
    {
44
        $em = Database::getManager();
45
        $platforms = $em->getRepository('ChamiloPluginBundle:LtiProvider\Platform')->findAll();
46
47
        $ltiCustomers = [];
48
        foreach ($platforms as $platform) {
49
            $issuer = $platform->getIssuer();
50
            $ltiCustomers[$issuer] = [
51
                'client_id' => $platform->getClientId(),
52
                'auth_login_url' => $platform->getAuthLoginUrl(),
53
                'auth_token_url' => $platform->getAuthTokenUrl(),
54
                'key_set_url' => $platform->getKeySetUrl(),
55
                'kid' => $platform->getKid(),
56
                'deployment' => [$platform->getDeploymentId()],
57
            ];
58
        }
59
        Session::write('iss', $ltiCustomers);
60
61
        return $ltiCustomers;
62
    }
63
64
    private function getPrivateKey()
65
    {
66
        $privateKey = '';
67
        $platformKey = Database::getManager()
68
            ->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey')
69
            ->findOneBy([]);
70
        if ($platformKey) {
71
            $privateKey = $platformKey->getPrivateKey();
72
        }
73
74
        return $privateKey;
75
    }
76
}
77