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

filter: Statementstmt 0.0% 0 / 58 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 92 CC 8 SCAR 23.8
CC
8
Cov
0%
getPublicNotifs
20.0
init
12.0
_extractNotification
12.0
CRAP 10.8
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.23.3';
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 =
56 [ map { $self->_extractNotification( $errors->{$_} ) } keys %$errors ];
57 my $public_warns =
58 [ map { $self->_extractNotification( $warns->{$_} ) } keys %$warns ];
59 my $public_infos =
60 [ map { $self->_extractNotification( $infos->{$_} ) } keys %$infos ];
61  
62 if ( @$public_errors || @$public_warns || @$public_infos ) {
63 my $res = to_json( {
64 public_errors => $public_errors,
65 public_warns => $public_warns,
66 public_infos => $public_infos,
67 }
68 );
69 my $cacheTag = $self->p->cacheTag;
70 $req->env->{DISPLAY_PUBLIC_NOTIFICATIONS} = 1 if $res;
71 $req->data->{customScript} .= <<EOF if $res;
72 <script type="application/init">
73 {
74 "publicNotifications": $res
75 }
76 </script>
77 <script type="text/javascript" src="$self->{p}->{staticPrefix}/common/js/carousel.min.js?v=$cacheTag"></script>
78 EOF
79 }
80 return PE_OK;
81 }
82  
83 sub _extractNotification {
84 my ( $self, $notif ) = @_;
85 $notif = from_json($notif);
86 $notif = $notif->[0] if ( ref($notif) eq 'ARRAY' );
87 if ( my $content = $notif->{xml} ) {
88 $self->logger->debug("Notification content: $content");
89 $content = from_json($content);
90 delete $notif->{xml};
91 return { %$notif, %$content };
92 }
93 else { return $notif; }
94 }
95  
96 1;