GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SendStyledEmail::doExecute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Drupal\df_tools_message\Plugin\RulesAction;
4
5
use Drupal\Core\Language\LanguageInterface;
6
use Drupal\Core\Mail\MailManagerInterface;
7
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8
use Drupal\rules\Core\RulesActionBase;
9
use Psr\Log\LoggerInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
13
/**
14
 * Provides "Send styled email" rules action.
15
 *
16
 * @RulesAction(
17
 *   id = "df_tools_message_send_styled_email",
18
 *   label = @Translation("Send styled email"),
19
 *   category = @Translation("System"),
20
 *   context = {
21
 *     "to" = @ContextDefinition("email",
22
 *       label = @Translation("Send to"),
23
 *       description = @Translation("Email address drupal will send an email to."),
24
 *     ),
25
 *     "subject" = @ContextDefinition("string",
26
 *       label = @Translation("Subject"),
27
 *       description = @Translation("The email's subject."),
28
 *     ),
29
 *     "message" = @ContextDefinition("string",
30
 *       label = @Translation("Message"),
31
 *       description = @Translation("The email's message body. If you want any non-default styling, use inline styles."),
32
 *     ),
33
 *     "format" = @ContextDefinition("string",
34
 *       label = @Translation("Format"),
35
 *       description = @Translation("The email's message text format."),
36
 *       default_value = "rich_text",
37
 *     )
38
 *   }
39
 * )
40
 */
41
class SendStyledEmail extends RulesActionBase implements ContainerFactoryPluginInterface {
42
43
  /**
44
   * The logger channel the action will write log messages to.
45
   *
46
   * @var \Psr\Log\LoggerInterface
47
   */
48
  protected $logger;
49
50
  /**
51
   * @var \Drupal\Core\Mail\MailManagerInterface
52
   */
53
  protected $mailManager;
54
55
  /**
56
   * Constructs a SendEmail object.
57
   *
58
   * @param array $configuration
59
   *   A configuration array containing information about the plugin instance.
60
   * @param string $plugin_id
61
   *   The plugin ID for the plugin instance.
62
   * @param mixed $plugin_definition
63
   *   The plugin implementation definition.
64
   * @param \Psr\Log\LoggerInterface $logger
65
   *   The alias storage service.
66
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
67
   *   The mail manager service.
68
   */
69
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, MailManagerInterface $mail_manager) {
70
    parent::__construct($configuration, $plugin_id, $plugin_definition);
71
    $this->logger = $logger;
72
    $this->mailManager = $mail_manager;
73
  }
74
75
  /**
76
   * {@inheritdoc}
77
   */
78
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
79
    return new static(
80
      $configuration,
81
      $plugin_id,
82
      $plugin_definition,
83
      $container->get('logger.factory')->get('rules'),
84
      $container->get('plugin.manager.mail')
0 ignored issues
show
Bug introduced by
It seems like $container->get('plugin.manager.mail') can also be of type null; however, parameter $mail_manager of Drupal\df_tools_message\...ledEmail::__construct() does only seem to accept Drupal\Core\Mail\MailManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
      /** @scrutinizer ignore-type */ $container->get('plugin.manager.mail')
Loading history...
85
    );
86
  }
87
88
  /**
89
   * Send a styled email.
90
   *
91
   * @param string $to
92
   *   Email address of the recipient.
93
   * @param string $subject
94
   *   Subject of the email.
95
   * @param string $message
96
   *   Email message text.
97
   * @param string $format
98
   *   Email message text format.
99
   */
100
  protected function doExecute($to, $subject, $message, $format) {
101
    $params = [
102
      'subject' => $subject,
103
      'message' => $message,
104
      'format' => $format,
105
    ];
106
    // Set a unique key for this mail.
107
    $key = 'rules_action_mail_' . $this->getPluginId();
108
    $message = $this->mailManager->mail('df_tools_message', $key, $to, LanguageInterface::LANGCODE_NOT_SPECIFIED, $params);
109
    if ($message['result']) {
110
      $this->logger->notice('Successfully sent styled email to %recipient', ['%recipient' => $to]);
111
    }
112
  }
113
114
}
115