BrowserActionCollector   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 106
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A collect() 0 19 1
A processPerformanceLog() 0 35 4
A pushAction() 0 14 2
A pushPerformanceLog() 0 20 1
1
<?php
2
3
namespace BeyondCode\DuskDashboard;
4
5
use BeyondCode\DuskDashboard\Console\StartDashboardCommand;
6
use BeyondCode\DuskDashboard\Dusk\Browser;
7
use GuzzleHttp\Client;
8
use GuzzleHttp\RequestOptions;
9
10
class BrowserActionCollector
11
{
12
    /** @var Client */
13
    protected $client;
14
15
    protected $testName;
16
17
    public function __construct($testName)
18
    {
19
        $this->testName = $testName;
20
21
        $this->client = new Client();
22
    }
23
24
    public function collect(string $action, array $arguments, Browser $browser, string $previousHtml = null)
25
    {
26
        $path = parse_url($browser->driver->getCurrentURL(), PHP_URL_PATH) ?? '';
27
28
        $action = new Action($action, $arguments, $browser->getCurrentPageSource(), $path);
29
30
        $action->setPreviousHtml($previousHtml);
31
32
        $this->pushAction('dusk-event', [
33
            'test' => $this->testName,
34
            'path' => $action->getPath(),
35
            'name' => $action->getName(),
36
            'arguments' => $action->getArguments(),
37
            'before' => $action->getPreviousHtml(),
38
            'html' => $action->getHtml(),
39
        ]);
40
41
        $this->processPerformanceLog($browser);
42
    }
43
44
    protected function processPerformanceLog(Browser $browser)
45
    {
46
        $logs = collect([]);
47
48
        try {
49
            $logs = collect($browser->driver->manage()->getLog('performance'));
50
        } catch (\Exception $e) {
51
            // performance logging might be disabled.
52
        }
53
54
        $allowedMethods = [
55
            'Network.requestWillBeSent',
56
            'Network.responseReceived',
57
        ];
58
59
        $logs
60
            ->map(function ($log) {
61
                return json_decode($log['message']);
62
            })
63
            ->filter(function ($log) use ($allowedMethods) {
64
                $method = data_get($log, 'message.method');
65
66
                $type = data_get($log, 'message.params.type');
67
68
                return in_array($method, $allowedMethods) && $type === 'XHR';
69
            })->groupBy(function ($log) {
70
                if (data_get($log, 'message.method') === 'Network.requestWillBeSent') {
71
                    return data_get($log, 'message.requestId');
72
                }
73
74
                return data_get($log, 'params.requestId');
75
            })->map(function ($log) use ($browser) {
76
                $this->pushPerformanceLog($log->toArray(), $browser);
77
            });
78
    }
79
80
    protected function pushAction(string $name, array $payload)
81
    {
82
        try {
83
            $this->client->post('http://127.0.0.1:'.StartDashboardCommand::PORT.'/events', [
84
                RequestOptions::JSON => [
85
                    'channel' => 'dusk-dashboard',
86
                    'name' => $name,
87
                    'data' => $payload,
88
                ],
89
            ]);
90
        } catch (\Exception $e) {
91
            // Dusk-Dashboard Server might be turned off. No need to panic!
92
        }
93
    }
94
95
    protected function pushPerformanceLog(array $log, Browser $browser)
96
    {
97
        $request = $log[0];
98
        $response = $log[1];
99
100
        $url = parse_url(data_get($request, 'message.params.request.url'));
101
102
        $this->pushAction('dusk-event', [
103
            'test' => $this->testName,
104
            'name' => 'XHR',
105
            'arguments' => [
106
                data_get($request, 'message.params.request.method').' '.
107
                $url['path'].' '.
108
                data_get($response, 'message.params.response.status').' '.
109
                data_get($response, 'message.params.response.statusText'),
110
            ],
111
            'html' => $browser->getCurrentPageSource(),
112
            'logs' => $log,
113
        ]);
114
    }
115
}
116