Irc::executeIrcCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Fabrica\Tools\Plugin;
4
5
use Fabrica\Tools\Builder;
6
use Fabrica\Models\Infra\Ci\Build;
7
use Fabrica\Tools\Plugin;
8
9
/**
10
 * IRC Plugin - Sends a notification to an IRC channel
11
 *
12
 * @author Ricardo Sierra <[email protected]>
13
 */
14
class Irc extends Plugin
15
{
16
    protected $message;
17
    protected $server;
18
    protected $port;
19
    protected $room;
20
    protected $nick;
21
22
    /**
23
     * @return string
24
     */
25
    public static function pluginName()
26
    {
27
        return 'irc';
28
    }
29
    
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function __construct(Builder $builder, Build $build, array $options = [])
34
    {
35
        parent::__construct($builder, $build, $options);
36
37
        $this->message = $options['message'];
38
        $buildSettings = $this->builder->getConfig('build_settings');
39
40 View Code Duplication
        if (isset($buildSettings['irc'])) {
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...
41
            $irc = $buildSettings['irc'];
42
43
            $this->server = $irc['server'];
44
            $this->port   = $irc['port'];
45
            $this->room   = $irc['room'];
46
            $this->nick   = $irc['nick'];
47
        }
48
    }
49
50
    /**
51
     * Run IRC plugin.
52
     *
53
     * @return bool
54
     */
55
    public function execute()
56
    {
57
        $msg = $this->builder->interpolate($this->message);
58
59
        if (empty($this->server) || empty($this->room) || empty($this->nick)) {
60
            $this->builder->logFailure('You must configure a server, room and nick.');
61
        }
62
63
        if (empty($this->port)) {
64
            $this->port = 6667;
65
        }
66
67
        $sock = fsockopen($this->server, $this->port);
68
        stream_set_timeout($sock, 1);
69
70
        $connectCommands = [
71
            'USER ' . $this->nick . ' 0 * :' . $this->nick,
72
            'NICK ' . $this->nick,
73
        ];
74
        $this->executeIrcCommands($sock, $connectCommands);
75
        $this->executeIrcCommand($sock, 'JOIN ' . $this->room);
76
        $this->executeIrcCommand($sock, 'PRIVMSG ' . $this->room . ' :' . $msg);
77
78
        fclose($sock);
79
80
        return true;
81
    }
82
83
    /**
84
     * @param  resource $socket
85
     * @param  array    $commands
86
     * @return bool
87
     */
88
    private function executeIrcCommands($socket, array $commands)
89
    {
90
        foreach ($commands as $command) {
91
            fputs($socket, $command . "\n");
92
        }
93
94
        $pingBack = false;
95
96
        // almost all servers expect pingback!
97
        while ($response = fgets($socket)) {
98
            $matches = [];
99
            if (preg_match('/^PING \\:([A-Z0-9]+)/', $response, $matches)) {
100
                $pingBack = $matches[1];
101
            }
102
        }
103
104
        if ($pingBack) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pingBack of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
105
            $command = 'PONG :' . $pingBack . "\n";
106
            fputs($socket, $command);
107
        }
108
    }
109
110
    /**
111
     *
112
     * @param  resource $socket
113
     * @param  string   $command
114
     * @return bool
115
     */
116
    private function executeIrcCommand($socket, $command)
117
    {
118
        return $this->executeIrcCommands($socket, [$command]);
119
    }
120
}
121