Completed
Push — master ( 3f4b39...daa8e9 )
by Kirill
10:57
created

HasSender   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setSender() 0 4 1
A getSender() 0 4 1
A send() 0 16 2
1
<?php declare(strict_types=1);
2
3
namespace Personnage\Tinkoff\SDK;
4
5
use Personnage\Tinkoff\SDK\Event\FailureEvent;
6
use Personnage\Tinkoff\SDK\Event\StartedEvent;
7
use Personnage\Tinkoff\SDK\Event\SuccessEvent;
8
use Personnage\Tinkoff\SDK\Exception\HttpException;
9
use Psr\Http\Message\RequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
12
trait HasSender
13
{
14
    use HasEvents;
15
16
    /**
17
     * @var Sender|null
18
     */
19
    private $sender;
20
21
    public function setSender(Sender $sender)
22
    {
23
        $this->sender = $sender;
24
    }
25
26
    public function getSender()
27
    {
28
        return $this->sender;
29
    }
30
31
    /**
32
     * Call http request and throw events.
33
     *
34
     * @param  RequestInterface $request
35
     *
36
     * @return ResponseInterface
37
     * @throws HttpException
38
     */
39
    protected function send(RequestInterface $request): ResponseInterface
40
    {
41
        $started = microtime(true);
42
43
        try {
44
            $this->fire(StartedEvent::class, [$request, $started]);
45
            $response = $this->sender->send($request);
46
            $this->fire(SuccessEvent::class, [$request, $response, $started, microtime(true)]);
47
48
            return $response;
49
        } catch (HttpException $e) {
50
            $this->fire(FailureEvent::class, [$request, $e, $started, microtime(true)]);
51
52
            throw $e;
53
        }
54
    }
55
}
56