Bug 33974: (QA follow-up) Remove superflous import
[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 store
35
36     $server->store;
37
38 Overloaded store method.
39
40 =cut
41
42 sub store {
43     my ($self) = @_;
44
45     $self->_result->result_source->schema->txn_do(
46         sub {
47             Koha::SMTP::Servers->search->update( { is_default => 0 },
48                 { no_triggers => 1 } )
49               if $self->is_default;
50
51             $self = $self->SUPER::store;
52         }
53     );
54
55     return $self;
56 }
57
58 =head3 transport
59
60     my $transport = $smtp_server->transport;
61     $email->transport($transport);
62     $email->send_or_die;
63
64 Returns an I<Email::Sender::Transport::SMTP> object that can be used directly
65 with Email::Sender.
66
67 =cut
68
69 sub transport {
70     my ($self) = @_;
71
72     my $params = {
73         host => $self->host,
74         port => $self->port,
75     };
76
77     $params->{ssl} = $self->ssl_mode
78         unless $self->ssl_mode eq 'disabled';
79
80     $params->{timeout} = $self->timeout
81         if $self->timeout;
82
83     $params->{sasl_username} = $self->user_name
84         if $self->user_name;
85
86     $params->{sasl_password} = $self->password
87         if $self->password;
88
89     $params->{debug} = $self->debug;
90
91     require Email::Sender::Transport::SMTP;
92     my $transport = Email::Sender::Transport::SMTP->new( $params );
93
94     return $transport;
95 }
96
97 =head3 libraries
98
99     my $libraries = $smtp_server->libraries
100
101 Accessor to get the list of libraries that are linked to this SMTP server
102
103 =cut
104
105 sub libraries {
106     my ($self) = @_;
107
108     my @library_ids = $self->_result->library_smtp_servers->get_column('library_id')->all;
109     return Koha::Libraries->search( { branchcode => { -in => \@library_ids } } );
110 }
111
112 =head3 is_system_default
113
114     if ( $smtp_server->is_system_default ) { ... }
115
116 Method that tells if a Koha::SMTP::Server is the hardcoded one.
117
118 =cut
119
120 sub is_system_default {
121     my ($self) = @_;
122
123     return $self->{_is_system_default};
124 }
125
126 =head3 to_api
127
128     my $json = $smtp_server->to_api;
129
130 Overloaded method that returns a JSON representation of the Koha::SMTP::Server object,
131 suitable for API output.
132
133 =cut
134
135 sub to_api {
136     my ( $self, $params ) = @_;
137
138     my $json = $self->SUPER::to_api( $params );
139     delete $json->{password};
140
141     return $json;
142 }
143
144 =head3 to_api_mapping
145
146 This method returns the mapping for representing a Koha::SMTP::Server object
147 on the API.
148
149 =cut
150
151 sub to_api_mapping {
152     return {
153         id => 'smtp_server_id'
154     };
155 }
156
157 =head2 Internal methods
158
159 =head3 _type
160
161 Return type of Object relating to Schema ResultSet
162
163 =cut
164
165 sub _type {
166     return 'SmtpServer';
167 }
168
169 1;