DeviceResolver::getIsNewDevice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace OneSignal\Resolver;
6
7
use OneSignal\Config;
8
use OneSignal\Devices;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
11
class DeviceResolver implements ResolverInterface
12
{
13
    private $config;
14
    private $isNewDevice;
15
16
    public function __construct(Config $config, bool $isNewDevice)
17
    {
18
        $this->config = $config;
19
        $this->isNewDevice = $isNewDevice;
20
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    public function resolve(array $data): array
26
    {
27
        $resolver = (new OptionsResolver())
28
            ->setDefined('identifier')
29
            ->setAllowedTypes('identifier', 'string')
30
            ->setDefined('language')
31
            ->setAllowedTypes('language', 'string')
32
            ->setDefined('timezone')
33
            ->setAllowedTypes('timezone', 'int')
34
            ->setDefined('game_version')
35
            ->setAllowedTypes('game_version', 'string')
36
            ->setDefined('device_model')
37
            ->setAllowedTypes('device_model', 'string')
38
            ->setDefined('device_os')
39
            ->setAllowedTypes('device_os', 'string')
40
            ->setDefined('ad_id')
41
            ->setAllowedTypes('ad_id', 'string')
42
            ->setDefined('sdk')
43
            ->setAllowedTypes('sdk', 'string')
44
            ->setDefined('session_count')
45
            ->setAllowedTypes('session_count', 'int')
46
            ->setDefined('tags')
47
            ->setAllowedTypes('tags', 'array')
48
            ->setDefined('amount_spent')
49
            ->setAllowedTypes('amount_spent', 'float')
50
            ->setDefined('created_at')
51
            ->setAllowedTypes('created_at', 'int')
52
            ->setDefined('playtime')
53
            ->setAllowedTypes('playtime', 'int')
54
            ->setDefined('badge_count')
55
            ->setAllowedTypes('badge_count', 'int')
56
            ->setDefined('last_active')
57
            ->setAllowedTypes('last_active', 'int')
58
            ->setDefined('notification_types')
59
            ->setAllowedTypes('notification_types', 'int')
60
            ->setAllowedValues('notification_types', [1, -2])
61
            ->setDefined('test_type')
62
            ->setAllowedTypes('test_type', 'int')
63
            ->setAllowedValues('test_type', [1, 2])
64
            ->setDefined('long')
65
            ->setAllowedTypes('long', 'double')
66
            ->setDefined('lat')
67
            ->setAllowedTypes('lat', 'double')
68
            ->setDefined('country')
69
            ->setAllowedTypes('country', 'string')
70
            ->setDefined('external_user_id')
71
            ->setAllowedTypes('external_user_id', 'string')
72
            ->setDefault('app_id', $this->config->getApplicationId())
73
            ->setAllowedTypes('app_id', 'string');
74
75
        if ($this->isNewDevice) {
76
            $resolver
77
                ->setRequired('device_type')
78
                ->setAllowedTypes('device_type', 'int')
79
                ->setAllowedValues('device_type', [
80
                    Devices::IOS,
81
                    Devices::ANDROID,
82
                    Devices::AMAZON,
83
                    Devices::WINDOWS_PHONE,
84
                    Devices::WINDOWS_PHONE_MPNS,
85
                    Devices::CHROME_APP,
86
                    Devices::CHROME_WEB,
87
                    Devices::WINDOWS_PHONE_WNS,
88
                    Devices::SAFARI,
89
                    Devices::FIREFOX,
90
                    Devices::MACOS,
91
                    Devices::ALEXA,
92
                    Devices::EMAIL,
93
                    Devices::HUAWEI,
94
                    Devices::SMS,
95
                ]);
96
        }
97
98
        return $resolver->resolve($data);
99
    }
100
101
    public function setIsNewDevice(bool $isNewDevice): void
102
    {
103
        $this->isNewDevice = $isNewDevice;
104
    }
105
106
    public function getIsNewDevice(): bool
107
    {
108
        return $this->isNewDevice;
109
    }
110
}
111