Slack   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 4.11 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 5
c 3
b 0
f 2
lcom 1
cbo 1
dl 3
loc 73
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 2
B notify() 3 36 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Behatch\Notifier;
4
5
class Slack extends Notifier
6
{
7
    static private $url;
8
    static private $settings;
9
    static private $prefix;
10
    static private $attachment;
11
12
    public function __construct($url, $settings = [], $prefix = null, $attachment = [], $spamTimeout = 60)
13
    {
14
15
        if (!class_exists('\\Maknz\\Slack\\Client')) {
16
            $message = 'Class \Maknz\Slack\Client does not exist. Are you sure you have installed it? i.e. composer require "maknz/slack"';
17
            throw new \Exception( $message );
18
        }
19
20
        parent::__construct($spamTimeout);
21
22
        self::$url = $url;
23
        self::$prefix = $prefix;
24
25
        self::$settings = $settings + [
26
            'username' => 'Behat',
27
            'channel' => '#general',
28
            'link_names' => true,
29
            'icon' => ':fire:',
30
        ];
31
32
        self::$attachment = $attachment + [
33
            'fallback' => 'Behat test failed',
34
            'text' => '',
35
            'pretext' => '',
36
            'color' => 'danger',
37
            'fields' => array(),
38
        ];
39
    }
40
41
    protected static function notify($status, $title, $message)
42
    {
43
44
        // We don't need the second notification about the suite failing
45
        if (strpos($title, 'suite ended') !== false) {
46
            return;
47
        }
48
49
        // If there's a prefix, prepend it to the title of the message
50 View Code Duplication
        if (self::$prefix !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            $title = '[' . self::$prefix . '] ' . $title;
52
        }
53
54
        // Fire up the Slack Client with the given webhook url and settings
55
        $client = new \Maknz\Slack\Client(self::$url, self::$settings);
56
57
        // Split the message on > as the behat step failures give the regex then the message
58
        // We just want the piece after the final '>'. @TODO: Better option here? As this will
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
59
        // fail horribly if there is a '>' character in the Exception
60
        $splitMessage = explode( '>', $message );
61
62
        // Build the attachment
63
        self::$attachment['pretext'] = $title;
64
65
        self::$attachment['fields'] = array(
66
            array(
67
                'title' => 'Step',
68
                'value' => end($splitMessage),
69
                'short' => false,
70
            ),
71
        );
72
73
        // Send!
74
        $client->attach(self::$attachment)->enableMarkdown()->send();
75
76
    }
77
}
78