Caster::castToAppId()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 10
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (c) Ne-Lexa
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 *
11
 * @see https://github.com/Ne-Lexa/google-play-scraper
12
 */
13
14
namespace Nelexa\GPlay\Util;
15
16
use Nelexa\GPlay\Enum\CategoryEnum;
17
use Nelexa\GPlay\Model\App;
18
use Nelexa\GPlay\Model\AppId;
19
use Nelexa\GPlay\Model\AppInfo;
20
use Nelexa\GPlay\Model\Category;
21
use Nelexa\GPlay\Model\Developer;
22
23
/**
24
 * @internal
25
 */
26
final class Caster
27
{
28
    /**
29
     * @param string|int|Developer|App|AppInfo $developerId
30
     *
31
     * @return string
32
     */
33 7
    public static function castToDeveloperId($developerId): string
34
    {
35 7
        if ($developerId instanceof AppInfo) {
36 1
            return $developerId->getDeveloper()->getId();
37
        }
38
39 6
        if ($developerId instanceof Developer) {
40 1
            return $developerId->getId();
41
        }
42
43 5
        if (\is_int($developerId)) {
44 2
            return (string) $developerId;
45
        }
46
47 3
        return $developerId;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $developerId could return the type Nelexa\GPlay\Model\App which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
50
    /**
51
     * @param Category|CategoryEnum|string $category
52
     *
53
     * @return string
54
     */
55 18
    public static function castToCategoryId($category): string
56
    {
57 18
        if ($category instanceof CategoryEnum) {
58 17
            return $category->value();
59
        }
60
61 1
        if ($category instanceof Category) {
62
            return $category->getId();
63
        }
64
65 1
        return (string) $category;
66
    }
67
68
    /**
69
     * Casts the application id to the {@see AppId} type.
70
     *
71
     * @param string|AppId $appId   application ID
72
     * @param string       $locale
73
     * @param string       $country
74
     *
75
     * @return AppId application ID such as {@see AppId}
76
     */
77 24
    public static function castToAppId($appId, string $locale, string $country): AppId
78
    {
79 24
        if ($appId === null) {
80 1
            throw new \InvalidArgumentException('Application ID is null');
81
        }
82
83 23
        if (\is_string($appId)) {
84 16
            return new AppId($appId, $locale, $country);
85
        }
86
87 8
        return $appId;
88
    }
89
}
90