Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
81.25% |
13 / 16 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| PortTransfer | |
81.25% |
13 / 16 |
|
50.00% |
2 / 4 |
5.16 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| connectOnly | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| send | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
1.00 | |||
| end | |
66.67% |
4 / 6 |
|
0.00% |
0 / 1 |
1.04 | |||
| 1 | <?php |
| 2 | namespace Apie\FtpServer\Transfers; |
| 3 | |
| 4 | use React\Promise\PromiseInterface; |
| 5 | use React\Socket\ConnectionInterface; |
| 6 | use React\Socket\ConnectorInterface; |
| 7 | |
| 8 | class PortTransfer implements TransferInterface |
| 9 | { |
| 10 | private PromiseInterface $connectComplete; |
| 11 | |
| 12 | public function __construct( |
| 13 | private readonly ConnectorInterface $connector, |
| 14 | private readonly string $ip, |
| 15 | private readonly int $port |
| 16 | ) { |
| 17 | } |
| 18 | |
| 19 | public function connectOnly(): PromiseInterface |
| 20 | { |
| 21 | if (!isset($this->connectComplete)) { |
| 22 | $this->connectComplete = $this->connector->connect($this->ip . ':' . $this->port); |
| 23 | } |
| 24 | return $this->connectComplete; |
| 25 | } |
| 26 | |
| 27 | public function send(string $data, ?callable $onRejected = null): void |
| 28 | { |
| 29 | |
| 30 | $this->connectOnly()->then( |
| 31 | function (ConnectionInterface $connection) use ($data) { |
| 32 | $connection->write($data); |
| 33 | }, |
| 34 | $onRejected |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | public function end(): void |
| 39 | { |
| 40 | $this->connectOnly()->then( |
| 41 | function (ConnectionInterface $connection) { |
| 42 | $connection->end(); |
| 43 | unset($this->connectComplete); |
| 44 | } |
| 45 | ); |
| 46 | } |
| 47 | } |