Passed
Pull Request — master (#9)
by mouhsine
01:45
created

ClassroomApi::download()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
1
<?php
2
namespace mikemix\Wiziq\API;
3
4
use mikemix\Wiziq\API\Request;
5
use mikemix\Wiziq\Common\Api\ClassroomApiInterface;
6
use mikemix\Wiziq\Entity\Attendees;
7
use mikemix\Wiziq\Entity\Classroom;
8
use mikemix\Wiziq\Entity\PermaClassroom;
9
10
class ClassroomApi implements ClassroomApiInterface
11
{
12
    /** @var Gateway */
13
    protected $gateway;
14
15 7
    public function __construct(Gateway $requester)
16
    {
17 7
        $this->gateway = $requester;
18 7
    }
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 1
    public function create(Classroom $classroom)
24
    {
25 1
        $response = $this->gateway->sendRequest(new Request\Create($classroom))
26 1
            ->create[0]->class_details[0];
27
28
        return [
29 1
            'class_id'        => (int)$response->class_id,
30 1
            'recording_url'   => (string)$response->recording_url,
31 1
            'presenter_email' => (string)$response->presenter_list[0]->presenter[0]->presenter_email,
32 1
            'presenter_url'   => (string)$response->presenter_list[0]->presenter[0]->presenter_url,
33 1
        ];
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function modify($classroomId, Classroom $classroom)
40
    {
41
42 1
        $response = (boolean)$this->gateway->sendRequest(new Request\Modify($classroomId, $classroom))->modify["status"];
43
44 1
        return $response;
45
    }
46
     /**
47
     * {@inheritdoc}
48
     */
49
    public function download($classroomId, $recordingFormat = 'zip')
50
    {
51
52
        $response = $this->gateway->sendRequest(new Request\Download($classroomId, $recordingFormat))->download_recording;
0 ignored issues
show
Bug introduced by
The property download_recording does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
53
54
        return $response;
55
    }
56
    /**
57
     * {@inheritdoc}
58
     */
59 1
    public function cancel($classroomId)
60
    {
61 1
        $response = (boolean)$this->gateway->sendRequest(new Request\Cancel($classroomId))->cancel['status'];
62 1
        return $response;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function getAttendanceReport($classroomId)
69
    {
70 1
        $attendance = [];
71
72 1
        $response = $this->gateway->sendRequest(new Request\AttendanceReport($classroomId))->get_attendance_report;
0 ignored issues
show
Bug introduced by
The property get_attendance_report does not seem to exist in SimpleXMLElement.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73
74 1
        foreach($response->attendee_list[0] as $attendee) {
75
            $data = [
76 1
                    'entry_time'       => (string)$attendee->entry_time,
77 1
                    'exit_time'        => (string)$attendee->exit_time,
78 1
                    'attended_minutes' => (string)$attendee->attended_minutes,
79 1
                    ];
80 1
            if(isset($attendee['presenter']) and $attendee['presenter']==true) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
81 1
                $data['presenter_id'] = (int)$attendee->presenter_id;
82 1
            } else {
83 1
                $data['attendee_id'] = (int)$attendee->attendee_id;
84
            }
85
86 1
            $attendance[] = $data;
87 1
        }
88
89 1
        return $attendance;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 1
    public function createPermaClas(PermaClassroom $classroom)
96
    {
97 1
        $response = $this->gateway->sendRequest(new Request\CreatePermaClass($classroom));
98 1
        $details  = $response->create_perma_class[0]->perma_class_details[0];
99
100
        return [
101 1
            'class_id'        => (int)$details->class_master_id,
102 1
            'attendee_url'    => (string)$details->common_perma_attendee_url,
103 1
            'presenter_email' => (string)$details->presenter[0]->presenter_email,
104 1
            'presenter_url'   => (string)$details->presenter[0]->presenter_url,
105 1
        ];
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function addAttendeesToClass($classroomId, Attendees $attendees)
112
    {
113 1
        $response = $this->gateway->sendRequest(new Request\AddAttendees($classroomId, $attendees));
114 1
        $result   = [];
115
116 1
        foreach ($response->add_attendees[0]->attendee_list[0] as $attendee) {
117 1
            $result[] = ['id' => (int)$attendee->attendee_id, 'url' => (string)$attendee->attendee_url];
118 1
        }
119
120 1
        return $result;
121
    }
122
123
    /**
124
     * @codeCoverageIgnore
125
     * @param \SimpleXMLElement $response
126
     * @return array
127
     */
128
    protected function getPresentersFromResponse(\SimpleXMLElement $response)
129
    {
130
        $presenters = [];
131
        foreach ($response->presenter_list[0] as $presenter) {
132
            $presenters[] = [
133
                'email' => (string)$presenter->presenter_email,
134
                'url'   => (string)$presenter->presenter_url,
135
            ];
136
        }
137
138
        return $presenters;
139
    }
140
}
141