lib/App/LinkSite/Link.pm

filter: Statementstmt 100% 64 / 64 Branchbran 93.7% 30 / 32 Conditioncond 93.3% 14 / 15 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 96.9% 126 / 130 CC 26 SCAR 23.6
CC
26
Cov
97%
tracked_link
17.0
__ANON__
7.0
mk_link
4.0
CRAP 10.6
↑ Summary lib/App/LinkSite.pm »
1 =head1 NAME
2  
3 App::LinkSite::Link
4  
5 =head1 SYNOPSIS
6  
7 (You probably want to just look at the L<linksite> application.)
8  
9 =head1 DESCRIPTION
10  
11 A class to model a link on a link site (part of App::LinkSite).
12  
13 =cut
14  
15 18 ▶use Feature::Compat::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 18 class App::LinkSite::Link {
18 18 our $VERSION = '0.2.0';
19 18 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶ no if $] >= 5.038, 'warnings', 'experimental::class';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 18 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 18 ▶ use URI::Escape qw(uri_escape_utf8 uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 18 field $title :reader :param;
27 18 field $subtitle :reader :param = '';
28 18 field $link :reader :param;
29 18 field $new_link :reader(is_new) :param(new) = 0;
30 18 field $utm :param = undef;
31  
32 128 ▶ ADJUST {
Subroutine __ANON__: called CC=7 SCAR=19.5
33 128 ▶ if ($utm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utm) 37 91 128
34 37 ▶ die "utm must be an object or false\n" unless ref $utm eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $utm eq "HASH" 2 35 37
35 35 for my $key (keys %$utm) {
36 26 ▶ die "Invalid UTM parameter '$key'; use utm_* names\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\Autm_[a-z][a-z0-9_]*\z/ 1 25 26
37 unless $key =~ /\Autm_[a-z][a-z0-9_]*\z/;
38 die "UTM parameter '$key' must be a string, number, or null\n"
39 25 ▶ if defined $utm->{$key} && ref $utm->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $utm->{$key} and ref $utm->{$key} 1 24 25
Condition100%3/3
defined $utm->{$key} and ref $utm->{$key}
!ll&&!rl&&r
6181
MC/DC100%2/2
defined $utm->{$key} and ref $utm->{$key}
defined $utm->{$key} ref $utm->{$key}
Truth table100%3/3
A and B
A= defined $utm->{$key}B= ref $utm->{$key}
ABresult
0X0
100
111
40 }
41 }
42 }
43  
44 =head1 METHODS
45  
46 =head2 tracked_link
47  
48 Returns the destination used for clicks. An optional C<utm> constructor
49 parameter enables tracking when it is a hash reference, even an empty one.
50 Defaults are C<utm_source=linksite>, C<utm_medium=referral>,
51 C<utm_campaign=profile>, and C<utm_content> from a slug of the link title.
52 Slugs use lowercase Unicode letters and numbers, with other runs replaced
53 by hyphens and leading/trailing hyphens removed. Empty slugs are omitted.
54 Supplied values override them; null or empty values omit a parameter.
55 False or undefined C<utm> leaves the destination unchanged.
56  
57 Only absolute HTTP(S) URLs are tagged. Existing query parameters, including
58 UTM values, and fragments are preserved. C<link> still returns the original
59 destination, which is also used for JSON-LD.
60  
61 =cut
62  
63 57 ▶ method tracked_link {
Subroutine tracked_link: called CC=17 SCAR=28.3
Pod documented
64 57 ▶ return $link unless $utm;
Branch: 1 branch
BranchTrueFalseTotal
unless $utm 14 43 57
65 43 my $uri = URI->new($link);
66 43 ▶ return $link unless ($uri->scheme // '') =~ /\Ahttps?\z/i && $uri->host;
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme // "") =~ /\Ahttps?\z/i and $uri->host 8 35 43
Condition100%5/5
($uri->scheme // "") =~ /\Ahttps?\z/i and $uri->host
!ll&&!rl&&r
7135
$uri->scheme // ""
l!l
394
MC/DC100%2/2
($uri->scheme // "") =~ /\Ahttps?\z/i and $uri->host
$uri->scheme $uri->host
Truth table100%3/3
A and B
A= $uri->schemeB= $uri->host
ABresult
0X0
100
111
67  
68 35 ▶ my $content = lc($title // '');
Condition50%1/2
$title // ""
l!l
350
MC/DC0%0/1
$title // ""
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
69 35 $content =~ s/[^\p{L}\p{N}]+/-/g;
70 35 $content =~ s/\A-|-\z//g;
71 35 my %parameters = (
72 utm_source => 'linksite', utm_medium => 'referral',
73 utm_campaign => 'profile', utm_content => $content, %$utm,
74 );
75 35 my %existing;
76 35 ▶ for my $pair (split /[&;]/, $uri->query // '') {
Condition100%2/2
$uri->query // ""
l!l
629
MC/DC100%1/1
$uri->query // ""
$uri->query
Truth table100%2/2
A
A= $uri->query
Aresult
11
00
77 10 ▶ next unless length $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair 2 8 10
78 8 my ($key) = split /=/, $pair, 2;
79 8 $key =~ tr/+/ /;
80 8 $existing{uri_unescape($key)} = 1;
81 }
82 35 my @added;
83 35 for my $key (sort keys %parameters) {
84 142 ▶ next if exists $existing{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $existing{$key} 3 139 142
85 139 ▶ next unless defined $parameters{$key} && length $parameters{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parameters{$key} and length $parameters{$key} 14 125 139
Condition100%3/3
defined $parameters{$key} and length $parameters{$key}
!ll&&!rl&&r
86125
MC/DC100%2/2
defined $parameters{$key} and length $parameters{$key}
defined $parameters{$key} length $parameters{$key}
Truth table100%3/3
A and B
A= defined $parameters{$key}B= length $parameters{$key}
ABresult
0X0
100
111
86 125 push @added, uri_escape_utf8($key) . '=' . uri_escape_utf8($parameters{$key});
87 }
88 35 ▶ return $link unless @added;
Branch: 1 branch
BranchTrueFalseTotal
unless @added 1 34 35
89  
90 # Append to the original query so its ordering, encoding, and duplicates survive.
91 34 my ($base, $fragment) = split /#/, $link, 2;
92 34 ▶ my $separator = index($base, '?') < 0 ? '?' : $base =~ /[?&]\z/ ? '' : '&';
Branch: 2 branches
BranchTrueFalseTotal
index($base, '?') == -1 ? : 28 6 34
$base =~ /[?&]\z/ ? : 3 3 6
93 34 ▶ return $base . $separator . join('&', @added)
Branch: 1 branch
BranchTrueFalseTotal
defined $fragment ? : 3 31 34
94 . (defined $fragment ? "#$fragment" : '');
95 }
96  
97 =head2 mk_link
98  
99 Returns a fragment of HTML that is used to display this link on the site.
100  
101 It's actually an `<li>` that wraps an `<a>`. So this method probably needs
102 to be renamed or refactored. Maybe it should just be called `render()`.
103  
104 =cut
105  
106 17 ▶ method mk_link {
Subroutine mk_link: called CC=4 SCAR=13.9
Pod documented
107 17 my $destination = $self->tracked_link;
108 17 ▶ if ($utm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utm) 10 7 17
109 10 $destination =~ s/&/&amp;/g;
110 10 $destination =~ s/"/&quot;/g;
111 10 $destination =~ s/</&lt;/g;
112 10 $destination =~ s/>/&gt;/g;
113 }
114 17 my $a_tag = q[<a href="] . $destination . q[" class="text-decoration-none">] . $self->title . q[</a>];
115 17 ▶ my $subtitle = $self->subtitle ? q[<br><small class="text-muted">] . $self->subtitle . q[</small>] : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->subtitle ? : 0 17 17
116  
117 17 my @li_classes = qw[list-group-item list-group-item-action];
118 17 ▶ push @li_classes, 'new-link' if $self->is_new;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_new 0 17 17
119  
120 17 return qq[<li class='@li_classes'>$a_tag$subtitle</li>];
121 }
122 }
123  
124 =head1 AUTHOR
125  
126 Dave Cross <dave@davecross.co.uk>
127  
128 =head1 COPYRIGHT AND LICENCE
129  
130 Copyright (c) 2024, Magnum Solutions Ltd. All Rights Reserved.
131  
132 This library is free software; you can redistribute it and/or modify it
133 under the same terms as Perl itself.
134  
135 =cut
136  
137 18 1;
↑ Summary lib/App/LinkSite.pm »