Test Failed
Push — master ( 9507e6...8ad776 )
by Artem
01:32
created

DebugTransport::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
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
    public function __construct(string $path = null)
19
    {
20
        if (empty($path)) {
21
            $path = realpath(__DIR__ . '/../../tests/data');
22
        }
23
24
        $this->path = $path;
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function send(AbstractMessage $message): void
31
    {
32
        try {
33
            $text = $message->render();
34
            $address = $message->getAddress();
35
36
            $filename = $this->getPath() . '/' . $address->__toString() . '_' . $this->getTimestamp() . '.txt';
37
38
            $test = $this->putContents($filename, $text);
39
40
            var_dump($test);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($test) looks like debug code. Are you sure you do not want to remove it?
Loading history...
41
42
            if (! $test) {
43
                throw new \RuntimeException('Unable to write data');
44
            }
45
        } catch (\Exception $exception) {
46
            throw new TransportException('Unable to send message', 1, $exception);
47
        }
48
    }
49
50
    /**
51
     * putContents wrapper
52
     *
53
     * @access	protected
54
     * @param	string	$filename	
55
     * @param	mixed 	$content 	
56
     * @return	int|bool
57
     */
58
    protected function putContents(string $filename, $content)
59
    {
60
        return file_put_contents($filename, $content);
61
    }
62
63
    /**
64
     * getTimestamp.
65
     *
66
     * @access	protected
67
     * @return	string
68
     */
69
    protected function getTimestamp(): string
70
    {
71
        return (string) strtotime('now');
72
    }
73
74
    /**
75
     * getPath.
76
     *
77
     * @access	protected
78
     * @return	string
79
     */
80
    protected function getPath(): string
81
    {
82
        return (string) $this->path;
83
    }
84
}
85