Lines 100.00% 15 / 15
Functions and Methods 100.00% 4 / 4
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
Medal 100.00% 15 / 15 100.00% 4 / 4 13 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 level 100.00% 5 / 5 100.00% 1 / 1 3
 onyxCount 100.00% 1 / 1 100.00% 1 / 1 2
 badge 100.00% 8 / 8 100.00% 1 / 1 7
1<?php
2
3declare(strict_types=1);
4
5namespace App\Medal;
6
7/**
8 * An Ingress medal, as in 4e-website's MedalChecker::$medalLevels (the app's data/Medals.kt).
9 */
10final readonly class Medal
11{
12    /** The tiers' image suffixes and names, bronze to onyx. */
13    public const array TIERS = ['bronze' => 'Bronze', 'silver' => 'Silver', 'gold' => 'Gold', 'platinum' => 'Platinum', 'black' => 'Onyx'];
14
15    /**
16     * @param string    $column       the agent_stat column holding the medal's value
17     * @param list<int> $levels       the values needed for bronze, silver, gold, platinum and onyx
18     * @param bool      $discontinued no longer awarded: hidden unless already earned
19     */
20    public function __construct(
21        public string $column,
22        public string $name,
23        public string $description,
24        public array $levels,
25        public bool $discontinued = false,
26    ) {
27    }
28
29    /** 0 (locked) to 5 (onyx). */
30    public function level(?int $value): int
31    {
32        $level = 0;
33        foreach ($this->levels as $needed) {
34            if (($value ?? 0) >= $needed) {
35                ++$level;
36            }
37        }
38
39        return $level;
40    }
41
42    /** How many times the onyx value was reached; 0 below onyx. */
43    public function onyxCount(?int $value): int
44    {
45        return 5 === $this->level($value) ? \intdiv($value ?? 0, $this->levels[4]) : 0;
46    }
47
48    /**
49     * The badge image of [$level] (1 to 5), as MedalChecker::getBadgePath, e.g. recon → badge_opr_*.
50     */
51    public function badge(int $level): string
52    {
53        $name = match ($this->column) {
54            'recon' => 'opr',
55            'specops' => 'spec_ops',
56            'missionday' => 'mission_day_prime',
57            'nl1331_meetups' => 'nl1331',
58            'ifs' => 'fs',
59            default => $this->column,
60        };
61
62        return 'badges/badge_'.$name.'_'.\array_keys(self::TIERS)[$level - 1].'.webp';
63    }
64}