Completed
Pull Request — develop (#33)
by Michael
02:17
created

WebHook   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 5
c 5
b 2
f 0
lcom 0
cbo 2
dl 0
loc 48
ccs 21
cts 21
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B setDefaultOptions() 0 27 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