Completed
Push — master ( b6f3d7...bfda35 )
by Tomas
04:37
created

DeviceResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 63
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 9.4347
c 0
b 0
f 0
cc 2
eloc 57
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    private $config;
12
13
    /**
14
     * @var bool
15
     */
16
    private $isNewDevice;
17
18
    /**
19
     * DeviceResolver constructor.
20
     *
21
     * @param Config $config
22
     * @param bool   $isNewDevice
23
     */
24
    public function __construct(Config $config, $isNewDevice = false)
25
    {
26
        $this->config = $config;
27
        $this->isNewDevice = $isNewDevice;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function resolve(array $data)
34
    {
35
        $resolver = (new OptionsResolver())
36
            ->setDefined('identifier')
37
            ->setAllowedTypes('identifier', 'string')
38
            ->setDefined('language')
39
            ->setAllowedTypes('language', 'string')
40
            ->setDefined('timezone')
41
            ->setAllowedTypes('timezone', 'int')
42
            ->setDefined('game_version')
43
            ->setAllowedTypes('game_version', 'string')
44
            ->setDefined('device_model')
45
            ->setAllowedTypes('device_model', 'string')
46
            ->setDefined('device_os')
47
            ->setAllowedTypes('device_os', 'string')
48
            ->setDefined('ad_id')
49
            ->setAllowedTypes('ad_id', 'string')
50
            ->setDefined('sdk')
51
            ->setAllowedTypes('sdk', 'string')
52
            ->setDefined('session_count')
53
            ->setAllowedTypes('session_count', 'int')
54
            ->setDefined('tags')
55
            ->setAllowedTypes('tags', 'array')
56
            ->setDefined('amount_spent')
57
            ->setAllowedTypes('amount_spent', 'float')
58
            ->setDefined('created_at')
59
            ->setAllowedTypes('created_at', 'int')
60
            ->setDefined('playtime')
61
            ->setAllowedTypes('playtime', 'int')
62
            ->setDefined('badge_count')
63
            ->setAllowedTypes('badge_count', 'int')
64
            ->setDefined('last_active')
65
            ->setAllowedTypes('last_active', 'int')
66
            ->setDefined('notification_types')
67
            ->setAllowedTypes('notification_types', 'int')
68
            ->setAllowedValues('notification_types', [1, -2])
69
            ->setDefined('test_type')
70
            ->setAllowedTypes('test_type', 'int')
71
            ->setAllowedValues('test_type', [1, 2])
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
                ]);
92
        }
93
94
        return $resolver->resolve($data);
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function getIsNewDevice()
101
    {
102
        return $this->isNewDevice;
103
    }
104
105
    /**
106
     * @param bool $isNewDevice
107
     */
108
    public function setIsNewDevice($isNewDevice)
109
    {
110
        $this->isNewDevice = $isNewDevice;
111
    }
112
}
113