| 1 | package Mojolicious::Plugin::Host; | ||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use Mojo::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 5 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 27 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 9 | 27 | my (undef, $app, $config) = @_; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 27 | my ($helper, $www) = _parse_and_validate_config($config); | |||||||||||||||||||||||||||
| 12 | 12 | my $sub; | |||||||||||||||||||||||||||
| 13 | 12 | ▶ | if (not defined $www) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 14 | 6 | ▶ | $sub = sub { $_[0]->req->url->to_abs->host }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | } elsif ($www eq 'always') { | ||||||||||||||||||||||||||||
| 16 | 4 | ▶ | $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 17 | 4 | my $host = $_[0]->req->url->to_abs->host; | |||||||||||||||||||||||||||
| 18 | 4 | ▶ | if (index($host, 'www.') != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 2 | $host = "www.$host"; | |||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | return $host; | |||||||||||||||||||||||||||
| 23 | 3 | }; | |||||||||||||||||||||||||||
| 24 | } elsif ($www eq 'never') { | ||||||||||||||||||||||||||||
| 25 | 4 | ▶ | $sub = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 4 | my $host = $_[0]->req->url->to_abs->host; | |||||||||||||||||||||||||||
| 27 | 4 | ▶ | if (index($host, 'www.') == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 2 | substr $host, 0, 4, ''; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 4 | return $host; | |||||||||||||||||||||||||||
| 32 | 3 | }; | |||||||||||||||||||||||||||
| 33 | } else { | ||||||||||||||||||||||||||||
| 34 | 0 | Carp::croak qq{unknown value provided for www: '$www'}; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | $app->helper($helper => $sub); | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 27 | ▶ | sub _parse_and_validate_config { | ||||||||||||||||||||||||||
|
Subroutine
_parse_and_validate_config: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 41 | 27 | my ($config) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 27 | my $helper; | |||||||||||||||||||||||||||
| 44 | 27 | ▶ | if (exists $config->{helper}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 10 | $helper = delete $config->{helper}; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 10 | my $ref = ref $helper; | |||||||||||||||||||||||||||
| 48 | 10 | ▶ | Carp::croak qq{helper must be a string, but was '$ref'} if $ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 8 | ▶ | Carp::croak 'helper must be non-empty' if not defined $helper or $helper eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $helper or $helper eq ""
MC/DC100%2/2
not defined $helper or $helper eq ""
not defined $helper
$helper eq ""
Truth table100%3/3
not(A) or B
A= not defined $helperB= $helper eq ""
| |||||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 17 | $helper = 'host'; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 23 | my $www; | |||||||||||||||||||||||||||
| 55 | 23 | ▶ | if (exists $config->{www}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 15 | $www = delete $config->{www}; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 15 | my $ref = ref $www; | |||||||||||||||||||||||||||
| 59 | 15 | ▶ | Carp::croak qq{www must be a string, but was '$ref'} if $ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 13 | ▶ | Carp::croak 'www must be non-empty' if not defined $www or $www eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $www or $www eq ""
MC/DC100%2/2
not defined $www or $www eq ""
not defined $www
$www eq ""
Truth table100%3/3
not(A) or B
A= not defined $wwwB= $www eq ""
| |||||||||||||||||||||||||||||
| 61 | Carp::croak qq{www must be either 'always' or 'never', but was '$www'} | ||||||||||||||||||||||||||||
| 62 | 11 | ▶ | unless grep { $www eq $_ } 'always', 'never'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 16 | ▶ | Carp::croak 'unknown keys/values: ' . Mojo::Util::dumper $config if %$config; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 12 | return $helper, $www; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | 1; | |||||||||||||||||||||||||||
| 71 | __END__ | ||||||||||||||||||||||||||||