File Coverage

blib/lib/App/bif/new/identity.pm
Criterion Covered Total %
statement 58 58 100.0
branch 4 4 100.0
condition 16 28 57.1
subroutine 9 9 100.0
pod 1 1 100.0
total 88 100 88.0


line stmt bran cond sub pod time code
1             package App::bif::new::identity;
2 41     41   8661 use strict;
  41         91  
  41         1525  
3 41     41   229 use warnings;
  41         92  
  41         1423  
4 41     41   225 use Bif::Mo;
  41         86  
  41         382  
5 41     41   239 use Config::Tiny;
  41         90  
  41         1232  
6 41     41   957 use DBIx::ThinSQL qw/bv/;
  41         27308  
  41         354  
7 41     41   30412 use IO::Prompt::Tiny qw/prompt/;
  41         21610  
  41         3459  
8 41     41   248 use Path::Tiny qw/path/;
  41         85  
  41         33499  
9              
10             our $VERSION = '0.1.5_6';
11             extends 'App::bif';
12              
13             sub run {
14 44     44 1 107 my $self = shift;
15 44         203 my $opts = $self->opts;
16 44         304 my $dbw = $self->dbw;
17              
18             $dbw->txn(
19             sub {
20 42     42   4397 my $start = time;
21 42         440 $self->stop_work(
22             stop => $start,
23             save => 1,
24             );
25              
26 42         95 my $name = '';
27 42         109 my $email = '';
28 42         96 my $spacer = '';
29              
30 42 100       230 if ( $opts->{self} ) {
31 41         16791 print "Creating \"self\" identity:\n";
32              
33 41         451 my $git_conf_file =
34             path( File::HomeDir->my_home, '.gitconfig' );
35 41   50     4062 my $git_conf = Config::Tiny->read($git_conf_file) || {};
36              
37 41   50     4125 $name = $git_conf->{user}->{name} || 'Your Name';
38 41   50     287 $email = $git_conf->{user}->{email} || 'your@email.adddr';
39              
40 41         239 $name =~ s/(^")|("$)//g;
41 41         229 $email =~ s/(^")|("$)//g;
42              
43 41         171 $spacer = ' ';
44             }
45              
46 42   50     370 $opts->{name} ||= prompt( $spacer . 'Name:', $name )
      66        
47             || return $self->err( 'NameRequired', 'name is required' );
48              
49 42         3137 my $short = join( '', $opts->{name} =~ m/(\b\w)/g );
50 42   50     450 $opts->{shortname} ||= prompt( $spacer . 'Short Name:', $short )
      33        
51             || return $self->err( 'NameRequired', 'shortname is required' );
52              
53 42   50     2975 $opts->{method} ||= prompt( $spacer . 'Contact Method:', 'email' )
      66        
54             || return $self->err( 'MethodRequired', 'method is required' );
55              
56             $opts->{value} ||=
57 42   50     2499 prompt( $spacer . 'Contact ' . ucfirst( $opts->{method} ) . ':',
      66        
58             $email )
59             || return $self->err( 'ValueRequired', 'value is required' );
60              
61 42   100     2427 $opts->{message} ||= '';
62 42         444 $opts->{id} = $dbw->nextval('nodes');
63 42         13814 $opts->{ecm_id} = $dbw->nextval('nodes');
64              
65 42 100       10988 if ( $opts->{self} ) {
66             $opts->{change_id} = $self->new_change(
67             author => $opts->{name},
68             author_contact => bv( $opts->{value}, DBI::SQL_VARCHAR ),
69             author_contact_method => $opts->{method},
70             author_shortname => $opts->{shortname},
71 41         324 );
72             }
73             else {
74 1         10 $opts->{change_id} = $self->new_change();
75             }
76              
77             $dbw->xdo(
78             insert_into => 'func_new_identity',
79             values => {
80             id => $opts->{id},
81             change_id => $opts->{change_id},
82             name => $opts->{name},
83             shortname => $opts->{shortname},
84             self => $opts->{self},
85             },
86 41         809 );
87              
88             $dbw->xdo(
89             insert_into => 'func_new_identity_contact_method',
90             values => {
91             change_id => $opts->{change_id},
92             id => $opts->{ecm_id},
93             identity_id => $opts->{id},
94             method => $opts->{method},
95 41         111544 mvalue => bv( $opts->{value}, DBI::SQL_VARCHAR ),
96             },
97             );
98              
99             $dbw->xdo(
100             insert_into => 'func_update_identity',
101             values => {
102             change_id => $opts->{change_id},
103             id => $opts->{id},
104             contact_id => $opts->{id},
105             default_contact_method_id => $opts->{ecm_id},
106             },
107 41         87619 );
108              
109             $self->start_work(
110             node_id => $opts->{id},
111 41         73178 start => $start,
112             start_comment => "new identity",
113             billable => 1,
114             );
115              
116 41         23038 $self->stop_work(
117             stop => time,
118             restore => 1,
119             );
120              
121             $self->record_work(
122             node_id => $opts->{id},
123             change_id => $opts->{change_id}
124 41         380 );
125              
126             $self->end_change(
127             id => $opts->{change_id},
128             message => $opts->{message},
129             action_format => "new identity %s "
130             . "$opts->{name} ($opts->{shortname})",
131             action_node_id_1 => $opts->{id},
132 41         15293 );
133             }
134 42         476 );
135              
136 41         7103 return $self->ok('NewIdentity');
137             }
138              
139             1;
140             __END__