Passed
Push — master ( 1c618d...c1c6b0 )
by Yannick
10:36 queued 02:52
created

LtiResourceLink::validateItemData()   F

Complexity

Conditions 31
Paths 6080

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 45
rs 0
c 0
b 0
f 0
cc 31
nc 6080
nop 1

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 Chamilo\CoreBundle\Entity\Course;
5
use Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool;
6
7
/**
8
 * Class LtiContentItem.
9
 */
10
class LtiResourceLink extends LtiContentItemType
11
{
12
    /**
13
     * @var string
14
     */
15
    private $url;
16
    /**
17
     * @var string
18
     */
19
    private $title;
20
    /**
21
     * @var string
22
     */
23
    private $text;
24
    /**
25
     * @var stdClass
26
     */
27
    private $icon;
28
    /**
29
     * @var stdClass
30
     */
31
    private $thumbnail;
32
    /**
33
     * @var stdClass
34
     */
35
    private $iframe;
36
    /**
37
     * @var array
38
     */
39
    private $custom;
40
    /**
41
     * @var stdClass
42
     */
43
    private $lineItem;
44
    /**
45
     * @var stdClass
46
     */
47
    private $available;
48
    /**
49
     * @var stdClass
50
     */
51
    private $submission;
52
53
    /**
54
     * @throws \Doctrine\ORM\OptimisticLockException
55
     *
56
     * @return ImsLtiTool
57
     */
58
    public function save(ImsLtiTool $baseTool, Course $course)
59
    {
60
        $newTool = $this->createTool($baseTool);
61
        $newTool->setActiveDeepLinking(false);
62
63
        $em = Database::getManager();
64
65
        $em->persist($newTool);
66
        $em->flush();
67
68
        ImsLtiPlugin::create()->addCourseTool($course, $newTool);
69
    }
70
71
    /**
72
     * @throws Exception
73
     */
74
    protected function validateItemData(stdClass $itemData)
75
    {
76
        $this->url = empty($itemData->url) ? '' : $itemData->url;
77
        $this->title = empty($itemData->title) ? '' : $itemData->title;
78
        $this->text = empty($itemData->text) ? '' : $itemData->text;
79
        $this->custom = empty($itemData->custom) || !is_array($itemData->custom) ? [] : (array) $itemData->custom;
80
81
        $this->icon = empty($itemData->icon) ? null : $itemData->icon;
82
83
        if ($this->icon
84
            && (empty($this->icon->url) || empty($this->icon->width) || empty($this->icon->height))
85
        ) {
86
            throw new Exception(sprintf("Icon properties are missing in data form content item: %s", print_r($itemData, true)));
87
        }
88
89
        $this->thumbnail = empty($itemData->thumbnail) ? null : $itemData->thumbnail;
90
91
        if ($this->thumbnail
92
            && (empty($this->thumbnail->url) || empty($this->thumbnail->width) || empty($this->thumbnail->height))
93
        ) {
94
            throw new Exception(sprintf("Thumbnail URL is missing in data form content item: %s", print_r($itemData, true)));
95
        }
96
97
        $this->iframe = empty($itemData->iframe) ? null : $itemData->iframe;
98
99
        if ($this->iframe && (empty($this->iframe->width) || empty($this->iframe->height))) {
100
            throw new Exception(sprintf("Iframe size is wrong in data form content item: %s", print_r($itemData, true)));
101
        }
102
103
        $this->lineItem = empty($itemData->lineItem) ? null : $itemData->lineItem;
104
105
        if ($this->lineItem && empty($this->lineItem->scoreMaximum)) {
106
            throw new Exception(sprintf("LineItem properties are missing in data form content item: %s", print_r($itemData, true)));
107
        }
108
109
        $this->available = empty($itemData->available) ? null : $itemData->available;
110
111
        if ($this->available && empty($this->available->startDateTime) && empty($this->available->endDateTime)) {
112
            throw new Exception(sprintf("LineItem properties are missing in data form content item: %s", print_r($itemData, true)));
113
        }
114
115
        $this->submission = empty($itemData->submission) ? null : $itemData->submission;
116
117
        if ($this->submission && empty($this->submission->startDateTime) && empty($this->submission->endDateTime)) {
118
            throw new Exception(sprintf("Submission properties are missing in data form content item: %s", print_r($itemData, true)));
119
        }
120
    }
121
122
    /**
123
     * @return ImsLtiTool
124
     */
125
    private function createTool(ImsLtiTool $baseTool)
126
    {
127
        $newTool = clone $baseTool;
128
        $newTool->setParent($baseTool);
129
130
        if (!empty($this->url)) {
131
            $newTool->setLaunchUrl($this->url);
132
        }
133
134
        if (!empty($this->title)) {
135
            $newTool->setName($this->title);
136
        }
137
138
        if (!empty($this->text)) {
139
            $newTool->setDescription($this->text);
140
        }
141
142
        if (!empty($this->custom)) {
143
            $newTool->setCustomParams(
144
                $newTool->encodeCustomParams($this->custom)
145
            );
146
        }
147
148
        return $newTool;
149
    }
150
}
151