Passed
Branch merging-leagues-tournaments (a75688)
by Benedikt
07:29
created

StartAndFinishable::cloneFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 2/4/18
7
 * Time: 1:56 AM
8
 */
9
10
namespace Tfboe\FmLib\Entity\Helpers;
11
12
use DateTime;
13
use Doctrine\ORM\Mapping as ORM;
14
use Tfboe\FmLib\Exceptions\Internal;
15
16
/**
17
 * Trait StartAndFinishable
18
 * @package App\Entity\Helpers
19
 */
20
trait StartAndFinishable
21
{
22
  use TimeEntity;
23
24
//<editor-fold desc="Fields">
25
  /**
26
   * @ORM\Column(type="smallint")
27
   * @var int
28
   */
29
  private $status = 0;
30
//</editor-fold desc="Fields">
31
32
//<editor-fold desc="Public Final Methods">
33
  /**
34
   * @return int
35
   */
36
  final public function getStatus(): int
37
  {
38
    return $this->status;
39
  }
40
41
  /**
42
   * @return bool
43
   */
44
  final public function isFinished(): bool
45
  {
46
    return $this->statusIsFinished($this->status);
47
  }
48
49
  /**
50
   * @return bool
51
   */
52
  final public function isStarted(): bool
53
  {
54
    return $this->statusIsStarted($this->status);
55
  }
56
57
  /**
58
   * @param StartAndFinishableInterface $entity
59
   */
60
  final public function cloneFrom(StartAndFinishableInterface $entity): void
61
  {
62
    $this->status = $entity->getStatus();
63
    $this->setStartTime($entity->getStartTime());
64
    $this->setEndTime($entity->getEndTime());
65
  }
66
67
  /**
68
   * @param int $status
69
   * @param DateTime|null $now
70
   * @param bool $setStartTime
71
   * @param bool $setEndTime
72
   */
73
  final public function setStatus(int $status, ?DateTime $now, bool $setStartTime = true, bool $setEndTime = true): void
74
  {
75
    if ($status === $this->status) {
76
      //nothing to do
77
      return;
78
    }
79
    $this->ensureValidValue($status);
80
    if (!$this->changeIsValid($this->status, $status)) {
81
      Internal::error("Invalid status change!");
82
    }
83
    //set reset start/end times
84
    if ($this->statusIsFinished($status)) {
85
      if ($setEndTime && !$this->statusIsFinished($this->status)) {
86
        $this->setEndTime($now);
87
      }
88
      if ($setStartTime && !$this->statusIsStarted($this->status)) {
89
        $this->setStartTime($now);
90
      }
91
    } elseif ($this->statusIsStarted($status)) {
92
      if ($setEndTime) {
93
        $this->setEndTime(null);
94
      }
95
      if ($setStartTime && !$this->statusIsStarted($this->status)) {
96
        $this->setStartTime($now);
97
      }
98
    } else {
99
      if ($setEndTime) {
100
        $this->setEndTime(null);
101
      }
102
      if ($setStartTime) {
103
        $this->setStartTime(null);
104
      }
105
    }
106
107
    $this->status = $status;
108
  }
109
//</editor-fold desc="Public Final Methods">
110
111
//<editor-fold desc="Public Methods">
112
//</editor-fold desc="Public Methods">
113
114
//<editor-fold desc="Protected Methods">
115
  /**
116
   * @param int|null $oldStatus
117
   * @param int $newStatus
118
   * @return bool whether it is allowed to change the status from old to new in one step
119
   */
120
  protected function changeIsValid(?int $oldStatus, int $newStatus): bool
121
  {
122
    return abs($newStatus - $oldStatus) <= 1;
123
  }
124
125
  /**
126
   * Ensures that the given status is valid
127
   * @param int $status
128
   */
129
  protected function ensureValidValue(int $status): void
130
  {
131
    StartFinishStatus::ensureValidValue($status);
132
  }
133
134
  /**
135
   * Checks if the given status is finished
136
   * @param int $status
137
   * @return bool
138
   */
139
  protected function statusIsFinished(int $status): bool
140
  {
141
    return $status >= StartFinishStatus::FINISHED;
142
  }
143
144
  /**
145
   * Checks if the given status is started
146
   * @param int $status
147
   * @return bool
148
   */
149
  protected function statusIsStarted(int $status): bool
150
  {
151
    return $status >= StartFinishStatus::STARTED;
152
  }
153
//</editor-fold desc="Protected Methods">
154
155
}