Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Mailer
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
42
 getCarriers
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 text
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3/**
4 * VuFind Mailer Class for SMS messages
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2009.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  SMS
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\SMS;
31
32use VuFind\Exception\SMS as SMSException;
33
34use function count;
35use function in_array;
36
37/**
38 * VuFind Mailer Class for SMS messages
39 *
40 * @category VuFind
41 * @package  SMS
42 * @author   Demian Katz <demian.katz@villanova.edu>
43 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
44 * @link     https://vufind.org/wiki/development Wiki
45 */
46class Mailer extends AbstractBase
47{
48    /**
49     * Default carriers, usually overridden by contents of web/conf/sms.ini.
50     *
51     * @var array
52     */
53    protected $carriers = [
54        'virgin' => ['name' => 'Virgin Mobile', 'domain' => 'vmobl.com'],
55        'att' => ['name' => 'AT&T', 'domain' => 'mms.att.net'],
56        'verizon' => ['name' => 'Verizon', 'domain' => 'vtext.com'],
57        'nextel' => ['name' => 'Nextel', 'domain' => 'messaging.nextel.com'],
58        'sprint' => ['name' => 'Sprint', 'domain' => 'messaging.sprintpcs.com'],
59        'tmobile' => ['name' => 'T Mobile', 'domain' => 'tmomail.net'],
60        'alltel' => ['name' => 'Alltel', 'domain' => 'message.alltel.com'],
61        'Cricket' => ['name' => 'Cricket', 'domain' => 'mms.mycricket.com'],
62    ];
63
64    /**
65     * Default "from" address
66     *
67     * @var string
68     */
69    protected $defaultFrom;
70
71    /**
72     * VuFind Mailer object
73     *
74     * @var \VuFind\Mailer\Mailer
75     */
76    protected $mailer;
77
78    /**
79     * Constructor
80     *
81     * @param \Laminas\Config\Config $config  SMS configuration
82     * @param array                  $options Additional options: defaultFrom
83     * (optional) and mailer (must be a \VuFind\Mailer\Mailer object)
84     */
85    public function __construct(\Laminas\Config\Config $config, $options = [])
86    {
87        // Set up parent object first:
88        parent::__construct($config);
89
90        // If found, use carriers from SMS configuration; otherwise, fall back to the
91        // default list of US carriers.
92        if (isset($config->Carriers) && count($config->Carriers) > 0) {
93            $this->carriers = [];
94            foreach ($config->Carriers as $id => $settings) {
95                [$domain, $name] = explode(':', $settings, 2);
96                $this->carriers[$id] = ['name' => $name, 'domain' => $domain];
97            }
98        }
99
100        // Load default "from" address:
101        $this->defaultFrom
102            = $options['defaultFrom'] ?? '';
103
104        // Make sure mailer dependency has been injected:
105        if (
106            !isset($options['mailer'])
107            || !($options['mailer'] instanceof \VuFind\Mailer\Mailer)
108        ) {
109            throw new \Exception(
110                '$options["mailer"] must be a \VuFind\Mailer\Mailer'
111            );
112        }
113        $this->mailer = $options['mailer'];
114    }
115
116    /**
117     * Get a list of carriers supported by the module. Returned as an array of
118     * associative arrays indexed by carrier ID and containing "name" and "domain"
119     * keys.
120     *
121     * @return array
122     */
123    public function getCarriers()
124    {
125        return $this->carriers;
126    }
127
128    /**
129     * Send a text message to the specified provider.
130     *
131     * @param string $provider The provider ID to send to
132     * @param string $to       The phone number at the provider
133     * @param string $from     The email address to use as sender
134     * @param string $message  The message to send
135     *
136     * @throws \VuFind\Exception\SMS
137     * @return void
138     */
139    public function text($provider, $to, $from, $message)
140    {
141        $knownCarriers = array_keys($this->carriers);
142        if (empty($provider) || !in_array($provider, $knownCarriers)) {
143            throw new SMSException(
144                'Unknown Carrier',
145                SMSException::ERROR_UNKNOWN_CARRIER
146            );
147        }
148
149        $to = $this->filterPhoneNumber($to)
150            . '@' . $this->carriers[$provider]['domain'];
151        $from = empty($from) ? $this->defaultFrom : $from;
152        $subject = '';
153        return $this->mailer->send($to, $from, $subject, $message);
154    }
155}