Passed
Push — master ( 4d4163...a7ef67 )
by Artem
03:02
created

DebugTransport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 42
ccs 13
cts 15
cp 0.8667
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A send() 0 11 2
A getPath() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prozorov\DataVerification\Transport;
6
7
use Prozorov\DataVerification\Contracts\TransportInterface;
8
use Prozorov\DataVerification\Messages\AbstractMessage;
9
use Prozorov\DataVerification\Exceptions\TransportException;
10
11
class DebugTransport implements TransportInterface
12
{
13
    /**
14
     * @var string $path
15
     */
16
    protected $path;
17
18 2
    public function __construct(string $path = null)
19
    {
20 2
        if (empty($path)) {
21 2
            $path = realpath(__DIR__ . '/../../tests/data');
22
        }
23
24 2
        $this->path = $path;
25 2
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30 1
    public function send(AbstractMessage $message): void
31
    {
32
        try {
33 1
            $text = $message->render();
34 1
            $address = $message->getAddress();
35
36 1
            $filename = $this->getPath() . '/' . $address->__toString() . '_' . strtotime('now') . '.txt';
37
38 1
            file_put_contents($filename, $text);
39
        } catch (\Exception $exception) {
40
            throw new TransportException('Unable to send message', 1, $exception);
41
        }
42 1
    }
43
44
    /**
45
     * getPath.
46
     *
47
     * @access	protected
48
     * @return	string
49
     */
50 1
    protected function getPath(): string
51
    {
52 1
        return (string) $this->path;
53
    }
54
}
55