Packet::jsonSerialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Crummy\Phlack\Bot\Mainframe;
4
5
use Crummy\Phlack\Common\Encodable;
6
use Crummy\Phlack\Common\Event;
7
use Crummy\Phlack\Common\OptionsResolver;
8
9
class Packet extends Event implements Encodable
10
{
11
    /**
12
     * @param array $options
13
     */
14 1
    public function __construct(array $options = [])
15
    {
16 1
        $resolver = new OptionsResolver();
17 1
        $this->setDefaultOptions($resolver);
18
19 1
        parent::__construct($resolver->resolve($options));
20 1
    }
21
22
    /**
23
     * @param OptionsResolver $resolver
24
     */
25 1
    protected function setDefaultOptions(OptionsResolver $resolver)
26
    {
27 1
        $resolver->setRequired(['command']);
28 1
        $resolver->setDefaults(['output' => null]);
29
30 1
        $resolver->setTypesAllowed([
31 1
            'command' => '\Crummy\Phlack\WebHook\CommandInterface',
32 1
            'output'  => ['\Crummy\Phlack\WebHook\Reply\Reply', 'null'],
33 1
        ]);
34 1
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function jsonSerialize()
40
    {
41
        return $this['output'] instanceof Encodable ? $this['output']->jsonSerialize() : $this['output'];
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function __toString()
48
    {
49
        return json_encode($this);
50
    }
51
}
52