Elive/Entity/Participant.pm

filter: Statementstmt 93.2% 110 / 118 Branchbran 77.7% 42 / 54 Conditioncond 60.6% 20 / 33 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 95.2% 20 / 21 Podpod 100% 4 / 4 totaltotal 80.5% 203 / 252 CC 39 SCAR 24.5
CC
39
Cov
84%
BUILDARGS
22.9
participant
13.1
stringify
8.2
CRAP 11.6
« Elive/Entity/Report.pm ↑ Summary Elive/Entity/Participants.pm »
1 package Elive::Entity::Participant;
2 23 ▶use warnings; use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 23 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use Mouse::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 23 extends 'Elive::Entity';
8  
9 23 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 23 ▶use Elive;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Elive::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Elive::Entity::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use Elive::Entity::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use Elive::Entity::InvitedGuest;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Elive::Entity::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Elive::Entity::Participant - A Single Meeting Participant
22  
23 =head1 DESCRIPTION
24  
25 This is a component of L<Elive::Entity::Participants>. It contains details
26 on a participating user, including their type and participation role.
27  
28 =head1 METHODS
29  
30 =cut
31  
32 23 __PACKAGE__->entity_name('Participant');
33  
34 23 has 'user' => (is => 'rw', isa => 'Elive::Entity::User',
35 documentation => 'User (type=0)',
36 coerce => 1,
37 );
38  
39 23 has 'group' => (is => 'rw', isa => 'Elive::Entity::Group',
40 documentation => 'Group of attendees (type=1)',
41 coerce => 1,
42 );
43 # for the benefit of createSession and updateSession commands
44 23 __PACKAGE__->_alias(participant => 'user' );
45  
46 23 has 'guest' => (is => 'rw', isa => 'Elive::Entity::InvitedGuest',
47 documentation => 'Guest (type=2)',
48 coerce => 1,
49 );
50 23 __PACKAGE__->_alias( invitedGuestId => 'guest' );
51  
52 23 has 'role' => (is => 'rw', isa => 'Elive::Entity::Role',
53 documentation => 'Role level of the meeting participant',
54 coerce => 1,
55 );
56  
57 23 has 'type' => (is => 'rw', isa => 'Int',
58 documentation => 'type of participant; 0:user, 1:group, 2:guest'
59 );
60  
61 23 our ( $TYPE_USER, $TYPE_GROUP, $TYPE_GUEST );
62 BEGIN {
63 23 ▶ $TYPE_USER = 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 23 $TYPE_GROUP = 1;
65 23 $TYPE_GUEST = 2;
66 }
67  
68 sub BUILDARGS {
69 554 ▶ my $class = shift;
Subroutine BUILDARGS: called CC=20 SCAR=31.3
Pod documented
70 554 local ($_) = shift;
71  
72 554 ▶ if (Scalar::Util::blessed($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_)) 354 200 554
73  
74 354 ▶ if (try {$_->isa('Elive::Entity::User')}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { $_->isa("Elive::Entity::User"); } )) 1 353 354
75 #
76 # coerce participant as regular user
77 #
78 return {
79 1 user => $_,
80 role => {roleId => ${Elive::Entity::Role::PARTICIPANT}},
81 type => $TYPE_USER,
82 }
83 }
84  
85 353 ▶ if (try {$_->isa('Elive::Entity::Group')}) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { $_->isa("Elive::Entity::Group"); } )) 1 352 353
86 #
87 # coerce to group of participants
88 #
89 return {
90 1 group => $_,
91 role => {roleId => ${Elive::Entity::Role::PARTICIPANT}},
92 type => $TYPE_GROUP,
93 }
94 }
95  
96 352 ▶ if (try {$_->isa('Elive::Entity::InvitedGuest')}) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { $_->isa("Elive::Entity::InvitedGuest"); } )) 0 352 352
97 #
98 # coerce to an invited guest
99 #
100 return {
101 0 guest => $_,
102 role => {roleId => ${Elive::Entity::Role::PARTICIPANT}},
103 type => $TYPE_GUEST,
104 }
105 }
106 }
107  
108 552 my $reftype = Elive::Util::_reftype($_);
109 552 ▶ if ($reftype eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype eq "HASH") 423 129 552
110 # already structured as a participant
111 423 my %spec = %$_;
112  
113 423 my $type;
114 423 ▶ if ($spec{user}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($spec{'user'}) { } 380 43 423
elsif ($spec{'group'}) { } 23 20 43
elsif ($spec{'guest'}) { } 20 0 20
115 380 ▶ $spec{type} ||= $TYPE_USER;
Condition33%1/3
$spec{"type"} ||= $Elive::Entity::Participant::TYPE_USER
l!l&&r!l&&!r
00380
MC/DC0%0/2
$spec{"type"} ||= $Elive::Entity::Participant::TYPE_USER
$spec{"type"} $Elive::Entity::Participant::TYPE_USER
Truth table33%1/3
A ||= B
A= $spec{"type"}B= $Elive::Entity::Participant::TYPE_USER
ABresult
1X1
011
000
116 }
117 elsif ($spec{group}) {
118 23 ▶ $spec{type} ||= $TYPE_GROUP;
Condition66%2/3
$spec{"type"} ||= $Elive::Entity::Participant::TYPE_GROUP
l!l&&r!l&&!r
2210
MC/DC0%0/2
$spec{"type"} ||= $Elive::Entity::Participant::TYPE_GROUP
$spec{"type"} $Elive::Entity::Participant::TYPE_GROUP
Truth table66%2/3
A ||= B
A= $spec{"type"}B= $Elive::Entity::Participant::TYPE_GROUP
ABresult
1X1
011
000
119 }
120 elsif ($spec{guest}) {
121 20 ▶ $spec{type} ||= $TYPE_GUEST;
Condition66%2/3
$spec{"type"} ||= $Elive::Entity::Participant::TYPE_GUEST
l!l&&r!l&&!r
1910
MC/DC0%0/2
$spec{"type"} ||= $Elive::Entity::Participant::TYPE_GUEST
$spec{"type"} $Elive::Entity::Participant::TYPE_GUEST
Truth table66%2/3
A ||= B
A= $spec{"type"}B= $Elive::Entity::Participant::TYPE_GUEST
ABresult
1X1
011
000
122 }
123  
124 423 ▶ return \%spec if defined $spec{type};
Branch: 1 branch
BranchTrueFalseTotal
if defined $spec{"type"} 423 0 423
125 }
126  
127 129 ▶ if ($reftype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reftype) 0 129 129
128 0 ▶ warn YAML::Syck::Dump({unbuildable_participant => $_})
Branch: 1 branch
BranchTrueFalseTotal
if "Elive"->debug 0 0 0
129 if Elive->debug;
130 0 die "unable to build participant from $reftype reference";
131 }
132  
133 #
134 # Simple users:
135 # 'bob=3' => user:bob, role:3, type: 0 (user)
136 # 'alice' => user:bob, role:3 (defaulted), type: 0 (user)
137 # A leading '*' indicates a group:
138 # '*mygroup=2' => group:mygroup, role:2 type:1 (group)
139 # Invited guests are of the form: displayName(loginName)
140 # 'Robert(bob)' => guest:{loginName:bob, displayName:Robert}
141 #
142 129 my %parse;
143  
144 129 ▶ if (m{^ \s*
Branch: 2 branches
BranchTrueFalseTotal
if (/^ \s* ([^;]*?) # display name \s* \( ([^;\)]+) \) # (login name) \s* (= (\d) \s*)? # possible '=role' (ignored) $/x) { } 3 126 129
elsif (m[^ \s* (\*?) # optional group indicator '*' \s* ([^,]*?) # user/group id \s* (= (\d) \s*)? # optional '=role' $]x) { } 126 0 126
145 ([^;]*?) # display name
146 \s*
147 \( ([^;\)]+) \) # (login name)
148 \s*
149 (= (\d) \s*)? # possible '=role' (ignored)
150 $}x) {
151  
152 3 $parse{guest} = {displayName => $1, loginName => $2};
153 3 $parse{type} = $TYPE_GUEST;
154  
155 3 return \%parse;
156 }
157 elsif (m{^ \s*
158 (\*?) # optional group indicator '*'
159 \s*
160 ([^,]*?) # user/group id
161 \s*
162 (= (\d) \s*)? # optional '=role'
163 $}x) {
164  
165 126 my $is_group = $1;
166 126 my $id = $2;
167 126 my $roleId = $4;
168  
169 126 ▶ $roleId = ${Elive::Entity::Role::PARTICIPANT}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $roleId and $roleId ne "" 67 59 126
Condition66%2/3
defined $roleId and $roleId ne ""
!ll&&!rl&&r
67059
MC/DC50%1/2
defined $roleId and $roleId ne ""
defined $roleId $roleId ne ""
Truth table66%2/3
A and B
A= defined $roleIdB= $roleId ne ""
ABresult
0X0
100
111
170 unless defined $roleId && $roleId ne '';
171  
172 126 ▶ if ( $is_group ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_group) { } 7 119 126
173 7 $parse{group} = {groupId => $id};
174 7 $parse{type} = $TYPE_GROUP;
175 }
176 else {
177 119 $parse{user} = {userId => $id};
178 119 $parse{type} = $TYPE_USER;
179 }
180  
181 126 $parse{role}{roleId} = $roleId;
182  
183 126 return \%parse;
184 }
185  
186 #
187 # slightly convoluted die on return to keep Perl::Critic happy
188 #
189 0 return die "participant '$_' not in format: userId=[0-3] or *groupId=[0-3] or guestName(guestLogin)";
190 }
191  
192 coerce 'Elive::Entity::Participant' => from 'Str'
193 0 ▶ => via { __PACKAGE__->new( $_) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
194  
195 =head2 participant
196  
197 Returns a participant. This can either be of type L<Elive::Entity::User> (type
198 0), L<Elive::Entity::Group> (type 1) or L<Elive::Entity::InvitedGuest> (type 2).
199  
200 =cut
201  
202 sub participant {
203 5 ▶ my ($self) = @_;
Subroutine participant: called CC=7 SCAR=25.7
Pod documented
204
205 return (!defined $self->type || $self->type == $TYPE_USER) ? $self->user
206 : ($self->type == $TYPE_GROUP) ? $self->group
207 : ($self->type == $TYPE_GUEST) ? $self->guest
208 5 ▶ : do {warn "unknown participant type: ".$self->type;
Branch: 3 branches
BranchTrueFalseTotal
!defined($self->type) || $self->type == $Elive::Entity::Participant::TYPE_USER ? : 2 3 5
$self->type == $Elive::Entity::Participant::TYPE_GROUP ? : 2 1 3
$self->type == $Elive::Entity::Participant::TYPE_GUEST ? : 1 0 1
Condition66%2/3
not defined $self->type or $self->type == $Elive::Entity::Participant::TYPE_USER
l!l&&r!l&&!r
023
MC/DC50%1/2
not defined $self->type or $self->type == $Elive::Entity::Participant::TYPE_USER
not defined $self->type $self->type == $Elive::Entity::Participant::TYPE_USER
Truth table66%2/3
not(A) or B
A= not defined $self->typeB= $self->type == $Elive::Entity::Participant::TYPE_USER
ABresult
1X1
011
000
209 0 ▶ $self->user || $self->group || $self->guest};
Branch: 1 branch
BranchTrueFalseTotal
$self->user or $self->group or $self->guest 0 0 0
Condition0%0/3
$self->user or $self->group
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->user or $self->group
$self->user $self->group
Truth table0%0/3
A or B
A= $self->userB= $self->group
ABresult
1X1
011
000
210 }
211  
212 =head2 is_moderator
213  
214 Utility method to examine or set a meeting participant's moderator privileges.
215  
216 # does Bob have moderator privileges?
217 my $is_moderator = $bob->is_moderator;
218  
219 # grant moderator privileges to Alice
220 $alice->is_moderator(1);
221  
222 =cut
223  
224 sub is_moderator {
225 9 ▶ my $self = shift;
Subroutine is_moderator: called CC=7 SCAR=19.6
Pod documented
226  
227 9 my $role_id;
228  
229 9 ▶ if (defined $self->type && $self->type == $TYPE_GUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->type and $self->type == $Elive::Entity::Participant::TYPE_GUEST) 1 8 9
Condition66%2/3
defined $self->type and $self->type == $Elive::Entity::Participant::TYPE_GUEST
!ll&&!rl&&r
081
MC/DC50%1/2
defined $self->type and $self->type == $Elive::Entity::Participant::TYPE_GUEST
defined $self->type $self->type == $Elive::Entity::Participant::TYPE_GUEST
Truth table66%2/3
A and B
A= defined $self->typeB= $self->type == $Elive::Entity::Participant::TYPE_GUEST
ABresult
0X0
100
111
230 #
231 # invited guests cannot be moderators
232 1 return;
233 }
234  
235 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 4 8
236 4 my $is_moderator = shift;
237 4 ▶ $role_id = $is_moderator
Branch: 1 branch
BranchTrueFalseTotal
$is_moderator ? : 2 2 4
238 ? ${Elive::Entity::Role::MODERATOR}
239 : ${Elive::Entity::Role::PARTICIPANT};
240  
241 4 ▶ $self->role( $role_id )
Branch: 1 branch
BranchTrueFalseTotal
unless $role_id == $self->role->stringify 4 0 4
242 unless $role_id == $self->role->stringify;
243 }
244 else {
245 4 $role_id = $self->role->stringify;
246 }
247  
248 8 ▶ return defined $role_id && $role_id != ${Elive::Entity::Role::PARTICIPANT};
Condition66%2/3
defined $role_id && $role_id != $Elive::Entity::Role::PARTICIPANT
!ll&&!rl&&r
044
MC/DC50%1/2
defined $role_id && $role_id != $Elive::Entity::Role::PARTICIPANT
defined $role_id $role_id != $Elive::Entity::Role::PARTICIPANT
Truth table66%2/3
A && B
A= defined $role_idB= $role_id != $Elive::Entity::Role::PARTICIPANT
ABresult
0X0
100
111
249 }
250  
251 =head2 stringify
252  
253 Returns a string of the form 'userId=role' (users) '*groupId=role (groups),
254 or displayName(loginName) (guests). This value is used for comparisons,
255 display, etc...
256  
257 =cut
258  
259 sub stringify {
260 352 ▶ my $self = shift;
Subroutine stringify: called CC=8 SCAR=21.0
Pod documented
261 352 ▶ my $data = shift || $self;
Condition66%2/3
shift() || $self
l!l&&r!l&&!r
304480
MC/DC0%0/2
shift() || $self
shift() $self
Truth table66%2/3
A || B
A= shift()B= $self
ABresult
1X1
011
000
262  
263 352 $data = $self->BUILDARGS($data);
264 352 my $role_id = Elive::Entity::Role->stringify( $data->{role} );
265  
266 352 ▶ if (!defined $data->{type} || $data->{type} == $TYPE_USER) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $data->{'type'} or $data->{'type'} == $Elive::Entity::Participant::TYPE_USER) { } 316 36 352
elsif ($data->{'type'} == $Elive::Entity::Participant::TYPE_GUEST) { } 15 21 36
elsif ($data->{'type'} == $Elive::Entity::Participant::TYPE_GROUP) { } 21 0 21
Condition66%2/3
not defined $data->{"type"} or $data->{"type"} == $Elive::Entity::Participant::TYPE_USER
l!l&&r!l&&!r
031636
MC/DC50%1/2
not defined $data->{"type"} or $data->{"type"} == $Elive::Entity::Participant::TYPE_USER
not defined $data->{"type"} $data->{"type"} == $Elive::Entity::Participant::TYPE_USER
Truth table66%2/3
not(A) or B
A= not defined $data->{"type"}B= $data->{"type"} == $Elive::Entity::Participant::TYPE_USER
ABresult
1X1
011
000
267 # user => 'userId'
268 316 return Elive::Entity::User->stringify($data->{user}).'='.$role_id;
269 }
270 elsif ($data->{type} == $TYPE_GUEST) {
271 # guest => 'displayName(loginName)'
272 15 my $guest_str = Elive::Entity::InvitedGuest->stringify($data->{guest});
273  
274 15 ▶ Carp::carp ("ignoring moderator role for invited guest: $guest_str")
Branch: 1 branch
BranchTrueFalseTotal
if defined $role_id and $role_id != $Elive::Entity::Role::PARTICIPANT 1 14 15
Condition100%3/3
defined $role_id and $role_id != $Elive::Entity::Role::PARTICIPANT
!ll&&!rl&&r
591
MC/DC100%2/2
defined $role_id and $role_id != $Elive::Entity::Role::PARTICIPANT
defined $role_id $role_id != $Elive::Entity::Role::PARTICIPANT
Truth table100%3/3
A and B
A= defined $role_idB= $role_id != $Elive::Entity::Role::PARTICIPANT
ABresult
0X0
100
111
275 if defined $role_id && $role_id != ${Elive::Entity::Role::PARTICIPANT};
276  
277 15 return $guest_str;
278 }
279 elsif ($data->{type} == $TYPE_GROUP) {
280 # group => '*groupId'
281 21 return Elive::Entity::Group->stringify($data->{group}).'='.$role_id;
282 }
283 else {
284 # unknown
285 0 die "unrecognised participant type: $data->{type}";
286 }
287 }
288  
289 =head1 SEE ALSO
290  
291 L<Elive::Entity::ParticipantList>
292 L<Elive::Entity::Participants>
293 L<Elive::Entity::User>
294 L<Elive::Entity::Group>
295 L<Elive::Entity::InvitedGuest>;
296 L<Elive::Entity::Role>
297  
298 =cut
299  
300 23 1;
« Elive/Entity/Report.pm ↑ Summary Elive/Entity/Participants.pm »