Test Failed
Push — master ( a9de71...77d308 )
by Artem
01:39
created

DebugTransport   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 68
ccs 15
cts 20
cp 0.75
rs 10
c 3
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A send() 0 13 3
A getPath() 0 3 1
A putContents() 0 3 1
A __construct() 0 7 2
A getTimestamp() 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
            $path = realpath(__DIR__ . '/../../tests/data');
22
        }
23
24 2
        $this->path = $path;
25 2
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30 2
    public function send(AbstractMessage $message): void
31
    {
32
        try {
33 2
            $text = $message->render();
34 2
            $address = $message->getAddress();
35
36 2
            $filename = $this->getPath() . '/' . $address->__toString() . '_' . $this->getTimestamp() . '.txt';
37
38 2
            if (! $this->putContents($filename, $text)) {
39 2
                throw new \RuntimeException('Unable to write data');
40
            }
41 1
        } catch (\Exception $exception) {
42 1
            throw new TransportException('Unable to send message', 1, $exception);
43
        }
44 1
    }
45
46
    /**
47
     * putContents wrapper
48
     *
49
     * @access	protected
50
     * @param	string	$filename	
51
     * @param	mixed 	$content 	
52
     * @return	int|bool
53
     */
54
    protected function putContents(string $filename, $content)
55
    {
56
        return file_put_contents($filename, $content);
57
    }
58
59
    /**
60
     * getTimestamp.
61
     *
62
     * @access	protected
63
     * @return	string
64
     */
65
    protected function getTimestamp(): string
66
    {
67
        return (string) strtotime('now');
68
    }
69
70
    /**
71
     * getPath.
72
     *
73
     * @access	protected
74
     * @return	string
75
     */
76 2
    protected function getPath(): string
77
    {
78 2
        return (string) $this->path;
79
    }
80
}
81