Galileo.pm

filter: Statementstmt 87.5% 119 / 136 Branchbran 47.7% 21 / 44 Conditioncond 63.6% 7 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 91.6% 22 / 24 Podpod 50.0% 1 / 2 totaltotal 76.7% 172 / 224 CC 28 SCAR 22.9
CC
28
Cov
77%
load_config
27.8
__ANON__
20.0
startup
3.0
CRAP 9.9
« Galileo/Command/setup.pm ↑ Summary Galileo/Command/dump.pm »
1 package Galileo;
2 3 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 our $VERSION = '0.043';
5 3 $VERSION = eval $VERSION;
6  
7 3 ▶use File::Basename 'dirname';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec::Functions qw'rel2abs catdir';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use Mojo::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶has db => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
16 0 my $self = shift;
17 0 ▶ my $schema_class = $self->config->{db_schema} or die "Unknown DB Schema Class";
Branch: 1 branch
BranchTrueFalseTotal
my $schema_class = $self->config->{"db_schema"} or die "Unknown DB Schema Class" 0 0 0
18 0 ▶ eval "require $schema_class" or die "Could not load Schema Class ($schema_class). $@\n";
Branch: 1 branch
BranchTrueFalseTotal
eval "require $schema_class" or die "Could not load Schema Class ($schema_class). $@\n" 0 0 0
19  
20 my $schema = $schema_class->connect(
21 0 @{ $self->config }{ qw/db_dsn db_username db_password db_options/ }
22 0 ▶ ) or die "Could not connect to $schema_class using DSN " . $self->config->{db_dsn};
Branch: 1 branch
BranchTrueFalseTotal
my $schema = $schema_class->connect(@{$self->config;}{"db_dsn", "db_username", "db_password", "db_options"}) or die "Could not connect to $schema_class using DSN " . $self->config->{"db_dsn"} 0 0 0
23  
24 0 return $schema;
25 3 };
26  
27 3 ▶has home => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
28 3 ▶ my $path = $ENV{GALILEO_HOME} || getcwd;
Condition66%2/3
$ENV{'GALILEO_HOME'} || getcwd()
l!l&&r!l&&!r
120
MC/DC0%0/2
$ENV{'GALILEO_HOME'} || getcwd()
$ENV{'GALILEO_HOME'} getcwd()
Truth table66%2/3
A || B
A= $ENV{'GALILEO_HOME'}B= getcwd()
ABresult
1X1
011
000
29 3 return Mojo::Home->new(File::Spec->rel2abs($path));
30 3 };
31  
32 3 ▶has config_file => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
33 3 my $self = shift;
34 3 ▶ return $ENV{GALILEO_CONFIG} if $ENV{GALILEO_CONFIG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"GALILEO_CONFIG"} 0 3 3
35 3 return $self->home->rel_file('galileo.conf');
36 3 };
37  
38 3 ▶sub load_config {
Subroutine load_config: called CC=13 SCAR=33.3
Pod undocumented
39 3 my $app = shift;
40  
41 3 $app->plugin( Config => {
42 file => $app->config_file,
43 default => {
44 db_schema => 'Galileo::DB::Schema',
45 db_dsn => 'dbi:SQLite:dbname=' . $app->home->rel_file('galileo.db'),
46 db_username => undef,
47 db_password => undef,
48 db_options => { sqlite_unicode => 1 },
49 extra_css => [ '/themes/standard.css' ],
50 extra_js => [],
51 extra_static_paths => ['static'],
52 sanitize => 1,
53 pagedown_extra_options => {
54 highlighter => 'highlight',
55 table_class => 'table table-striped',
56 extensions => [qw/
57 tables
58 fenced_code_gfm
59 def_list
60 attr_list
61 footnotes
62 smartypants
63 strikethrough
64 newlines
65 /],
66 },
67 secrets => [],
68 upload_path => 'uploads',
69 },
70 });
71  
72 # handle deprecated db_connect
73 3 ▶ if ( my $db_connect = delete $app->config->{db_connect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db_connect = delete $app->config->{"db_connect"}) 0 3 3
74 0 warn "### Configuration key db_connect is deprecated ###\n";
75 0 ▶ if ( ref $db_connect ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $db_connect) { } 0 0 0
76 0 @{ $app->config }{ qw/db_dsn db_username db_password db_options/ } = @$db_connect;
77 } else {
78 0 $app->config->{db_dsn} = $db_connect;
79 }
80 }
81  
82 # handle deprecated files key
83 3 ▶ if (my $files = $app->config->{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $files = $app->config->{"files"}) 0 3 3
84 0 warn "### 'files' config key is deprecated, use extra_static_paths ###\n";
85 0 ▶ push @{ $app->config->{extra_static_paths} }, (ref $files ? @$files : $files);
Branch: 1 branch
BranchTrueFalseTotal
ref $files ? : 0 0 0
86 }
87  
88 # add the files directories to array of static content folders
89 # TODO don't repeat
90 3 foreach my $dir ( @{$app->config->{extra_static_paths}} ) {
91 # convert relative paths to relative one (to home dir)
92 3 $dir = $app->_to_abs($dir);
93 3 ▶ push @{ $app->static->paths }, $dir if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 1 2 3
94 }
95  
96 # normalize and make helper for upload directory
97 3 my $upload;
98 3 ▶ if ( my $dir = $app->config->{upload_path} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dir = $app->config->{"upload_path"}) 3 0 3
99 3 $upload = $app->_to_abs($dir);
100 }
101 3 ▶ if ( -d $upload ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $upload) { } 1 2 3
102 1 push @{ $app->static->paths }, $upload;
103 1 $app->config->{upload_path} = $upload;
104 1 ▶ $app->helper( upload_path => sub { $upload } );
Subroutine __ANON__: called CC=1 SCAR=0.0
105 } else {
106 0 ▶ $app->helper( upload_path => sub { 0 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
107 }
108  
109  
110 3 ▶ if ( my $secrets = $app->config->{secrets} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $secrets = $app->config->{'secrets'}) { } 3 0 3
elsif (my $secret = $app->config->{'secret'}) { } 0 0 0
111 3 ▶ $app->secrets($secrets) if @$secrets;
Branch: 1 branch
BranchTrueFalseTotal
if @$secrets 0 3 3
112 } elsif ( my $secret = $app->config->{secret} ) {
113 0 warn "### 'secret' config key is deprecated, use 'secrets' instead ###\n";
114 0 ▶ $app->secrets([$secret]) if $secret;
Branch: 1 branch
BranchTrueFalseTotal
if $secret 0 0 0
115 }
116 }
117  
118 6 ▶sub _to_abs {
Subroutine _to_abs: called CC=2 SCAR=7.0
119 6 my ($self, $dir) = @_;
120 6 ▶ unless ( File::Spec->file_name_is_absolute( $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("File::Spec"->file_name_is_absolute($dir)) 6 0 6
121 6 $dir = $self->home->rel_file( $dir );
122 }
123 6 return $dir;
124 }
125  
126 3 ▶sub startup {
Subroutine startup: called CC=3 SCAR=11.0
Pod documented
127 3 my $app = shift;
128  
129 3 $app->load_config;
130  
131 {
132 # use content from directories under lib/Galileo/files or using File::ShareDir
133 3 my $lib_base = catdir(dirname(rel2abs(__FILE__)), 'Galileo', 'files');
134  
135 3 my $public = catdir($lib_base, 'public');
136 3 ▶ $app->static->paths->[0] = -d $public ? $public : catdir(dist_dir('Galileo'), 'public');
Branch: 1 branch
BranchTrueFalseTotal
-d $public ? : 0 3 3
137  
138 3 my $templates = catdir($lib_base, 'templates');
139 3 ▶ $app->renderer->paths->[0] = -d $templates ? $templates : catdir(dist_dir('Galileo'), 'templates');
Branch: 1 branch
BranchTrueFalseTotal
-d $templates ? : 0 3 3
140 }
141  
142 # use commands from Galileo::Command namespace
143 3 push @{$app->commands->namespaces}, 'Galileo::Command';
144  
145 ## Helpers ##
146  
147 3 ▶ $app->helper( schema => sub { shift->app->db } );
Subroutine __ANON__: called CC=1 SCAR=0.0
148  
149 3 ▶ $app->helper( 'home_page' => sub{ '/page/home' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
150  
151 3 ▶ $app->helper( 'auth_fail' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
152 3 my $self = shift;
153 3 ▶ my $message = shift || "Not Authorized";
Condition50%1/2
shift() || 'Not Authorized'
l!l
03
MC/DC0%0/1
shift() || 'Not Authorized'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
154 3 $self->humane_flash( $message );
155 3 $self->redirect_to( $self->home_page );
156 3 return 0;
157 3 });
158  
159 75 ▶ $app->helper( 'get_user' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
160 75 my ($self, $name) = @_;
161 75 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 34 41 75
162 34 $name = $self->session->{username};
163 }
164 75 ▶ return undef unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 5 70 75
165 70 return $self->schema->resultset('User')->single({name => $name});
166 3 });
167  
168 38 ▶ $app->helper( 'is_author' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
169 38 my $self = shift;
170 38 my $user = $self->get_user(@_);
171 38 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 4 34 38
172 34 return $user->is_author;
173 3 });
174 32 ▶ $app->helper( 'is_admin' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
175 32 my $self = shift;
176 32 my $user = $self->get_user(@_);
177 32 ▶ return undef unless $user;
Branch: 1 branch
BranchTrueFalseTotal
unless $user 1 31 32
178 31 return $user->is_admin;
179 3 });
180  
181 3 $app->plugin('Memorize');
182 3 $app->plugin('Galileo::Plugin::Modal');
183 3 $app->plugin('Humane', auto => 0);
184  
185 ## Routing ##
186  
187 3 my $r = $app->routes;
188  
189 3 ▶ $r->any( '/' => sub { my $self = shift; $self->redirect_to( $self->home_page ) });
Subroutine __ANON__: called CC=1 SCAR=0.0
190 3 $r->any( '/page/:name' )->to('page#show')->name('show_page');
191 3 $r->post( '/login' )->to('user#login');
192 3 $r->any( '/logout' )->to('user#logout');
193  
194 16 ▶ my $if_author = $r->under( sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
195 16 my $self = shift;
196 16 ▶ return $self->is_author || $self->auth_fail;
Condition66%2/3
$self->is_author || $self->auth_fail
l!l&&r!l&&!r
1402
MC/DC50%1/2
$self->is_author || $self->auth_fail
$self->is_author $self->auth_fail
Truth table66%2/3
A || B
A= $self->is_authorB= $self->auth_fail
ABresult
1X1
011
000
197 3 });
198  
199 3 $if_author->any( '/admin/menu' )->to('menu#edit');
200 3 $if_author->any( '/edit/:name' )->to('page#edit')->name('edit_page');
201 3 $if_author->websocket( '/store/page' )->to('page#store');
202 3 $if_author->websocket( '/store/menu' )->to('menu#store');
203 3 $if_author->websocket( '/files/list' )->to('file#list');
204  
205 14 ▶ my $if_admin = $r->under( sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
206 14 my $self = shift;
207 14 ▶ return $self->is_admin || $self->auth_fail;
Condition66%2/3
$self->is_admin || $self->auth_fail
l!l&&r!l&&!r
1301
MC/DC50%1/2
$self->is_admin || $self->auth_fail
$self->is_admin $self->auth_fail
Truth table66%2/3
A || B
A= $self->is_adminB= $self->auth_fail
ABresult
1X1
011
000
208 3 });
209  
210 3 $if_admin->any( '/admin/users' )->to('admin#users');
211 3 $if_admin->any( '/admin/pages' )->to('admin#pages');
212 3 $if_admin->any( '/admin/user/:name' )->to('admin#user');
213 3 $if_admin->websocket( '/store/user' )->to('admin#store_user');
214 3 $if_admin->websocket( '/remove/page' )->to('admin#remove_page');
215  
216 }
217  
218 3 1;
219  
220 __END__
« Galileo/Command/setup.pm ↑ Summary Galileo/Command/dump.pm »