| 1 | package WebService::Avalara::AvaTax::Role::Dumper; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Dump and restore compiled SOAP clients for AvaTax | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 9 | our $VERSION = '0.020'; # VERSION | |||||||||||||||||||||||||||
| 9 | 9 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | #pod =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 12 | #pod | ||||||||||||||||||||||||||||
| 13 | #pod use Moo; | ||||||||||||||||||||||||||||
| 14 | #pod with 'WebService::Avalara::AvaTax::Role::Dumper'; | ||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||
| 16 | #pod =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||
| 18 | #pod This role handles the dumping of compiled SOAP client code to storage, | ||||||||||||||||||||||||||||
| 19 | #pod and then restoring it later to save time without having to recompile. | ||||||||||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||||||||||
| 21 | #pod =cut | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 9 | ▶ | use English '-no_match_vars'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 9 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 9 | ▶ | use Package::Stash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 9 | ▶ | use Path::Tiny 0.018; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 9 | ▶ | use Types::Path::Tiny qw(Dir Path); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 9 | ▶ | use Types::Standard qw(Bool Str); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 9 | ▶ | use XML::Compile::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 9 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | #pod =attr use_wss | ||||||||||||||||||||||||||||
| 33 | #pod | ||||||||||||||||||||||||||||
| 34 | #pod This role overrides the value of the | ||||||||||||||||||||||||||||
| 35 | #pod L<use_wss attribute from WebService::Avalara::AvaTax::Role::Connection|WebService::Avalara::AvaTax::Role::Connection/use_wss> | ||||||||||||||||||||||||||||
| 36 | #pod to false, since L<XML::Compile::WSS|XML::Compile::WSS> does not seem to | ||||||||||||||||||||||||||||
| 37 | #pod work cleanly with L<XML::Compile::Dumper|XML::Compile::Dumper>. | ||||||||||||||||||||||||||||
| 38 | #pod | ||||||||||||||||||||||||||||
| 39 | #pod =cut | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 1 | return { @_[ 2 .. $#_ ], use_wss => 0 }; | |||||||||||||||||||||||||||
| 43 | 9 | }; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #pod =cut | ||||||||||||||||||||||||||||
| 46 | #pod | ||||||||||||||||||||||||||||
| 47 | #pod =attr recompile | ||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||
| 49 | #pod A boolean value you can set at construction to signal that the generated | ||||||||||||||||||||||||||||
| 50 | #pod class files for this service should be deleted and recompiled. | ||||||||||||||||||||||||||||
| 51 | #pod Defaults to false. | ||||||||||||||||||||||||||||
| 52 | #pod | ||||||||||||||||||||||||||||
| 53 | #pod =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 9 | has recompile => ( is => 'ro', isa => Bool, default => 0 ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #pod =attr dump_dir | ||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||
| 59 | #pod The directory in which to save and look for the generated class files. | ||||||||||||||||||||||||||||
| 60 | #pod Defaults to a temporary directory provided by | ||||||||||||||||||||||||||||
| 61 | #pod L<Path::Tiny::tempdir|Path::Tiny>. | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod =head1 CAVEATS | ||||||||||||||||||||||||||||
| 64 | #pod | ||||||||||||||||||||||||||||
| 65 | #pod The generated class files in the L</dump_dir> directory will be read and | ||||||||||||||||||||||||||||
| 66 | #pod executed, therefore it is B<critical> that this directory is in a secure | ||||||||||||||||||||||||||||
| 67 | #pod location on the file system that cannot be written to by untrusted users | ||||||||||||||||||||||||||||
| 68 | #pod or processes! | ||||||||||||||||||||||||||||
| 69 | #pod | ||||||||||||||||||||||||||||
| 70 | #pod =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 9 | has dump_dir => ( is => 'lazy', isa => Dir, coerce => 1 ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | sub _build_dump_dir { | ||||||||||||||||||||||||||
|
Subroutine
_build_dump_dir: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my $dump_dir = Path::Tiny->tempdir( TEMPLATE => 'AvalaraDumpXXXXX' ); | |||||||||||||||||||||||||||
| 77 | 0 | unshift @INC => "$dump_dir"; | |||||||||||||||||||||||||||
| 78 | 0 | return $dump_dir; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #pod =attr dump_file_name | ||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||
| 83 | #pod The path to the file in L</dump_dir> used to save/read the generated classes. | ||||||||||||||||||||||||||||
| 84 | #pod Defaults to F<< lib/I<package-name-converted-to-path.pm> >>. | ||||||||||||||||||||||||||||
| 85 | #pod | ||||||||||||||||||||||||||||
| 86 | #pod =cut | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 9 | has dump_file_name => ( is => 'lazy', isa => Path, coerce => 1 ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub _build_dump_file_name { | ||||||||||||||||||||||||||
|
Subroutine
_build_dump_file_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 92 | 0 | my $dump_dir = $self->dump_dir; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $dump_file = $self->_package_name; | |||||||||||||||||||||||||||
| 95 | 0 | $dump_file =~ s/ :: /\//xmsg; | |||||||||||||||||||||||||||
| 96 | 0 | $dump_file = Path::Tiny->new("lib/$dump_file.pm"); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $dump_dir->child($dump_file)->parent->mkpath; | |||||||||||||||||||||||||||
| 99 | 0 | return $dump_file; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | has _package_name => ( | ||||||||||||||||||||||||||||
| 103 | is => 'lazy', | ||||||||||||||||||||||||||||
| 104 | isa => Str, | ||||||||||||||||||||||||||||
| 105 | 0 | ▶ | default => sub { ref( $_[0] ) . q{::} . $_[0]->service }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 9 | ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | #pod =attr clients | ||||||||||||||||||||||||||||
| 109 | #pod | ||||||||||||||||||||||||||||
| 110 | #pod This role wraps around the builder for the C<clients> attribute to either read | ||||||||||||||||||||||||||||
| 111 | #pod in the generated class file (if it already exists) or generate it and then | ||||||||||||||||||||||||||||
| 112 | #pod add it to the symbol table. | ||||||||||||||||||||||||||||
| 113 | #pod | ||||||||||||||||||||||||||||
| 114 | #pod =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | around _build_clients => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 117 | 0 | my ( $orig, $self, @params ) = @_; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $package = $self->_package_name; | |||||||||||||||||||||||||||
| 120 | 0 | my $path = $self->dump_dir->child( $self->dump_file_name ); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ( $path->is_file and not $self->recompile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$path->is_file and not $self->recompile
MC/DC0%0/2
$path->is_file and not $self->recompile
$path->is_file
not $self->recompile
Truth table0%0/3
A and not(B)
A= $path->is_fileB= not $self->recompile
| |||||||||||||||||||||||||||||
| 123 | 0 | require "$path"; ## no critic (Modules::RequireBarewordIncludes) | |||||||||||||||||||||||||||
| 124 | 0 | my $stash = Package::Stash->new($package); | |||||||||||||||||||||||||||
| 125 | 0 | return { map { ( $_ => $stash->get_symbol("&$_") ) } | |||||||||||||||||||||||||||
| 126 | $stash->list_all_symbols('CODE') }; | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my %clients = %{ $self->$orig(@params) }; | |||||||||||||||||||||||||||
| 130 | 0 | $self->_write_dump_file( "$path", $package, %clients ); | |||||||||||||||||||||||||||
| 131 | 0 | return \%clients; | |||||||||||||||||||||||||||
| 132 | 9 | }; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub _write_dump_file { | ||||||||||||||||||||||||||
|
Subroutine
_write_dump_file: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 135 | 0 | my ( $self, $path, $package, %clients ) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 0 | ▶ | my $dumper = XML::Compile::Dumper->new( | ||||||||||||||||||||||||||
|
Condition0%0/3
$package || $self->_package_name
MC/DC0%0/2
$package || $self->_package_name
$package
$self->_package_name
Truth table0%0/3
A || B
A= $packageB= $self->_package_name
| |||||||||||||||||||||||||||||
| 138 | package => ( $package || $self->_package_name ), | ||||||||||||||||||||||||||||
| 139 | filename => "$path", | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | my $dump_fh = $dumper->file; | |||||||||||||||||||||||||||
| 143 | 0 | $dump_fh->print(<<'END_PERL'); | |||||||||||||||||||||||||||
| 144 | use URI::https; | ||||||||||||||||||||||||||||
| 145 | use LWPx::UserAgent::Cached; | ||||||||||||||||||||||||||||
| 146 | use HTTP::Config; | ||||||||||||||||||||||||||||
| 147 | use Mozilla::CA; | ||||||||||||||||||||||||||||
| 148 | use XML::Compile::WSDL11; | ||||||||||||||||||||||||||||
| 149 | use XML::Compile::SOAP11; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | BEGIN { $ENV{HTTPS_CA_FILE} = Mozilla::CA::SSL_ca_file() } | ||||||||||||||||||||||||||||
| 152 | END_PERL | ||||||||||||||||||||||||||||
| 153 | 0 | $dump_fh->print( 'use ' . ref($self) . ";\n" ); | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | $dumper->freeze(%clients); | |||||||||||||||||||||||||||
| 156 | 0 | $dumper->close; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | local @ARGV = ("$path"); | |||||||||||||||||||||||||||
| 159 | 0 | local $INPLACE_EDIT = '.bak'; | |||||||||||||||||||||||||||
| 160 | 0 | ▶ | while (<>) { / weaken [(] [\$] s [)] /xms or print } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 161 | 0 | Path::Tiny->new("$path$INPLACE_EDIT")->remove; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 9 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||