Completed
Pull Request — master (#23)
by Erin
03:28
created

ExceptionResponder::respond()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 10
rs 9.4285
cc 3
eloc 6
nc 3
nop 3
1
<?php
2
namespace Peridot\Leo\Responder;
3
4
use Exception;
5
use Peridot\Leo\Formatter\FormatterInterface;
6
use Peridot\Leo\Matcher\Match;
7
use Peridot\Leo\Matcher\Template\TemplateInterface;
8
use Peridot\Leo\Responder\Exception\AssertionException;
9
10
/**
11
 * The ExceptionResponder responds to a match by throwing an exception
12
 * on a failed match.
13
 *
14
 * @package Peridot\Leo\Responder
15
 */
16
class ExceptionResponder implements ResponderInterface
17
{
18
    /**
19
     * @var FormatterInterface
20
     */
21
    protected $formatter;
22
23
    /**
24
     * @param FormatterInterface $formatter
25
     */
26
    public function __construct(FormatterInterface $formatter)
27
    {
28
        $this->formatter = $formatter;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * Throws an exception containing the formatted message.
35
     *
36
     * @param Match $match
37
     * @param TemplateInterface $template
38
     * @param string $message
39
     * @return void
40
     * @throws Exception
41
     */
42
    public function respond(Match $match, TemplateInterface $template, $message = "")
43
    {
44
        if ($match->isMatch()) {
45
            return;
46
        }
47
48
        $this->formatter->setMatch($match);
49
        $message = ($message) ? $message : $this->formatter->getMessage($template);
50
        throw new AssertionException($message);
51
    }
52
}
53