Completed
Pull Request — master (#392)
by
unknown
01:27
created

DrupalMailManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 82
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A enableTestMailBackend() 0 7 2
A restoreInitialMailBackend() 0 3 1
A startCollectingMail() 0 4 1
A stopCollectingMail() 0 3 1
A enableMail() 0 3 1
A disableMail() 0 3 1
A getMail() 0 3 1
A clearMail() 0 3 1
1
<?php
2
3
namespace Drupal;
4
5
use Drupal\Driver\DriverInterface;
6
7
/**
8
 * Default implementation of the Drupal mail manager service.
9
 * 
10
 * This uses Drupal core's test_mail_collector mail backend, which both
11
 * collects outbound mail and prevents it from being sent. Therefore using
12
 * this implementation, mail is collected if and only if sending is disabled.
13
 */
14
class DrupalMailManager implements DrupalMailManagerInterface {
15
16
  /**
17
   * The active Drupal driver.
18
   *
19
   * @var \Drupal\Driver\DriverInterface
20
   */
21
  protected $driver;
22
23
  /**
24
   * The name or config array of the initial mail backend.
25
   *
26
   * @var mixed
27
   */
28
  protected $initialMailBackend;
29
  
30
  public function __construct(DriverInterface $driver) {
31
    $this->driver = $driver;
32
  }
33
34
  /**
35
   * Replace the initial mail backend with the test mail backend.
36
   */
37
  protected function enableTestMailBackend() {
38
    if (is_null($this->initialMailBackend)) {
39
      $this->initialMailBackend = $this->driver->getMailBackend();
0 ignored issues
show
Bug introduced by
The method getMailBackend() does not seem to exist on object<Drupal\Driver\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
    }
41
    // @todo Use a collector that supports html after D#2223967 lands.
42
    $this->driver->setMailBackend('test_mail_collector');
0 ignored issues
show
Bug introduced by
The method setMailBackend() does not seem to exist on object<Drupal\Driver\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
  }
44
45
  /**
46
   * Restore the initial mail backend.
47
   */
48
  protected function restoreInitialMailBackend() {
49
    $this->driver->setMailBackend($this->initialMailBackend);
0 ignored issues
show
Bug introduced by
The method setMailBackend() does not seem to exist on object<Drupal\Driver\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function startCollectingMail() {
56
    $this->enableTestMailBackend();
57
    $this->clearMail();
58
  }
59
60
  /**
61
   * {@inheritdoc}
62
   */
63
  public function stopCollectingMail() {
64
    $this->restoreInitialMailBackend();
65
  }
66
  
67
  /**
68
   * {@inheritdoc}
69
   */
70
  public function enableMail() {
71
    $this->restoreInitialMailBackend();
72
  }
73
74
  /**
75
   * {@inheritdoc}
76
   */
77
  public function disableMail() {
78
    $this->startCollectingMail();
79
  }
80
  
81
  /**
82
   * {@inheritdoc}
83
   */
84
  public function getMail($store = 'default') {
85
    return $this->driver->getMail();
0 ignored issues
show
Bug introduced by
The method getMail() does not seem to exist on object<Drupal\Driver\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91
  public function clearMail($store = 'default') {
92
    $this->driver->clearMail();
0 ignored issues
show
Bug introduced by
The method clearMail() does not seem to exist on object<Drupal\Driver\DriverInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
  }
94
95
}
96