Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
EmailUsersJob
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 run
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types=1 );
3namespace MediaWiki\Extension\CampaignEvents\Messaging;
4
5use InvalidArgumentException;
6use Job;
7use MailAddress;
8use UserMailer;
9
10class EmailUsersJob extends Job {
11
12    private MailAddress $to;
13    private string $subject;
14    private string $message;
15    private ?MailAddress $replyTo;
16    private MailAddress $from;
17
18    /**
19     * @param string $command
20     * @param array $params
21     * @phpcs:ignore Generic.Files.LineLength
22     * @phan-param array{to:array{0:string,1:?string,2:?string},subject:string,message:string,from:array{0:string,1:?string,2:?string},replyTo:?array{0:string,1:?string,2:?string}} $params
23     */
24    public function __construct( $command, array $params ) {
25        parent::__construct( $command, $params );
26        static $required = [ 'to', 'subject', 'message', 'from', 'replyTo' ];
27        $missing = implode( ', ', array_diff( $required, array_keys( $params ) ) );
28        if ( $missing !== '' ) {
29            throw new InvalidArgumentException( "Missing parameter(s) $missing" );
30        }
31        $this->removeDuplicates = true;
32        $this->command = $command;
33        $this->subject = $params['subject'];
34        $this->message = $params['message'];
35        $this->to = new MailAddress( ...$params['to'] );
36        $this->from = new MailAddress( ...$params['from'] );
37        $this->replyTo = $params['replyTo'] ? new MailAddress( ...$params['replyTo'] ) : null;
38    }
39
40    public function run(): bool {
41        $opts = $this->replyTo !== null
42            ? [ 'replyTo' => $this->replyTo ]
43            : [];
44        $status = UserMailer::send(
45            $this->to,
46            $this->from,
47            $this->subject,
48            $this->message,
49            $opts
50        );
51        return $status->isGood();
52    }
53}