Factory::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 13
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of the Numverify API Client for PHP.
7
 *
8
 * (c) 2024 Eric Sizemore <[email protected]>
9
 * (c) 2018-2021 Mark Rogoyski <[email protected]>
10
 *
11
 * This source file is subject to the MIT license. For the full copyright,
12
 * license information, and credits/acknowledgements, please view the LICENSE
13
 * and README files that were distributed with this source code.
14
 */
15
16
namespace Numverify\PhoneNumber;
17
18
use stdClass;
19
20
/**
21
 * PhoneNumber Factory.
22
 *
23
 * Role: Factory class to create the appropriate PhoneNumber object.
24
 *
25
 * @phpstan-type ValidPhoneNumberObject = stdClass&object{
26
 *     valid: bool|string,
27
 *     number: int|string,
28
 *     local_format: string,
29
 *     international_format: string,
30
 *     country_prefix: string,
31
 *     country_code: string,
32
 *     country_name: string,
33
 *     location: string,
34
 *     carrier: string,
35
 *     line_type: string
36
 * }
37
 * @phpstan-type InvalidPhoneNumberObject = stdClass&object{valid: bool|string, number: int|string}
38
 */
39
class Factory
40
{
41
    /**
42
     * @param InvalidPhoneNumberObject|ValidPhoneNumberObject $validatedPhoneNumber
43
     */
44 8
    public static function create(stdClass $validatedPhoneNumber): PhoneNumberInterface
45
    {
46 8
        if ((bool) $validatedPhoneNumber->valid === false) {
47
            /**
48
             * @var InvalidPhoneNumberObject $validatedPhoneNumber
49
             */
50 2
            return new InvalidPhoneNumber($validatedPhoneNumber);
51
        }
52
53
        /**
54
         * @var ValidPhoneNumberObject $validatedPhoneNumber
55
         */
56 6
        return new ValidPhoneNumber($validatedPhoneNumber);
57
    }
58
}
59