Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationNotificationsEmailJob
0.00% covered (danger)
0.00%
0 / 52
0.00% covered (danger)
0.00%
0 / 7
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
 addressFromUser
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 buildAddress
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getMailAddress
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 validateParams
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 validateEmail
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/*
3 * @file
4 * @license GPL-2.0-or-later
5 */
6
7namespace MediaWiki\Extension\TranslationNotifications\Jobs;
8
9use InvalidArgumentException;
10use MailAddress;
11use MediaWiki\Title\Title;
12use MediaWiki\User\User;
13use UserMailer;
14
15/**
16 * Uses UserMailer to send out emails.
17 *
18 * @ingroup JobQueue
19 * @since 2020.02
20 */
21class TranslationNotificationsEmailJob extends GenericTranslationNotificationsJob {
22    public function __construct( Title $title, array $params ) {
23        parent::__construct( 'TranslationNotificationsEmailJob', $title, $params );
24        $this->validateParams( $params );
25    }
26
27    /**
28     * Execute the job
29     * @return bool
30     */
31    public function run() {
32        $this->logDebug( 'Starting execution...' );
33        $to = $this->getMailAddress( $this->params['to'] );
34        $from = $this->getMailAddress( $this->params['from'] );
35        $replyTo = $this->getMailAddress( $this->params['replyTo'] );
36        $subject = $this->params['subject'];
37
38        $status = UserMailer::send(
39            $to,
40            $from,
41            $subject,
42            $this->params['body'] ?? '',
43            [ 'replyTo' => $replyTo ]
44        );
45
46        if ( !$status->isOK() ) {
47            $errorMsg = $status->getMessage()->text();
48            $this->logError( $errorMsg );
49            $this->setLastError( $errorMsg );
50            return false;
51        }
52
53        $this->logInfo( 'Sent email to user regarding: ' . $subject );
54        return true;
55    }
56
57    public static function addressFromUser( User $user ): array {
58        return [
59            'email' => $user->getEmail(),
60            'name' => $user->getName(),
61            'fullName' => $user->getRealName()
62        ];
63    }
64
65    public static function buildAddress( string $email, string $name, ?string $fullName ): array {
66        return [
67            'email' => $email,
68            'name' => $name,
69            'fullName' => $fullName ?? ''
70        ];
71    }
72
73    private function getMailAddress( array $address ): MailAddress {
74        return new MailAddress(
75            $address['email'],
76            $address['name'],
77            $address['fullName']
78        );
79    }
80
81    private function validateParams( array $params ): void {
82        $from = $params[ 'from' ] ?? [];
83        $to = $params[ 'to' ] ?? [];
84        $replyTo = $params['replyTo'] ?? [];
85        $subject = $params['subject'] ?? false;
86
87        $this->validateEmail( $from, 'from' );
88        $this->validateEmail( $to, 'to' );
89        $this->validateEmail( $replyTo, 'replyTo' );
90
91        if ( !is_string( $subject ) || strlen( $subject ) === 0 ) {
92            throw new InvalidArgumentException( 'Parameter "subject" must be a non-empty string.' );
93        }
94    }
95
96    private function validateEmail( array $address, string $propName ): void {
97        $requiredProps = [ 'email', 'name' ];
98        foreach ( $requiredProps as $prop ) {
99            $propVal = $address[ $prop ] ?? false;
100            if ( !is_string( $propVal ) || strlen( $propVal ) === 0 ) {
101                throw new InvalidArgumentException(
102                    "Parameter: $propName must contain " . implode( ", ", $requiredProps )
103                );
104            }
105        }
106    }
107}