Lines 100.00% 4 / 4
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
DeviceInfo 100.00% 4 / 4 100.00% 1 / 1 3 100.00% 1 / 1
 __construct 100.00% 4 / 4 100.00% 1 / 1 3
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Exposure;
6
7use Stewart\Contracts\Exception\ExposureException;
8
9final readonly class DeviceInfo
10{
11    private const string IDENTIFIER_PATTERN = '/\A[a-z][a-z0-9_]*\z/';
12
13    /** @throws ExposureException */
14    public function __construct(
15        public string $identifier,
16        public string $name,
17        public ?string $manufacturer = null,
18        public ?string $model = null,
19        public ?string $suggestedArea = null,
20    ) {
21        if (preg_match(self::IDENTIFIER_PATTERN, $identifier) !== 1) {
22            throw ExposureException::configInvalid(\sprintf('The device identifier "%s" must be lowercase letters, digits and "_", starting with a letter.', $identifier));
23        }
24
25        if ($name === '') {
26            throw ExposureException::configInvalid('The device name must not be empty.');
27        }
28    }
29}