RemoteListener::complete()   A
last analyzed

Complexity

Conditions 4
Paths 15

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.25

Importance

Changes 0
Metric Value
cc 4
eloc 23
nc 15
nop 1
dl 0
loc 33
ccs 21
cts 28
cp 0.75
crap 4.25
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Bridge\CodeCoverage\Listener;
15
16
use Doyo\Bridge\CodeCoverage\Event\CoverageEvent;
17
use Doyo\Bridge\CodeCoverage\Http\ClientInterface;
18
use Doyo\Bridge\CodeCoverage\Session\SessionInterface;
19
20
class RemoteListener extends AbstractSessionListener
21
{
22
    /**
23
     * @var ClientInterface
24
     */
25
    private $httpClient;
26
27
    /**
28
     * @var array
29
     */
30
    private $config;
31
32
    /**
33
     * @var string
34
     */
35
    private $coverageUrl;
36
37
    /**
38
     * @var \Exception[]
39
     */
40
    private $exceptions;
41
42 5
    public function __construct(
43
        SessionInterface $session,
44
        ClientInterface $httpClient,
45
        string $coverageUrl,
46
        array $config
47
    ) {
48 5
        parent::__construct($session);
49
50 5
        $this->httpClient  = $httpClient;
51 5
        $this->coverageUrl = $coverageUrl;
52 5
        $this->config      = $config;
53 5
        $this->exceptions  = [];
54
    }
55
56 1
    public static function getSubscribedEvents()
57
    {
58
        return [
59 1
            CoverageEvent::refresh  => 'refresh',
60
            CoverageEvent::complete => 'complete',
61
        ];
62
    }
63
64 3
    public function refresh()
65
    {
66 3
        $client      = $this->httpClient;
67 3
        $session     = $this->session;
68 3
        $body        = json_encode($this->config);
69 3
        $coverageUrl = $this->coverageUrl;
70
71
        $options = [
72 3
            'body'  => $body,
73
            'query' => [
74 3
                'action'  => 'init',
75 3
                'session' => $session->getName(),
76
            ],
77
        ];
78
79
        try {
80 3
            $client->request('POST', $coverageUrl, $options);
81 1
        } catch (\Exception $exception) {
82 1
            $this->exceptions[] = $exception;
83
        }
84
    }
85
86 1
    public function complete(CoverageEvent $event)
87
    {
88 1
        $coverageUrl = $this->coverageUrl;
89 1
        $client      = $this->httpClient;
90 1
        $session     = $this->session;
91 1
        $consoleIO   = $event->getConsoleIO();
92
93
        $options = [
94
            'query' => [
95 1
                'action'  => 'read',
96 1
                'session' => $session->getName(),
97
            ],
98
        ];
99
100
        try {
101
            /** @var SessionInterface $remoteSession */
102 1
            $response      = $client->request('GET', $coverageUrl, $options);
103 1
            $remoteSession = $response->getBody()->getContents();
104 1
            $remoteSession = unserialize($remoteSession);
105 1
            $event->getProcessor()->merge($remoteSession->getProcessor());
106 1
        } catch (\Exception $exception) {
107 1
            $this->exceptions[] = $exception;
108
        }
109
110 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...
111
112 1
        $ids = [];
113 1
        foreach ($exceptions as $exception) {
114 1
            $message = $exception->getMessage();
115 1
            $id      = md5($message);
116 1
            if (!\in_array($id, $ids, true)) {
117 1
                $ids[] = $id;
118 1
                $consoleIO->coverageInfo($message);
119
            }
120
        }
121
    }
122
}
123