Completed
Pull Request — master (#8)
by Romain
04:20
created

Options   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 110
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 4
A build() 0 13 1
A fromArray() 0 15 3
1
<?php
2
namespace ker0x\Push\Adapter\Fcm\Message;
3
4
use Cake\Utility\Inflector;
5
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidOptionsException;
6
7
class Options
8
{
9
10
    /**
11
     * This parameter identifies a group of messages.
12
     *
13
     * @var null|string
14
     */
15
    protected $collapseKey;
16
17
    /**
18
     * Sets the priority of the message.
19
     *
20
     * @var null|string
21
     */
22
    protected $priority;
23
24
    /**
25
     * When a notification or message is sent and this is set to true,
26
     * an inactive client app is awoken.
27
     *
28
     * @var bool
29
     */
30
    protected $contentAvailable = false;
31
32
    /**
33
     * This parameter specifies how long (in seconds) the message should be kept
34
     * in FCM storage if the device is offline.
35
     *
36
     * @var null|int
37
     */
38
    protected $timeToLive;
39
40
    /**
41
     * This parameter specifies the package name of the application where the registration
42
     * tokens must match in order to receive the message.
43
     *
44
     * @var null|string
45
     */
46
    protected $restrictedPackageName;
47
48
    /**
49
     * This parameter, when set to true, allows developers to test a request without
50
     * actually sending a message.
51
     *
52
     * @var bool
53
     */
54
    protected $dryRun = false;
55
56
    /**
57
     * Options constructor.
58
     *
59
     * @param array|\ker0x\Push\Adapter\Fcm\Message\OptionsBuilder $optionsBuilder Options for the push.
60
     */
61
    public function __construct($optionsBuilder)
62
    {
63
        if (is_array($optionsBuilder)) {
64
            $optionsBuilder = $this->fromArray($optionsBuilder);
65
        }
66
67
        $this->collapseKey = $optionsBuilder->getCollapseKey();
68
        $this->priority = $optionsBuilder->getPriority();
69
        $this->contentAvailable = $optionsBuilder->isContentAvailable() ? true : null;
70
        $this->timeToLive = $optionsBuilder->getTimeToLive();
71
        $this->dryRun = $optionsBuilder->isDryRun() ? true : null;
72
        $this->restrictedPackageName = $optionsBuilder->getRestrictedPackageName();
73
    }
74
75
    /**
76
     * Return options as an array.
77
     *
78
     * @return array
79
     */
80
    public function build()
81
    {
82
        $options = [
83
            'collapse_key' => $this->collapseKey,
84
            'content_available' => $this->contentAvailable,
85
            'dry_run' => $this->dryRun,
86
            'priority' => $this->priority,
87
            'restricted_package_name' => $this->restrictedPackageName,
88
            'time_to_live' => $this->timeToLive,
89
        ];
90
91
        return array_filter($options);
92
    }
93
94
    /**
95
     * Build options from an array.
96
     *
97
     * @param array $optionsArray Array of options for the push.
98
     * @return \ker0x\Push\Adapter\Fcm\Message\OptionsBuilder
99
     * @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidOptionsException
100
     */
101
    private function fromArray(array $optionsArray)
102
    {
103
        if (empty($optionsArray)) {
104
            throw InvalidOptionsException::arrayEmpty();
105
        }
106
107
        $optionsBuilder = new OptionsBuilder();
108
        foreach ($optionsArray as $key => $value) {
109
            $key = Inflector::camelize($key);
110
            $setter = 'set' . $key;
111
            $optionsBuilder->$setter($value);
112
        }
113
114
        return $optionsBuilder;
115
    }
116
}
117