Completed
Pull Request — master (#74)
by
unknown
01:26
created

DeviceResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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