Completed
Push — develop ( f11aab...a4697b )
by Alejandro
09:31
created

MockTransport::send()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace AcMailerTest\Mail\Transport;
3
4
use Zend\Mail\Transport\TransportInterface;
5
use Zend\Mail\Message;
6
use Zend\Mail\Transport\Exception\RuntimeException;
7
8
/**
9
 * Mocks mail transport
10
 * @author Alejandro Celaya Alastrué
11
 * @link http://www.alejandrocelaya.com
12
 */
13
class MockTransport implements TransportInterface
14
{
15
    const ERROR_MESSAGE = 'This is a forced exception';
16
    
17
    /**
18
     * @var boolean
19
     */
20
    private $forceError = false;
21
    /**
22
     * @var \Exception
23
     */
24
    private $exeption = null;
25
    
26
    public function send(Message $message)
27
    {
28
        if ($this->forceError) {
29
            throw $this->exeption;
30
        }
31
    }
32
    
33
    /**
34
     * If force error is set to true, the method send will throw a RuntimeException when is called
35
     * @param boolean $forceError
36
     * @param \Exception|null $exception
37
     */
38
    public function setForceError($forceError, $exception = null)
39
    {
40
        $this->forceError = $forceError;
41
        $this->exeption = $exception instanceof \Exception
42
            ? $exception
43
            : new RuntimeException(self::ERROR_MESSAGE, -1);
44
    }
45
46
    /**
47
     * Tells if a RuntimeException will be thrown when the method send is called
48
     * @return boolean
49
     */
50
    public function isForceError()
51
    {
52
        return $this->forceError;
53
    }
54
}
55