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

DebugTransport::setDebugPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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