Bug 28416: Delay load of Email::Sender::Transport::SMTP
[koha.git] / Koha / SMTP / Server.pm
1 package Koha::SMTP::Server;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Koha::Database;
21 use Koha::Exceptions::Object;
22 use Koha::SMTP::Servers;
23
24 use base qw(Koha::Object);
25
26 =head1 NAME
27
28 Koha::SMTP::Server - Koha SMTP Server Object class
29
30 =head1 API
31
32 =head2 Class methods
33
34 =head3 transport
35
36     my $transport = $smtp_server->transport;
37     $email->transport($transport);
38     $email->send_or_die;
39
40 Returns an I<Email::Sender::Transport::SMTP> object that can be used directly
41 with Email::Sender.
42
43 =cut
44
45 sub transport {
46     my ($self) = @_;
47
48     my $params = {
49         host => $self->host,
50         port => $self->port,
51     };
52
53     $params->{ssl} = $self->ssl_mode
54         unless $self->ssl_mode eq 'disabled';
55
56     $params->{timeout} = $self->timeout
57         if $self->timeout;
58
59     $params->{sasl_username} = $self->user_name
60         if $self->user_name;
61
62     $params->{sasl_password} = $self->password
63         if $self->password;
64
65     $params->{debug} = $self->debug;
66
67     require Email::Sender::Transport::SMTP;
68     my $transport = Email::Sender::Transport::SMTP->new( $params );
69
70     return $transport;
71 }
72
73 =head3 libraries
74
75     my $libraries = $smtp_server->libraries
76
77 Accessor to get the list of libraries that are linked to this SMTP server
78
79 =cut
80
81 sub libraries {
82     my ($self) = @_;
83
84     my @library_ids = $self->_result->library_smtp_servers->get_column('library_id')->all;
85     return Koha::Libraries->search( { branchcode => { -in => \@library_ids } } );
86 }
87
88 =head3 is_system_default
89
90     if ( $smtp_server->is_system_default ) { ... }
91
92 Method that tells if a Koha::SMTP::Server is the hardcoded one.
93
94 =cut
95
96 sub is_system_default {
97     my ($self) = @_;
98
99     return $self->{_is_system_default};
100 }
101
102 =head3 to_api
103
104     my $json = $smtp_server->to_api;
105
106 Overloaded method that returns a JSON representation of the Koha::SMTP::Server object,
107 suitable for API output.
108
109 =cut
110
111 sub to_api {
112     my ( $self, $params ) = @_;
113
114     my $json = $self->SUPER::to_api( $params );
115     delete $json->{password};
116
117     return $json;
118 }
119
120 =head3 to_api_mapping
121
122 This method returns the mapping for representing a Koha::SMTP::Server object
123 on the API.
124
125 =cut
126
127 sub to_api_mapping {
128     return {
129         id => 'smtp_server_id'
130     };
131 }
132
133 =head2 Internal methods
134
135 =head3 _type
136
137 Return type of Object relating to Schema ResultSet
138
139 =cut
140
141 sub _type {
142     return 'SmtpServer';
143 }
144
145 1;