Passed
Branch feature/refactoring (13cbf0)
by Alexey
03:46
created

Caster::castToDeveloperId()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

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