Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.35% covered (warning)
82.35%
28 / 34
75.00% covered (warning)
75.00%
6 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
SessionChangeMessage
82.35% covered (warning)
82.35%
28 / 34
75.00% covered (warning)
75.00%
6 / 8
12.79
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 session
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 testState
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 unitState
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 setSession
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 setTestState
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setUnitState
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare(strict_types=1);
3// TODO unit-tests
4
5class SessionChangeMessage implements JsonSerializable {
6  protected int $personId;
7  protected int $timestamp = 0;
8  protected ?int $testId;
9  protected ?string $groupName;
10  protected ?string $groupLabel = "";
11  protected ?string $personLabel = "";
12  protected ?string $mode = "";
13  protected ?array $testState = [];
14  protected ?string $bookletName = "";
15  protected ?string $unitName = "";
16  protected ?array $unitState = [];
17
18  private function __construct(string $groupName, int $personId, int $testId, int $timestamp = null) {
19    $this->personId = $personId;
20    $this->testId = $testId;
21    $this->groupName = $groupName;
22
23    $this->timestamp = $timestamp ?? TimeStamp::now();
24  }
25
26  public static function session(int $testId, PersonSession $session, int $timestamp = null): SessionChangeMessage {
27    $message = new SessionChangeMessage(
28      $session->getLoginSession()->getLogin()->getGroupName(),
29      $session->getPerson()->getId(),
30      $testId,
31      $timestamp
32    );
33    $message->setSession($session);
34    return $message;
35  }
36
37  public static function testState(string $groupName, int $personId, int $testId, array $testState, string $bookletName = null): SessionChangeMessage {
38    $message = new SessionChangeMessage($groupName, $personId, $testId);
39    $message->setTestState($testState, $bookletName);
40    return $message;
41  }
42
43  public static function unitState(string $groupName, int $personId, int $testId, string $unitName, array $unitState): SessionChangeMessage {
44    $message = new SessionChangeMessage($groupName, $personId, $testId);
45    $message->setUnitState($unitName, $unitState);
46    return $message;
47  }
48
49  public function setSession(PersonSession $session): void {
50    $login = $session->getLoginSession()->getLogin();
51    $suffix = $session->getPerson()->getNameSuffix();
52
53    $this->personLabel = $login->getName() . ($suffix ? '/' . $suffix : '');
54
55    $this->mode = $login->getMode();
56    $this->groupName = $login->getGroupName();
57    $this->groupLabel = $login->getGroupLabel();
58  }
59
60  public function setTestState(array $testState, string $bookletName = null): void {
61    $this->testState = $testState;
62    if ($bookletName !== null) {
63      $this->bookletName = $bookletName;
64    }
65  }
66
67  public function setUnitState(string $unitName, array $unitState) {
68    $this->unitName = $unitName;
69    $this->unitState = $unitState;
70  }
71
72  public function jsonSerialize(): mixed {
73    $jsonData = [];
74
75    foreach ($this as $key => $value) {
76      if ($value !== "") {
77        $jsonData[$key] = $value;
78      }
79    }
80
81    return $jsonData;
82  }
83}