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