blib/lib/Lemonldap/NG/Portal/Plugins/PublicNotifications.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 71 CC 6 SCAR 23.5
CC
6
Cov
0%
getPublicNotifs
20.0
init
12.0
BEGIN
2.0
CRAP 10.5
1 package Lemonldap::NG::Portal::Plugins::PublicNotifications;
2  
3 use strict;
4 use JSON;
5 use Mouse;
6 use Lemonldap::NG::Portal::Main::Constants qw(
7 PE_OK
8 PE_ERROR
9 );
10  
11 our $VERSION = '2.22.0';
12  
13 extends 'Lemonldap::NG::Portal::Main::Plugin';
14  
15 use constant beforeAuth => 'getPublicNotifs';
16  
17 has notifObject => (
18 is => 'ro',
19 lazy => 1,
20 default => sub {
21 $_[0]
22 ->p->loadedModules->{'Lemonldap::NG::Portal::Plugins::Notifications'};
23 }
24 );
25  
26 sub init {
27 my ($self) = @_;
28 unless ( defined $self->notifObject ) {
29 $self->logger->error("Notifications must be enabled");
30 return 0;
31 }
32 if ( $self->conf->{oldNotifFormat} ) {
33 $self->logger->error("Public notifications don't support XML format");
34 return 0;
35 }
36 return 1;
37 }
38  
39 sub getPublicNotifs {
40 my ( $self, $req ) = @_;
41  
42 # Here we reuse existing notification getter object:
43 # - $self->notifObject : Plugins::Notifications object
44 # - $self->notifObject->module : librarie depending on chosen format
45 # (Notifications::JSON or Notifications::XML)
46 # - $self->notifObject->module->notifObject : Common::Notification
47 # storage layer
48 my $errors =
49 $self->notifObject->module->notifObject->getNotifications("public-error");
50 my $warns =
51 $self->notifObject->module->notifObject->getNotifications("public-warn");
52 my $infos =
53 $self->notifObject->module->notifObject->getNotifications("public-info");
54  
55 my $public_errors = [ map { from_json( $errors->{$_} ) } keys %$errors ];
56 my $public_warns = [ map { from_json( $warns->{$_} ) } keys %$warns ];
57 my $public_infos = [ map { from_json( $infos->{$_} ) } keys %$infos ];
58  
59 if ( @$public_errors || @$public_warns || @$public_infos ) {
60 my $res = to_json( {
61 public_errors => $public_errors,
62 public_warns => $public_warns,
63 public_infos => $public_infos,
64 }
65 );
66 $req->env->{DISPLAY_PUBLIC_NOTIFICATIONS} = 1 if $res;
67 $req->data->{customScript} .= <<EOF if $res;
68 <script type="application/init">
69 {
70 "publicNotifications": $res
71 }
72 </script>
73 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/carousel.js?v=$self->{p}->cacheTag"></script>
74 EOF
75 }
76 return PE_OK;
77 }
78  
79 1;