File Coverage

blib/lib/App/iTan/Command/Import.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


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