Completed
Branch 0.4-dev (79cc15)
by Evgenij
03:32
created

TooSlowRecvException::tooSlowDataReceiving()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 3
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\Exception;
11
12
use AsyncSockets\Socket\SocketInterface;
13
14
/**
15
 * Class TooSlowRecvException. Thrown when transfer speed is slow then given in socket setup
16
 */
17
class TooSlowRecvException extends RecvDataException
18
{
19
    /**
20
     * Speed at the moment of exception in bytes per second
21
     *
22
     * @var double
23
     */
24
    private $speed;
25
26
    /**
27
     * Duration of low speed in seconds
28
     *
29
     * @var int
30
     */
31
    private $duration;
32
33
    /**
34
     * {@inheritDoc}
35
     */
36 1
    public function __construct(
37
        SocketInterface $socket,
38
        $speed,
39
        $duration,
40
        $message = '',
41
        $code = 0,
42
        \Exception $previous = null
43
    ) {
44 1
        parent::__construct($socket, $message, $code, $previous);
45 1
        $this->speed    = $speed;
46 1
        $this->duration = $duration;
47 1
    }
48
49
    /**
50
     * tooSlowDataReceiving
51
     *
52
     * @param SocketInterface $socket Socket
53
     * @param double          $speed Speed at the moment of exception in bytes per second
54
     * @param int             $duration Duration of low speed in seconds
55
     *
56
     * @return TooSlowRecvException
57
     */
58 1
    public static function tooSlowDataReceiving(SocketInterface $socket, $speed, $duration)
59
    {
60 1
        return new self(
61
            $socket,
62
            $speed,
63
            $duration,
64 1
            'Data transfer is going to be aborted because of too slow speed.'
65
        );
66
    }
67
68
    /**
69
     * Return speed at the moment of exception in bytes per second
70
     *
71
     * @return float
72
     */
73
    public function getSpeed()
74
    {
75
        return $this->speed;
76
    }
77
78
    /**
79
     * Return duration of low speed in seconds
80
     *
81
     * @return int
82
     */
83
    public function getDuration()
84
    {
85
        return $this->duration;
86
    }
87
}
88