Completed
Branch develop (f935f6)
by Romain
01:43
created

Options   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 88
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
5
use Cake\Utility\Inflector;
6
use ker0x\Push\Adapter\Fcm\Message\Exception\InvalidOptionsException;
7
8
class Options
9
{
10
    /**
11
     * @var null|string
12
     */
13
    protected $collapseKey;
14
15
    /**
16
     * @var null|string
17
     */
18
    protected $priority;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $contentAvailable = false;
24
25
    /**
26
     * @var null|int
27
     */
28
    protected $timeToLive;
29
30
    /**
31
     * @var null|string
32
     */
33
    protected $restrictedPackageName;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $dryRun = false;
39
40
    /**
41
     * Options constructor.
42
     * @param array|OptionsBuilder $optionsBuilder
43
     */
44
    public function __construct($optionsBuilder)
45
    {
46
        if (is_array($optionsBuilder)) {
47
            $optionsBuilder = $this->fromArray($optionsBuilder);
48
        }
49
50
        $this->collapseKey = $optionsBuilder->getCollapseKey();
51
        $this->priority = $optionsBuilder->getPriority();
52
        $this->contentAvailable = $optionsBuilder->isContentAvailable() ? true : null;
53
        $this->timeToLive = $optionsBuilder->getTimeToLive();
54
        $this->dryRun = $optionsBuilder->isDryRun() ? true : null;
55
        $this->restrictedPackageName = $optionsBuilder->getRestrictedPackageName();
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function build()
62
    {
63
        $options = [
64
            'collapse_key' => $this->collapseKey,
65
            'content_available' => $this->contentAvailable,
66
            'dry_run' => $this->dryRun,
67
            'priority' => $this->priority,
68
            'restricted_package_name' => $this->restrictedPackageName,
69
            'time_to_live' => $this->timeToLive,
70
        ];
71
72
        return array_filter($options);
73
    }
74
75
    /**
76
     * @param array $optionsArray
77
     * @return \ker0x\Push\Adapter\Fcm\Message\OptionsBuilder
78
     * @throws \ker0x\Push\Adapter\Fcm\Message\Exception\InvalidOptionsException
79
     */
80
    private function fromArray(array $optionsArray): OptionsBuilder
81
    {
82
        if (empty($optionsArray)) {
83
            throw InvalidOptionsException::arrayEmpty();
84
        }
85
86
        $optionsBuilder = new OptionsBuilder();
87
        foreach ($optionsArray as $key => $value) {
88
            $key = Inflector::camelize($key);
89
            $setter = 'set' . $key;
90
            $optionsBuilder->$setter($value);
91
        }
92
93
        return $optionsBuilder;
94
    }
95
}