OX/Meta/Middleware.pm

filter: Statementstmt 98.0% 51 / 52 Branchbran 100% 8 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 1 totaltotal 95.9% 71 / 74 CC 5 SCAR 6.3
CC
5
Cov
98%
__ANON__
4.0
__ANON__
2.0
resolve
2.0
CRAP 1.9
« OX/RouteBuilder/ControllerAction.pm ↑ Summary OX/Meta/Role/HasRouteBuilders.pm »
1 package OX::Meta::Middleware;
2 44 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 44 $OX::Meta::Middleware::AUTHORITY = 'cpan:STEVAN';
4 }
5 44 $OX::Meta::Middleware::VERSION = '0.14';
6 44 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 44 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 44 has middleware => (
10 is => 'ro',
11 isa => 'OX::Types::Middleware',
12 required => 1,
13 );
14  
15 has dependencies => (
16 is => 'ro',
17 isa => 'HashRef',
18 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 44 );
20  
21 44 has lifecycle => (
22 is => 'ro',
23 isa => 'Str',
24 );
25  
26 44 has condition => (
27 is => 'ro',
28 isa => 'CodeRef',
29 );
30  
31 has service => (
32 is => 'ro',
33 isa => 'Bread::Board::Service',
34 lazy => 1,
35 23 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
36 23 my $self = shift;
37  
38 23 my $mw = $self->middleware;
39 23 my $deps = $self->dependencies;
40  
41 23 ▶ my %common = (
Branch: 1 branch
BranchTrueFalseTotal
defined $self->lifecycle ? : 1 22 23
42 name => '__ANON__',
43 (defined $self->lifecycle
44 ? (lifecycle => $self->lifecycle)
45 : ()),
46 );
47 23 my $mw_service;
48 23 ▶ if (!ref($mw)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $mw) { } 8 15 23
elsif (builtin::blessed($mw)) { } 3 12 15
49 $mw_service = Bread::Board::BlockInjection->new(
50 %common,
51 12 ▶ block => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
52 12 my $s = shift;
53  
54 my $mw_obj = $mw->new(
55 12 map { $_ => $s->param($_) } $s->get_param_keys
56 );
57  
58 # this should just be sub { $mw_obj->wrap($_[0]) }
59 # (actually, this whole thing should just be a constructor
60 # injection) but wrap calls to_app, which calls prepare_app
61 # every time, which we'd like to avoid for singleton
62 # middleware. it is kind of gross though, would be nice to
63 # maybe push this back into plack
64 12 $mw_obj->prepare_app;
65 13 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 13 my $app = shift;
67 13 $mw_obj->{app} = $app;
68 13 ▶ sub { $mw_obj->call(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
69 12 };
70 },
71 8 dependencies => $deps,
72 );
73 }
74 elsif (blessed($mw)) {
75 3 $mw_service = Bread::Board::Literal->new(
76 %common,
77 value => $mw,
78 );
79 }
80 else {
81 $mw_service = Bread::Board::BlockInjection->new(
82 %common,
83 18 ▶ block => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 18 my $s = shift;
85 18 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
86 18 my $app = shift;
87 18 return $mw->($app, $s);
88 18 };
89 },
90 12 dependencies => $deps,
91 );
92 }
93  
94 23 return $mw_service;
95 },
96 44 );
97  
98 34 ▶sub resolve {
Subroutine resolve: called CC=2 SCAR=6.9
Pod undocumented
99 34 my $self = shift;
100 34 my ($container) = @_;
101  
102 34 my $mw_service = $self->service;
103 34 $mw_service->parent($container);
104 34 my $resolved_mw = $mw_service->get;
105 34 $mw_service->detach_from_parent;
106  
107 34 ▶ if (my $condition = $self->condition) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $condition = $self->condition) 6 28 34
108 6 require Plack::Middleware::Conditional;
109 6 my $builder = $resolved_mw;
110 6 ▶ $resolved_mw = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
111 Plack::Middleware::Conditional->new(
112 condition => $condition,
113 6 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 6 OX::Util::apply_middleware($_[0], $builder)
115 },
116 6 )->wrap($_[0]);
117 6 };
118 }
119  
120 34 return $resolved_mw;
121 }
122  
123 44 __PACKAGE__->meta->make_immutable;
124  
125 =for Pod::Coverage
126 resolve
127  
128 =cut
129  
130 44 1;
« OX/RouteBuilder/ControllerAction.pm ↑ Summary OX/Meta/Role/HasRouteBuilders.pm »