Builder::checkBuilderFields()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types = 1);
3
4
namespace SLWDC\NICParser;
5
6
7
use SLWDC\NICParser\Exception\BadMethodCallException;
8
use SLWDC\NICParser\Exception\InvalidArgumentException;
9
10
class Builder {
11
12
  /**
13
   * @var \DateTime
14
   */
15
  private $birthday;
16
17
  /**
18
   * @var string
19
   */
20
  private $gender;
21
22
  /**
23
   * @var int
24
   */
25
  private $serial_number;
26
27
  public function setParser(Parser $parser) {
28
    $this->birthday = $parser->getBirthday();
29
    $this->gender = $parser->getGender();
30
    $this->serial_number = $parser->getSerialNumber();
31
  }
32
33
  public function setBirthday(\DateTime $date) {
34
    $this->birthday = clone $date;
35
    return $this;
36
  }
37
38
  public function setGender(string $gender = 'M') {
39
    if ($gender === 'M' || $gender === 'F') {
40
      $this->gender = $gender;
41
      return $this;
42
    }
43
    throw new InvalidArgumentException('Unknown gender. Allowed values are: "M" and "F');
44
  }
45
46
  public function setSerialNumber(int $serial_number) {
47
    $this->serial_number = $serial_number;
48
  }
49
50
  public function getNumber(): string {
51
    $this->checkBuilderFields();
52
53
    $year = $this->birthday->format('Y');
54
    $start_date = (new \DateTime())->setDate((int) $year, 1, 1)->setTime(0, 0);
55
    $birth_date_count = (int) $this->birthday->diff($start_date)->format('%a');
0 ignored issues
show
Bug introduced by
It seems like $start_date can also be of type false; however, parameter $datetime2 of DateTime::diff() does only seem to accept DateTimeInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
    $birth_date_count = (int) $this->birthday->diff(/** @scrutinizer ignore-type */ $start_date)->format('%a');
Loading history...
56
57
    ++$birth_date_count;
58
59
    if ($this->gender === 'F') {
60
      $birth_date_count += 500;
61
    }
62
63
    $serial = $this->serial_number;
64
    return "{$year}{$birth_date_count}{$serial}";
65
  }
66
67
  public function getParser(): Parser {
68
    $number = $this->getNumber();
69
    return new Parser($number);
70
  }
71
72
  public function checkBuilderFields() {
73
    if (!$this->birthday) {
74
      throw new BadMethodCallException('Attempting to build ID number without a valid birthday set.');
75
    }
76
    if (!$this->gender) {
77
      throw new BadMethodCallException('Attempting to build ID number without a valid gender set.');
78
    }
79
    if (!$this->serial_number) {
80
      throw new BadMethodCallException('Attempting to build ID number without a valid serial number set.');
81
    }
82
  }
83
}
84