blib/lib/Dancer/Plugin/Facebook.pm

filter: Statementstmt 46.6% 42 / 90 Branchbran 19.2% 5 / 26 Conditioncond 16.6% 2 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 53.3% 8 / 15 Podpod n/a totaltotal 38.0% 57 / 150 CC 19 SCAR 25.9
CC
19
Cov
38%
__ANON__
33.0
_do_fb_postback
20.0
__ANON__
14.5
CRAP 13.3
↑ Summary
1 package Dancer::Plugin::Facebook;
2 {
3 2 $Dancer::Plugin::Facebook::VERSION = '0.991';
4 }
5 # ABSTRACT: Manage Facebook interaction within Dancer applications
6  
7 2 ▶use Dancer qw{:syntax};
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Dancer::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Facebook::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 2 my (%config, $fb, $redirect);
15  
16 1 ▶sub _get_fb {
Subroutine _get_fb: called CC=5 SCAR=19.8
17 1 ▶ debug "Getting fb object [", $fb // "undef", "]";
Condition50%1/2
$fb // "undef"
l!l
01
MC/DC0%0/1
$fb // "undef"
$fb
Truth table50%1/2
A
A= $fb
Aresult
11
00
18  
19 # The first time out, turn our raw, local postback URL into a
20 # fully qualified one (see _do_fb_postback for more explanation).
21 1 ▶ if ($config{raw_postback}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"raw_postback"}) 0 1 1
22 0 my $url = delete $config{raw_postback};
23 # Place the postback url in $config for object instantiation
24 0 $config{postback} = uri_for ($url);
25 0 debug "Full postback URL is ", $config{postback};
26 }
27  
28 # We use a before hook to clear a stale FB handle out, and just
29 # use ||= to regenerate as necessary here.
30 1 ▶ $fb ||= do {
Condition33%1/3
$fb ||= do { my(%settings) = %config; if (my $access_token = session()->{"auth"}{"facebook"}) { $settings{"access_token"} = $access_token; } ; debug("Creating Facebook::Graph object with settings ", \%settings); "Facebook::Graph"->new(%settings) }
l!l&&r!l&&!r
010
MC/DC0%0/2
$fb ||= do { my(%settings) = %config; if (my $access_token = session()->{"auth"}{"facebook"}) { $settings{"access_token"} = $access_token; } ; debug("Creating Facebook::Graph object with settings ", \%settings); "Facebook::Graph"->new(%settings) }
$fb do { my(%settings) = %config; if (my $access_token = session()->{"auth"}{"facebook"}) { $settings{"access_token"} = $access_token; } ; debug("Creating Facebook::Graph object with settings ", \%settings); "Facebook::Graph"->new(%settings) }
Truth table33%1/3
A ||= B
A= $fbB= do { my(%settings) = %config; if (my $access_token = session()->{"auth"}{"facebook"}) { $settings{"access_token"} = $access_token; } ; debug("Creating Facebook::Graph object with settings ", \%settings); "Facebook::Graph"->new(%settings) }
ABresult
1X1
011
000
31 1 my %settings = %config;
32 1 ▶ if (my $access_token = session->{auth}->{facebook}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $access_token = session()->{"auth"}{"facebook"}) 0 1 1
33 0 $settings{access_token} = $access_token;
34 }
35 1 debug "Creating Facebook::Graph object with settings ", \%settings;
36 1 Facebook::Graph->new (%settings);
37 };
38 }
39  
40 0 ▶sub _get_fb_redirect_url () {
Subroutine _get_fb_redirect_url: not called CC=3 SCAR=24.8
41 0 ▶ $redirect ||= do {
Condition0%0/3
$redirect ||= do { my $settings = plugin_setting(); debug("Settings are ", $settings); my(@permissions) = ref $settings->{"permissions"} eq "ARRAY" ? @{$settings->{"permissions"};} : (); _get_fb()->authorize->extend_permissions(@permissions)->uri_as_string }
l!l&&r!l&&!r
000
MC/DC0%0/2
$redirect ||= do { my $settings = plugin_setting(); debug("Settings are ", $settings); my(@permissions) = ref $settings->{"permissions"} eq "ARRAY" ? @{$settings->{"permissions"};} : (); _get_fb()->authorize->extend_permissions(@permissions)->uri_as_string }
$redirect do { my $settings = plugin_setting(); debug("Settings are ", $settings); my(@permissions) = ref $settings->{"permissions"} eq "ARRAY" ? @{$settings->{"permissions"};} : (); _get_fb()->authorize->extend_permissions(@permissions)->uri_as_string }
Truth table0%0/3
A ||= B
A= $redirectB= do { my $settings = plugin_setting(); debug("Settings are ", $settings); my(@permissions) = ref $settings->{"permissions"} eq "ARRAY" ? @{$settings->{"permissions"};} : (); _get_fb()->authorize->extend_permissions(@permissions)->uri_as_string }
ABresult
1X1
011
000
42 0 my $settings = plugin_setting;
43 0 debug "Settings are ", $settings;
44 0 ▶ my @permissions = ref $settings->{permissions} eq "ARRAY" ? @{$settings->{permissions}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $settings->{'permissions'} eq 'ARRAY' ? : 0 0 0
45 0 _get_fb->authorize->extend_permissions (@permissions)->uri_as_string;
46 };
47 }
48  
49 0 ▶sub _do_fb_redirect () {
Subroutine _do_fb_redirect: not called CC=1 SCAR=6.9
50 0 my $url = _get_fb_redirect_url;
51 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 debug "Redirecting to $url";
53 0 redirect $url, 303;
54 }
55 0 }
56  
57 0 ▶sub _do_fb_postback ($) {
Subroutine _do_fb_postback: not called CC=4 SCAR=30.0
58 0 my $settings = plugin_setting;
59 0 debug "Settings are ", $settings;
60  
61 0 my ($url) = @_;
62  
63 # We can only determine the relative URL right now, but that's
64 # enough for initializing the route. We put the relative URL
65 # in $config{raw_postback} so that when fb is called for the
66 # first time, which will be within a route handler, we can sub
67 # in the full URL, which is what FB actually needs
68 0 ▶ die "You must give me the postback URL when calling fb_postback" unless ($url);
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
69 0 $config{raw_postback} = $url;
70  
71 # This hook will get called when we successfully authenticate and have
72 # put the token in the session, so the application developer can
73 # retrieve it. It doesn't need to exist if a postback route hasn't been
74 # established
75 0 register_hook (['fb_access_token_available']);
76  
77 0 ▶ my $success = $settings->{landing}->{success} || "/";
Condition0%0/2
$settings->{'landing'}{'success'} || '/'
l!l
00
MC/DC0%0/1
$settings->{'landing'}{'success'} || '/'
$settings->{'landing'}{'success'}
Truth table0%0/2
A
A= $settings->{'landing'}{'success'}
Aresult
11
00
78 0 ▶ my $failure = $settings->{landing}->{failure} || "/";
Condition0%0/2
$settings->{'landing'}{'failure'} || '/'
l!l
00
MC/DC0%0/1
$settings->{'landing'}{'failure'} || '/'
$settings->{'landing'}{'failure'}
Truth table0%0/2
A
A= $settings->{'landing'}{'failure'}
Aresult
11
00
79  
80 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my $token = _get_fb->request_access_token (params->{code});
83 0 session->{auth}->{facebook} = $token->token;
84 0 execute_hooks 'fb_access_token_available', $token->token;
85 # Go back wherever
86 0 redirect $success;
87 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 redirect $failure;
89 0 };
90 }
91 0 }
92  
93 1 ▶register setup_fb => sub (;$) {
Subroutine __ANON__: called CC=5 SCAR=26.7
94 1 my ($url) = @_;
95 1 debug "Setting up fb access";
96  
97 # We need global access to this, grab it here
98 1 my $settings = plugin_setting;
99 1 debug "Settings are ", $settings;
100  
101 # Copy our registered application information over
102 1 ▶ if (ref $settings->{application} eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $settings->{"application"} eq "HASH") 0 1 1
103 0 debug "Setting application information";
104 0 ▶ $config{app_id} = $settings->{application}->{app_id} or die "You didn't give me an app_id for Dancer::Plugin::Facebook";
Branch: 1 branch
BranchTrueFalseTotal
$config{"app_id"} = $settings->{"application"}{"app_id"} or die "You didn't give me an app_id for Dancer::Plugin::Facebook" 0 0 0
105 0 ▶ $config{secret} = $settings->{application}->{secret} or die "You didn't give me a secret for Dancer::Plugin::Facebook";
Branch: 1 branch
BranchTrueFalseTotal
$config{"secret"} = $settings->{"application"}{"secret"} or die "You didn't give me a secret for Dancer::Plugin::Facebook" 0 0 0
106 }
107  
108 # Set a hook to clear out any old object unless existing tokens in
109 # the object and session match one another. In theory, this means
110 # that absent an access token, we should never replace it.
111 1 debug "Setting hook to clear facebook context";
112 1 ▶ hook before => sub {
Subroutine __ANON__: called CC=6 SCAR=35.0
113 1 ▶ if (defined $fb) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fb) 0 1 1
114 0 debug "Considering clearing facebook context";
115 0 ▶ if (defined session->{auth}->{facebook}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined session()->{'auth'}{'facebook'}) { } 0 0 0
116 0 ▶ if ($fb->has_access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fb->has_access_token) { } 0 0 0
117 0 ▶ if ($fb->access_token ne session->{auth}->{facebook}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fb->access_token ne session()->{"auth"}{"facebook"}) 0 0 0
118 0 debug "Current FB access token doesn't match";
119 0 undef $fb;
120 }
121 } else {
122 0 debug "Current FB doesn't have access token";
123 0 undef $fb;
124 }
125 } else {
126 0 ▶ if ($fb->has_access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fb->has_access_token) 0 0 0
127 0 debug "Current login doesn't have access token";
128 0 undef $fb;
129 }
130 }
131 }
132 1 };
133  
134 # If the user wants the automatic URL setup
135 1 ▶ if ($url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url) 0 1 1
136 0 debug "Creating handler for ", $url;
137 0 get $url => _do_fb_redirect;
138  
139 0 my $postback = "$url/postback";
140 0 debug "Creating handler for ", $postback;
141 0 get $postback => _do_fb_postback $postback;
142 }
143  
144 1 debug "Done setting up fb access";
145 2 };
146  
147 2 register fb => \&_get_fb;
148 2 register fb_redirect => \&_do_fb_redirect;
149 2 register fb_redirect_url => \&_get_fb_redirect_url;
150 2 register fb_postback => \&_do_fb_postback;
151 2 register_plugin;
152  
153  
154 2 1;
155  
156 __END__
↑ Summary