Passed
Pull Request — master (#5)
by ANTHONIUS
02:39
created

RemoteListener::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.216

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 2
cts 5
cp 0.4
crap 1.216
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace Doyo\Bridge\CodeCoverage\Listener;
5
6
7
use Doyo\Bridge\CodeCoverage\Event\CoverageEvent;
8
use Doyo\Bridge\CodeCoverage\Exception\HttpException;
9
use Doyo\Bridge\CodeCoverage\Http\ClientInterface;
10
use Doyo\Bridge\CodeCoverage\Session\SessionInterface;
11
12
class RemoteListener extends AbstractSessionListener
13
{
14
    /**
15
     * @var ClientInterface
16
     */
17
    private $httpClient;
18
19
    /**
20
     * @var array
21
     */
22
    private $config;
23
24
    /**
25
     * @var string
26
     */
27
    private $coverageUrl;
28
29
    /**
30
     * @var \Exception[]
31
     */
32
    private $exceptions;
33
34 5
    public function __construct(
35
        SessionInterface $session,
36
        ClientInterface $httpClient,
37
        string $coverageUrl,
38
        array $config
39
    )
40
    {
41 5
        parent::__construct($session);
42
43 5
        $this->httpClient = $httpClient;
44 5
        $this->coverageUrl = $coverageUrl;
45 5
        $this->config = $config;
46 5
        $this->exceptions = [];
47
    }
48
49 1
    public static function getSubscribedEvents()
50
    {
51
        return [
52 1
            CoverageEvent::refresh => 'refresh',
53
            CoverageEvent::complete => 'complete'
54
        ];
55
    }
56
57 3
    public function refresh()
58
    {
59 3
        $client = $this->httpClient;
60 3
        $session = $this->session;
61 3
        $body = json_encode($this->config);
62 3
        $coverageUrl = $this->coverageUrl;
63
64
        $options = [
65 3
            'body' => $body,
66
            'query' => [
67 3
                'action' => 'init',
68 3
                'session' => $session->getName()
69
            ]
70
        ];
71
72
        try{
73 3
            $client->request('POST', $coverageUrl, $options);
74 1
        }catch (\Exception $exception){
75 1
            $this->exceptions[] = $exception;
76
        }
77
    }
78
79 1
    public function complete(CoverageEvent $event)
80
    {
81 1
        $coverageUrl = $this->coverageUrl;
82 1
        $client = $this->httpClient;
83 1
        $session = $this->session;
84 1
        $consoleIO = $event->getConsoleIO();
85
86
        $options = [
87
            'query' => [
88 1
                'action' => 'read',
89 1
                'session' => $session->getName()
90
            ]
91
        ];
92
93
        try{
94
            /* @var SessionInterface $remoteSession */
95 1
            $response = $client->request('GET', $coverageUrl, $options);
96 1
            $remoteSession = $response->getBody()->getContents();
97 1
            $remoteSession = unserialize($remoteSession);
98 1
            $event->getProcessor()->merge($remoteSession->getProcessor());
99 1
        }catch (\Exception $exception){
100 1
            $this->exceptions[] = $exception;
101
        }
102
103 1
        $exceptions = array_merge($this->exceptions, $remoteSession->getExceptions());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $remoteSession does not seem to be defined for all execution paths leading up to this point.
Loading history...
104
105 1
        $ids = [];
106 1
        foreach($exceptions as $exception){
107 1
            $message = $exception->getMessage();
108 1
            $id = md5($message);
109 1
            if(!in_array($id,$ids)){
110 1
                $ids[] = $id;
111 1
                $consoleIO->coverageInfo($message);
112
            }
113
        }
114
    }
115
}
116