Lines 90.90% 10 / 11
Methods 66.66% 2 / 3
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getDateTime 75.00% 3 / 4 0.00% 0 / 1 2.06
 validateRanges 100.00% 6 / 6 100.00% 1 / 1 5
15final readonly class AgentStatInput
16{
17    public const string DATETIME_FORMAT = 'Y-m-d H:i:s';
18
19    /** agent_stat stores the stats as INT. */
20    private const int MAX_VALUE = 2147483647;
21
22    public function __construct(
23        #[Assert\NotBlank, Assert\DateTime(format: self::DATETIME_FORMAT)]
24        public string $datetime = '',
25        #[Assert\NotBlank(normalizer: 'trim'), Assert\Length(max: 150)]
26        public string $nickname = '',
27        #[Assert\NotBlank(normalizer: 'trim'), Assert\Length(max: 50)]
28        public string $faction = '',
29        #[Assert\NotNull]
30        public ?int $ap = null,
31        public ?int $level = null,
32        public ?int $explorer = null,
33        public ?int $recon = null,
34        public ?int $seer = null,
35        public ?int $trekker = null,
36        public ?int $builder = null,
37        public ?int $connector = null,
38        #[SerializedName('mind_controller')]
39        public ?int $mindController = null,
40        public ?int $illuminator = null,
41        public ?int $recharger = null,
42        public ?int $liberator = null,
43        public ?int $pioneer = null,
44        public ?int $engineer = null,
45        public ?int $purifier = null,
46        public ?int $specops = null,
47        public ?int $hacker = null,
48        public ?int $translator = null,
49        public ?int $sojourner = null,
50        public ?int $recruiter = null,
51        public ?int $missionday = null,
52        #[SerializedName('nl1331_meetups')]
53        public ?int $nl1331Meetups = null,
54        public ?int $ifs = null,
55        #[SerializedName('current_challenge')]
56        public ?int $currentChallenge = null,
57        public ?int $scout = null,
58        #[SerializedName('scout_controller')]
59        public ?int $scoutController = null,
60        #[SerializedName('longest_link')]
61        public ?int $longestLink = null,
62        #[SerializedName('largest_field')]
63        public ?int $largestField = null,
64        public ?int $recursions = null,
65        #[SerializedName('drone_flight_distance')]
66        public ?int $droneFlightDistance = null,
67        public ?int $maverick = null,
68        #[SerializedName('drone_portals_visited')]
69        public ?int $dronePortalsVisited = null,
70        #[SerializedName('drone_forced_recalls')]
71        public ?int $droneForcedRecalls = null,
72        #[SerializedName('kinetic_capsules_completed')]
73        public ?int $kineticCapsulesCompleted = null,
74        #[SerializedName('months_subscribed')]
75        public ?int $monthsSubscribed = null,
76        public ?int $epoch = null,
77        #[SerializedName('portals_discovered')]
78        public ?int $portalsDiscovered = null,
79        #[SerializedName('drones_returned')]
80        public ?int $dronesReturned = null,
81        #[SerializedName('second_sunday')]
82        public ?int $secondSunday = null,
83        public ?int $reclaimer = null,
84    ) {
85    }
86
87    public function getDateTime(): \DateTimeImmutable
88    {
89        $dateTime = \DateTimeImmutable::createFromFormat('!'.self::DATETIME_FORMAT, $this->datetime);
90        if (false === $dateTime) {
91            throw new \UnexpectedValueException('Invalid datetime, validate the input first.');
92        }
93
94        return $dateTime;
95    }
96
97    #[Assert\Callback]
98    public function validateRanges(ExecutionContextInterface $context): void
99    {
100        foreach (\get_object_vars($this) as $name => $value) {
101            if (\is_int($value) && ($value < 0 || $value > self::MAX_VALUE)) {
102                $context->buildViolation('The value must be between 0 and {{ max }}.')
103                    ->setParameter('{{ max }}', (string) self::MAX_VALUE)
104                    ->atPath($name)
105                    ->addViolation();
106            }
107        }
108    }
109}