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 50     50   5914 use strict;
  50         92  
  50         1801  
3 50     50   222 use warnings;
  50         78  
  50         1657  
4 50     50   207 use Bif::Mo;
  50         78  
  50         398  
5 50     50   257 use Config::Tiny;
  50         79  
  50         1378  
6 50     50   656 use DBIx::ThinSQL qw/bv/;
  50         18895  
  50         389  
7 50     50   27399 use IO::Prompt::Tiny qw/prompt/;
  50         24002  
  50         4425  
8 50     50   311 use Path::Tiny qw/path/;
  50         77  
  50         33983  
9              
10             our $VERSION = '0.1.5_5';
11             extends 'App::bif';
12              
13             sub run {
14 53     53 1 99 my $self = shift;
15 53         155 my $opts = $self->opts;
16 53         301 my $dbw = $self->dbw;
17              
18             $dbw->txn(
19             sub {
20 51     51   5731 my $start = time;
21 51         523 $self->stop_work(
22             stop => $start,
23             save => 1,
24             );
25              
26 51         102 my $name = '';
27 51         94 my $email = '';
28 51         82 my $spacer = '';
29              
30 51 100       254 if ( $opts->{self} ) {
31 50         2711 print "Creating \"self\" identity:\n";
32              
33 50         503 my $git_conf_file =
34             path( File::HomeDir->my_home, '.gitconfig' );
35 50   50     6022 my $git_conf = Config::Tiny->read($git_conf_file) || {};
36              
37 50   50     4598 $name = $git_conf->{user}->{name} || 'Your Name';
38 50   50     271 $email = $git_conf->{user}->{email} || 'your@email.adddr';
39              
40 50         289 $name =~ s/(^")|("$)//g;
41 50         225 $email =~ s/(^")|("$)//g;
42              
43 50         172 $spacer = ' ';
44             }
45              
46 51   50     640 $opts->{name} ||= prompt( $spacer . 'Name:', $name )
      66        
47             || return $self->err( 'NameRequired', 'name is required' );
48              
49 51         3610 my $short = join( '', $opts->{name} =~ m/(\b\w)/g );
50 51   50     421 $opts->{shortname} ||= prompt( $spacer . 'Short Name:', $short )
      33        
51             || return $self->err( 'NameRequired', 'shortname is required' );
52              
53 51   50     3103 $opts->{method} ||= prompt( $spacer . 'Contact Method:', 'email' )
      66        
54             || return $self->err( 'MethodRequired', 'method is required' );
55              
56             $opts->{value} ||=
57 51   50     2504 prompt( $spacer . 'Contact ' . ucfirst( $opts->{method} ) . ':',
      66        
58             $email )
59             || return $self->err( 'ValueRequired', 'value is required' );
60              
61 51   100     2976 $opts->{message} ||= '';
62 51         852 $opts->{id} = $dbw->nextval('nodes');
63 51         17228 $opts->{ecm_id} = $dbw->nextval('nodes');
64              
65 51 100       12732 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 50         369 );
72             }
73             else {
74 1         7 $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 50         1720 );
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 50         129162 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 50         101002 );
108              
109             $self->start_work(
110             node_id => $opts->{id},
111 50         84871 start => $start,
112             start_comment => "new identity",
113             billable => 1,
114             );
115              
116 50         25598 $self->stop_work(
117             stop => time,
118             restore => 1,
119             );
120              
121             $self->save_work(
122             node_id => $opts->{id},
123             change_id => $opts->{change_id}
124 50         450 );
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 50         16232 );
133             }
134 51         533 );
135              
136 50         9169 return $self->ok('NewIdentity');
137             }
138              
139             1;
140             __END__