Silki/Schema/Domain.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Silki/HTML/FormatText.pm ↑ Summary Silki/Formatter/WikiToHTML.pm »
1 package Silki::Schema::Domain;
2 {
3 $Silki::Schema::Domain::VERSION = '0.29';
4 }
5  
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use Net::Interface;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Silki::Config;
12 use Silki::I18N qw( loc );
13 use Silki::Schema;
14 use Silki::Types qw( Bool HashRef Int Str );
15 use Socket qw( AF_INET );
16 use Sys::Hostname qw( hostname );
17 use URI;
18  
19 use Fey::ORM::Table;
20 use MooseX::ClassAttribute;
21 use MooseX::Params::Validate qw( validated_hash validated_list );
22  
23 with 'Silki::Role::Schema::URIMaker';
24  
25 with 'Silki::Role::Schema::DataValidator' => {
26 steps => [
27 '_web_hostname_is_unique',
28 '_email_hostname_is_unique',
29 ],
30 };
31  
32 has_policy 'Silki::Schema::Policy';
33  
34 my $Schema = Silki::Schema->Schema();
35  
36 has_table( $Schema->table('Domain') );
37  
38 query wiki_count => (
39 select => __PACKAGE__->_WikiCountSelect(),
40 bind_params => sub { $_[0]->domain_id() },
41 );
42  
43 class_has 'DefaultDomain' => (
44 is => 'ro',
45 isa => __PACKAGE__,
46 lazy => 1,
47 default => sub { __PACKAGE__->_FindOrCreateDefaultDomain() },
48 );
49  
50 class_has _AllDomainSelect => (
51 is => 'ro',
52 isa => 'Fey::SQL::Select',
53 lazy => 1,
54 builder => '_BuildAllDomainSelect',
55 );
56  
57 has uri_params => (
58 is => 'ro',
59 isa => HashRef,
60 lazy => 1,
61 builder => '_build_uri_params',
62 init_arg => undef,
63 );
64  
65 around insert => sub {
66 my $orig = shift;
67 my $class = shift;
68 my %p = @_;
69  
70 $p{email_hostname} //= $p{web_hostname};
71  
72 return $class->$orig(%p);
73 };
74  
75 sub _web_hostname_is_unique {
76 my $self = shift;
77 my $p = shift;
78 my $is_insert = shift;
79  
80 return
81 if !$is_insert
82 && exists $p->{web_hostname}
83 && $p->{web_hostname} eq $self->web_hostname();
84  
85 return unless __PACKAGE__->new( web_hostname => $p->{web_hostname} );
86  
87 return {
88 field => 'web_hostname',
89 message => loc(
90 'The web hostname you provided is already in use by another domain.'
91 ),
92 };
93 }
94  
95 sub _email_hostname_is_unique {
96 my $self = shift;
97 my $p = shift;
98 my $is_insert = shift;
99  
100 return
101 if !$is_insert
102 && exists $p->{email_hostname}
103 && $p->{email_hostname} eq $self->email_hostname();
104  
105 return unless __PACKAGE__->new( email_hostname => $p->{email_hostname} );
106  
107 return {
108 field => 'email_hostname',
109 message => loc(
110 'The email hostname you provided is already in use by another domain.'
111 ),
112 };
113 }
114  
115 sub _base_uri_path {
116 return q{/};
117 }
118  
119 sub entity_uri {
120 my $self = shift;
121 my %p = @_;
122  
123 $p{view}
124 = 'domain/'
125 . $self->domain_id()
126 . ( $p{view} ? q{/} . $p{view} : q{} );
127  
128 return $self->uri(%p);
129 }
130  
131 sub EnsureRequiredDomainsExist {
132 my $class = shift;
133  
134 $class->_FindOrCreateDefaultDomain();
135 }
136  
137 sub _FindOrCreateDefaultDomain {
138 my $class = shift;
139  
140 my $hostname = $ENV{SILKI_HOSTNAME} || $class->_SystemHostname();
141  
142 my $domain = $class->new( web_hostname => $hostname );
143 return $domain if $domain;
144  
145 return $class->insert( web_hostname => $hostname );
146 }
147  
148 sub _SystemHostname {
149 for my $name (
150 hostname(),
151 map { scalar gethostbyaddr( $_->address(), AF_INET ) }
152 grep { $_->address() } Net::Interface->interfaces()
153 ) {
154 return $name if $name =~ /\.[^.]+$/;
155 }
156  
157 die 'Cannot determine system hostname.';
158 }
159  
160 sub domain { $_[0] }
161  
162 sub _build_uri_params {
163 my $self = shift;
164  
165 return {
166 scheme => ( $self->requires_ssl() ? 'https' : 'http' ),
167 host => $self->web_hostname(),
168 };
169 }
170  
171 sub application_uri {
172 my $self = shift;
173 my %p = validated_hash(
174 \@_,
175 path => { isa => Str, optional => 1 },
176 fragment => { isa => Str, optional => 1 },
177 query => { isa => HashRef, default => {} },
178 with_host => { isa => Bool, default => 0 },
179 );
180  
181 return $self->_make_uri(%p);
182 }
183  
184 sub _WikiCountSelect {
185 my $class = shift;
186  
187 my $select = Silki::Schema->SQLFactoryClass()->new_select();
188  
189 my $wiki_t = $Schema->table('Wiki');
190  
191 my $count
192 = Fey::Literal::Function->new( 'COUNT', $wiki_t->column('wiki_id') );
193  
194 #<<<
195 $select
196 ->select($count)
197 ->from( $wiki_t )
198 ->where( $wiki_t->column('domain_id'), '=',
199 Fey::Placeholder->new() );
200 #>>>
201 return $select;
202 }
203  
204 sub All {
205 my $class = shift;
206 my ( $limit, $offset ) = validated_list(
207 \@_,
208 limit => { isa => Int, optional => 1 },
209 offset => { isa => Int, default => 0 },
210 );
211  
212 my $select = $class->_AllDomainSelect()->clone();
213 $select->limit( $limit, $offset );
214  
215 my $dbh = Silki::Schema->DBIManager()->source_for_sql($select)->dbh();
216  
217 return Fey::Object::Iterator::FromSelect->new(
218 classes => 'Silki::Schema::Domain',
219 select => $select,
220 dbh => $dbh,
221 bind_params => [ $select->bind_params() ],
222 );
223 }
224  
225 sub _BuildAllDomainSelect {
226 my $class = shift;
227  
228 my $select = Silki::Schema->SQLFactoryClass()->new_select();
229  
230 my $domain_t = $Schema->table('Domain');
231  
232 #<<<
233 $select
234 ->select($domain_t)
235 ->from($domain_t)
236 ->order_by( $domain_t->column('web_hostname') );
237 #>>>
238 return $select;
239 }
240  
241 __PACKAGE__->meta()->make_immutable();
242  
243 1;
244  
245 # ABSTRACT: Represents a domain
246  
247 __END__
« Silki/HTML/FormatText.pm ↑ Summary Silki/Formatter/WikiToHTML.pm »