WebHook::setDefaultOptions()   B
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 27
ccs 21
cts 21
cp 1
rs 8.439
cc 5
eloc 15
nc 1
nop 1
crap 5
1
<?php
2
3
namespace Crummy\Phlack\WebHook;
4
5
use Crummy\Phlack\Common\OptionsResolver;
6
use Symfony\Component\OptionsResolver\Options;
7
8
class WebHook extends AbstractCommand implements WebHookInterface
9
{
10
    const COMMAND_DELIMITER = ':';
11
12
    protected $required = [
13
        'token',
14
        'team_id',
15
        'team_domain',
16
        'service_id',
17
        'channel_id',
18
        'channel_name',
19
        'timestamp',
20
        'user_id',
21
        'user_name',
22
        'text',
23
    ];
24
25
    /**
26
     * @param OptionsResolver $resolver
27
     */
28 7
    protected function setDefaultOptions(OptionsResolver $resolver)
29
    {
30 7
        parent::setDefaultOptions($resolver);
31
32 7
        $resolver->setDefaults([
33
            'command' => function (Options $options) {
34 6
                $text = $options['text'];
35 6
                $delimiterPos = strpos($text, self::COMMAND_DELIMITER);
36 6
                $delimiterPos = false === $delimiterPos ? strpos($text, ' ') : $delimiterPos;
37
38 6
                return false === $delimiterPos ? $text : substr($text, 0, $delimiterPos);
39 7
            },
40 7
        ]);
41
42 7
        $resolver->setNormalizers([
43 7
            'command' => function (Options $options, $value) {
44 6
                if (null !== $value) {
45 6
                    $value = preg_replace('/[^a-z0-9\-]/', '', strtolower($value));
46 6
                    if (false === strpos($value, self::COMMAND_DELIMITER)) {
47 6
                        $value .= self::COMMAND_DELIMITER;
48 6
                    }
49 6
                }
50
51 6
                return $value;
52 7
            },
53 7
        ]);
54 7
    }
55
}
56