Bug 30007: Add Koha::Exceptions::SysPref
[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 Email::Sender::Transport::SMTP;
25
26 use base qw(Koha::Object);
27
28 =head1 NAME
29
30 Koha::SMTP::Server - Koha SMTP Server Object class
31
32 =head1 API
33
34 =head2 Class methods
35
36 =head3 transport
37
38     my $transport = $smtp_server->transport;
39     $email->transport($transport);
40     $email->send_or_die;
41
42 Returns an I<Email::Sender::Transport::SMTP> object that can be used directly
43 with Email::Sender.
44
45 =cut
46
47 sub transport {
48     my ($self) = @_;
49
50     my $params = {
51         host => $self->host,
52         port => $self->port,
53     };
54
55     $params->{ssl} = $self->ssl_mode
56         unless $self->ssl_mode eq 'disabled';
57
58     $params->{timeout} = $self->timeout
59         if $self->timeout;
60
61     $params->{sasl_username} = $self->user_name
62         if $self->user_name;
63
64     $params->{sasl_password} = $self->password
65         if $self->password;
66
67     $params->{debug} = $self->debug;
68
69     my $transport = Email::Sender::Transport::SMTP->new( $params );
70
71     return $transport;
72 }
73
74 =head3 libraries
75
76     my $libraries = $smtp_server->libraries
77
78 Accessor to get the list of libraries that are linked to this SMTP server
79
80 =cut
81
82 sub libraries {
83     my ($self) = @_;
84
85     my @library_ids = $self->_result->library_smtp_servers->get_column('library_id')->all;
86     return Koha::Libraries->search( { branchcode => { -in => \@library_ids } } );
87 }
88
89 =head3 is_system_default
90
91     if ( $smtp_server->is_system_default ) { ... }
92
93 Method that tells if a Koha::SMTP::Server is the hardcoded one.
94
95 =cut
96
97 sub is_system_default {
98     my ($self) = @_;
99
100     return $self->{_is_system_default};
101 }
102
103 =head3 to_api
104
105     my $json = $smtp_server->to_api;
106
107 Overloaded method that returns a JSON representation of the Koha::SMTP::Server object,
108 suitable for API output.
109
110 =cut
111
112 sub to_api {
113     my ( $self, $params ) = @_;
114
115     my $json = $self->SUPER::to_api( $params );
116     delete $json->{password};
117
118     return $json;
119 }
120
121 =head3 to_api_mapping
122
123 This method returns the mapping for representing a Koha::SMTP::Server object
124 on the API.
125
126 =cut
127
128 sub to_api_mapping {
129     return {
130         id => 'smtp_server_id'
131     };
132 }
133
134 =head2 Internal methods
135
136 =head3 _type
137
138 Return type of Object relating to Schema ResultSet
139
140 =cut
141
142 sub _type {
143     return 'SmtpServer';
144 }
145
146 1;