Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.04% covered (success)
91.04%
61 / 67
54.55% covered (warning)
54.55%
6 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
XMLFileSysCheck
91.04% covered (success)
91.04%
61 / 67
54.55% covered (warning)
54.55%
6 / 11
28.56
0.00% covered (danger)
0.00%
0 / 1
 crossValidate
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getSaveKey
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 hasSaveKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasUnit
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUnitId
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 getCustomTexts
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
3.01
 getSkipNetwork
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 getQuestions
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
 getSpeedtestUploadParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSpeedtestDownloadParams
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSpeedParams
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
6.02
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4
5class XMLFileSysCheck extends XMLFile {
6  const type = 'SysCheck';
7  const canBeRelationSubject = true;
8  const canBeRelationObject = false;
9
10  public function crossValidate(WorkspaceCache $workspaceCache): void {
11    parent::crossValidate($workspaceCache);
12
13    $unitId = $this->getUnitId();
14    $unit = $workspaceCache->getUnit($unitId);
15
16    if ($unit != null) {
17      $this->addRelation(new FileRelation($unit->getType(), $unitId, FileRelationshipType::containsUnit, $unit));
18    }
19  }
20
21  public function getSaveKey() {
22    if (!$this->isValid()) {
23      return "";
24    }
25
26    $configNode = $this->getXml()->xpath('/SysCheck/Config[@savekey]');
27    return count($configNode) ? strtoupper((string) $configNode[0]['savekey']) : '';
28  }
29
30  public function hasSaveKey(): bool {
31    return strlen($this->getSaveKey()) > 0;
32  }
33
34  public function hasUnit(): bool {
35    return strlen($this->getUnitId()) > 0;
36  }
37
38  public function getUnitId(): string {
39    if (!$this->isValid()) {
40      return "";
41    }
42
43    $configNode = $this->getXml()->xpath('/SysCheck/Config[@unit]');
44    return count($configNode) ? strtoupper((string) $configNode[0]['unit']) : '';
45  }
46
47  public function getCustomTexts(): array {
48    if (!$this->isValid()) {
49      return [];
50    }
51
52    $customTextNodes = $this->getXml()->xpath('/SysCheck/Config/CustomText');
53    $customTexts = [];
54    foreach ($customTextNodes as $customTextNode) {
55      $customTexts[] = [
56        'key' => (string) $customTextNode['key'],
57        'value' => (string) $customTextNode
58      ];
59    }
60    return $customTexts;
61  }
62
63  public function getSkipNetwork(): bool {
64    if (!$this->isValid()) {
65      return false;
66    }
67
68    $configNode = $this->getXml()->xpath('/SysCheck/Config[@skipnetwork]');
69    return count($configNode) ? ($configNode[0]['skipnetwork'] == 'true') : false;
70  }
71
72  public function getQuestions(): array {
73    if (!$this->isValid()) {
74      return [];
75    }
76
77    $questions = [];
78    $questionNodes = $this->getXml()->xpath('/SysCheck/Config/Q');
79    foreach ($questionNodes as $questionNode) {
80      $questions[] = [
81        'id' => (string) $questionNode['id'],
82        'type' => (string) $questionNode['type'],
83        'prompt' => (string) $questionNode['prompt'],
84        'required' => (boolean) $questionNode['required'],
85        'options' => strlen((string) $questionNode) ? explode('#', (string) $questionNode) : []
86      ];
87    }
88    return $questions;
89  }
90
91  public function getSpeedtestUploadParams(): array {
92    return $this->getSpeedParams(true);
93  }
94
95  public function getSpeedtestDownloadParams(): array {
96    return $this->getSpeedParams(false);
97  }
98
99  private function getSpeedParams(bool $upload = false) {
100    $speedtestParams = [
101      'min' => 0,
102      'good' => 0,
103      'maxDevianceBytesPerSecond' => 0,
104      'maxErrorsPerSequence' => 0,
105      'maxSequenceRepetitions' => 0,
106      'sequenceSizes' => []
107    ];
108
109    if (!$this->isValid()) {
110      return $speedtestParams;
111    }
112
113    $node = $this->getXml()->xpath('/SysCheck/Config/' . ($upload ? 'UploadSpeed' : 'DownloadSpeed'));
114
115    if (!count($node)) {
116      return $speedtestParams;
117    }
118
119    foreach ($speedtestParams as $param => $default) {
120      $speedtestParams[$param] = (int) $node[0][$param] ?? $default;
121    }
122
123    if ((string) $node[0]) {
124      $speedtestParams['sequenceSizes'] = array_map(
125        function($str) {
126          return trim($str);
127        },
128        explode(',', (string) $node[0])
129      );
130    }
131
132    return $speedtestParams;
133  }
134}