Passed
Pull Request — master (#5629)
by Angel Fernando Quiroz
08:49
created

TinCanParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 51
c 1
b 0
f 0
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseLaunchUrl() 0 17 2
A parse() 0 30 4
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\PluginBundle\XApi\Parser;
8
9
use Chamilo\CoreBundle\Entity\XApiToolLaunch;
10
use Symfony\Component\DomCrawler\Crawler;
11
12
/**
13
 * Class TinCanParser.
14
 */
15
class TinCanParser extends PackageParser
16
{
17
    public function parse(): XApiToolLaunch
18
    {
19
        $content = file_get_contents($this->filePath);
20
21
        $xml = new Crawler($content);
22
23
        $activityNode = $xml->filter('tincan activities activity')->first();
24
        $nodeName = $activityNode->filter('name');
25
        $nodeDescription = $activityNode->filter('description');
26
        $nodeLaunch = $activityNode->filter('launch');
27
28
        $toolLaunch = new XApiToolLaunch();
29
        $toolLaunch
30
            ->setCourse($this->course)
31
            ->setSession($this->session)
32
            ->setCreatedAt(api_get_utc_datetime(null, false, true))
33
            ->setActivityId($activityNode->attr('id'))
34
            ->setActivityType($activityNode->attr('type'))
35
            ->setLaunchUrl($this->parseLaunchUrl($nodeLaunch))
36
        ;
37
38
        if ($nodeName) {
39
            $toolLaunch->setTitle($nodeName->text());
40
        }
41
42
        if ($nodeDescription) {
43
            $toolLaunch->setDescription($nodeDescription->text() ?: null);
44
        }
45
46
        return $toolLaunch;
47
    }
48
49
    private function parseLaunchUrl(Crawler $launchNode): string
50
    {
51
        $launchUrl = $launchNode->text();
52
53
        $urlInfo = parse_url($launchUrl);
54
55
        if (empty($urlInfo['scheme'])) {
56
            $baseUrl = str_replace(
57
                api_get_path(SYS_COURSE_PATH),
0 ignored issues
show
Bug introduced by
The constant Chamilo\PluginBundle\XApi\Parser\SYS_COURSE_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
58
                api_get_path(WEB_COURSE_PATH),
59
                \dirname($this->filePath)
60
            );
61
62
            return "$baseUrl/$launchUrl";
63
        }
64
65
        return $launchUrl;
66
    }
67
}
68