Lines 100.00% 8 / 8
Functions and Methods 100.00% 2 / 2
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
CgroupCpuQuota 100.00% 8 / 8 100.00% 2 / 2 8 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 parseCpuMax 100.00% 7 / 7 100.00% 1 / 1 7
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker;
6
7final readonly class CgroupCpuQuota
8{
9    private const string UNLIMITED = 'max';
10
11    private function __construct(public int $cpus) {}
12
13    public static function parseCpuMax(string $cpuMax): ?self
14    {
15        $fields = preg_split('/\s+/', trim($cpuMax));
16
17        if ($fields === false || \count($fields) !== 2) {
18            return null;
19        }
20
21        [$quota, $period] = $fields;
22
23        if ($quota === self::UNLIMITED || !ctype_digit($quota) || !ctype_digit($period) || (int) $period === 0) {
24            return null;
25        }
26
27        return new self(max(1, (int) ceil((int) $quota / (int) $period)));
28    }
29}