blib/lib/App/iTan/Command/Import.pm

filter: Statementstmt 28.2% 11 / 39 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 4 / 5 Podpod n/a totaltotal 23.4% 15 / 64 CC 12 SCAR 47.6
CC
12
Cov
19%
execute
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 117.2
↑ Summary blib/lib/App/iTan/Command/Get.pm »
1 # ================================================================
2 package App::iTan::Command::Import;
3 # ================================================================
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use 5.0100;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use MooseX::App::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 with qw(App::iTan::Utils);
10  
11 option 'file' => (
12 is => 'ro',
13 isa => 'Path::Class::File',
14 required => 1,
15 coerce => 1,
16 documentation => q[Path to a file containing the iTANs to be imported],
17 );
18  
19 option 'deletefile' => (
20 is => 'ro',
21 isa => 'Bool',
22 default => 0,
23 documentation => q[Delete import file after a successfull import],
24 );
25  
26 option 'overwrite' => (
27 is => 'ro',
28 isa => 'Bool',
29 default => 0,
30 documentation => q[Overwrite duplicate index numbers],
31 );
32  
33  
34 0 ▶sub execute {
Subroutine execute: not called CC=12 SCAR=50.5
35 0 my ( $self, $opts, $args ) = @_;
36  
37 0 ▶ my @itans = $self->file->slurp(chomp => 1)
Branch: 1 branch
BranchTrueFalseTotal
my(@itans) = $self->file->slurp("chomp", 1) or die 'Cannot read file ' . $self->file->stringify 0 0 0
38 or die ('Cannot read file '.$self->file->stringify);
39  
40 0 my $date = $self->_date();
41  
42 0 say "Start importing iTan ...";
43  
44 0 ▶ my $sth = $self->dbh->prepare("INSERT INTO itan (tindex,itan,imported,valid,used,memo) VALUES (?,?,'$date',1,NULL,NULL)")
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $self->dbh->prepare("INSERT INTO itan (tindex,itan,imported,valid,used,memo) VALUES (?,?,'${date}',1,NULL,NULL)") or die 'ERROR: Cannot prepare: ' . $self->dbh->errstr 0 0 0
45 or die "ERROR: Cannot prepare: " . $self->dbh->errstr();
46  
47 0 foreach my $line (@itans) {
48 0 my ($index,$tan);
49 0 ▶ unless ($line =~ m/^(?<index>\d{1,4})\D+(?<tan>\d{4,8})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(?<index>\d{1,4})\D+(?<tan>\d{4,8})$/) { } 0 0 0
50 0 say "... did not import '$line' (could not parse)";
51 } else {
52 0 $index = $+{index};
53 0 $tan = $self->crypt_string( $+{tan} );
54 0 ▶ if ($index eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($index eq 0) 0 0 0
55 0 my $nextindex = $self->dbh->selectrow_array("SELECT MAX(tindex) FROM itan WHERE valid = 1");
56 0 $nextindex ++;
57 0 $index = $nextindex;
58 }
59 0 eval {
60 0 $self->get($index);
61 };
62 0 ▶ if ($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif ($self->overwrite) { } 0 0 0
63 0 say "... import $index";
64 0 ▶ $sth->execute($index,$tan)
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($index, $tan) or die 'Cannot execute: ' . $sth->errstr 0 0 0
65 or die "Cannot execute: " . $sth->errstr();
66 } elsif ($self->overwrite) {
67 0 ▶ $self->dbh->do('UPDATE itan SET valid = 0 WHERE tindex = '.$index)
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh->do('UPDATE itan SET valid = 0 WHERE tindex = ' . $index) or die 'ERROR: Cannot execute: ' . $self->dbh->errstr 0 0 0
68 or die "ERROR: Cannot execute: " . $self->dbh->errstr();
69 0 say "... import $index (overwrite old index)";
70 0 ▶ $sth->execute($index,$tan)
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute($index, $tan) or die 'Cannot execute: ' . $sth->errstr 0 0 0
71 or die "Cannot execute: " . $sth->errstr();
72 } else {
73 0 say "... did not import $index (duplicate index)";
74 }
75 }
76 }
77 0 $sth->finish();
78  
79 0 ▶ if ($self->deletefile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->deletefile) 0 0 0
80 0 $self->file->remove();
81 }
82  
83 0 return;
84 }
85  
86 __PACKAGE__->meta->make_immutable;
87 1;
88  
89 =pod
90  
91 =encoding utf8
92  
93 =head1 NAME
94  
95 App::iTan::Command::Import - Imports a list of iTans into the database
96  
97 =head1 SYNOPSIS
98  
99 itan import --file IMPORT_FILE [--deletefile] [--overwrite]
100  
101 =head1 DESCRIPTION
102  
103 Imports a list of iTans into the database form a file with one iTAN per line.
104  
105 The file must contain two columns (separated by any non numeric characters).
106 The first column must be the index number. The second column must be the tan
107 number. If your online banking application does not use index numbers just set
108 the first column to zero.
109  
110 10 434167
111 11 937102
112 OR
113 0 320791
114 0 823602
115  
116 =head1 OPTIONS
117  
118 =head2 file
119  
120 Path to a file containing the iTANs to be imported.
121  
122 =head2 deletefile
123  
124 Delete import file after a successfull import
125  
126 =head2 overwrite
127  
128 Overwrite duplicate index numbers.
129  
130 Index numbers must be unique. Default behaviour is to skip duplicate iTan
131 indices. When this flag is enabled the duplicate iTans will be overwritten.
132  
133 =cut
↑ Summary blib/lib/App/iTan/Command/Get.pm »