blib/lib/Mojolicious/Plugin/CachePurge.pm

filter: Statementstmt 34.8% 15 / 43 Branchbran 20.0% 2 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.0% 3 / 13 Podpod 100% 1 / 1 totaltotal 29.1% 21 / 72 CC 7 SCAR 15.5
CC
7
Cov
30%
_purge
12.0
register
6.4
__ANON__
6.0
CRAP 4.7
↑ Summary
1 package Mojolicious::Plugin::CachePurge;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.01';
6  
7 0 ▶has 'ua' => sub { Mojo::UserAgent->new; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
8  
9 1 ▶sub register {
Subroutine register: called CC=4 SCAR=18.5
Pod documented
10 1 my ( $self, $app, $conf ) = @_;
11  
12 1 ▶ if ( defined( $conf->{baseurl} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{'baseurl'}) { } 0 1 1
13 $app->log->error("CachePurge: Can not parse baseurl as url, aborting")
14 0 ▶ unless ( $self->{'baseurl'} = Mojo::URL->new( $conf->{'baseurl'} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"baseurl"} = "Mojo::URL"->new($conf->{"baseurl"}) 0 0 0
15 }
16 else {
17 1 $app->log->error("CachePurge: No baseurl configured, aborting");
18 }
19  
20 1 ▶ if ( $self->{'baseurl'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'baseurl'}) { } 0 1 1
21 0 $self->_add_log_hooks($app);
22 0 ▶ $app->helper( 'cache_purge' => sub { $self->_purge(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 }
24 else {
25 $app->helper( 'cache_purge' =>
26 0 ▶ sub { $app->log->debug('CachePurge: inactive, config error') } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
27 }
28  
29 1 return $self;
30 }
31  
32 0 ▶sub _add_log_hooks {
Subroutine _add_log_hooks: not called CC=1 SCAR=6.9
33 0 my ( $self, $app ) = @_;
34  
35 $self->ua->on(
36 0 ▶ start => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 my ( $ua, $tx ) = @_;
38 0 my $msg =
39 "CachePurge: request, method="
40 . $tx->req->method
41 . ", url="
42 . $tx->req->url;
43 0 $app->log->debug($msg);
44 },
45 0 ▶ error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 my ( $ua, $err ) = @_;
47 0 $app->log->error( "CachePurge: " . $err );
48 }
49 0 );
50  
51 }
52  
53 0 ▶sub _purge {
Subroutine _purge: not called CC=3 SCAR=24.8
54 0 my ( $self, $c, $args, $cb ) = @_;
55  
56 # We may get a callback, but no args. It will be here as $args, so
57 # we move it.
58 0 ▶ if (ref $args eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "CODE") 0 0 0
59 0 $cb = $args;
60 0 undef $args;
61 }
62  
63 0 ▶ my $purge_path = $args->{'path'} ||= $c->req->url->path;
Condition0%0/3
$args->{"path"} ||= $c->req->url->path
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"path"} ||= $c->req->url->path
$args->{"path"} $c->req->url->path
Truth table0%0/3
A ||= B
A= $args->{"path"}B= $c->req->url->path
ABresult
1X1
011
000
64  
65 0 my $purge_url = Mojo::URL->new( $self->{'baseurl'} )->path($purge_path);
66  
67 Mojo::IOLoop->delay(
68 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my ($delay) = @_;
70 0 my $tx = $self->ua->build_tx( PURGE => $purge_url );
71 0 $self->ua->start( $tx => $delay->begin );
72 },
73  
74 # log response
75 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my ( $delay, $tx ) = @_;
77 0 $c->app->log->debug( "CachePurge: response, url="
78 . $tx->req->url
79 . ", code="
80 . $tx->res->code );
81 0 $delay->pass($tx);
82 },
83  
84 # handle callback, if any
85 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
86 0 my ( $delay, $tx ) = @_;
87 0 ▶ $self->$cb($tx) if ($cb);
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
88 }
89 0 );
90 }
91  
92 1 1;
93 __END__
↑ Summary