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

ExceptionResponder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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