Passed
Branch dev (745ef0)
by
unknown
03:03
created

fileReader::onMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use discord\discord;
27
28
/**
29
 * Class fileReaderJabber
30
 */
31
class fileReader
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    public $config;
34
    public $discord;
35
    public $logger;
36
    public $guild;
37
    private $db;
38
    private $channelConfig;
39
    private $lastCheck = 0;
40
41
    /**
42
     * @param $config
43
     * @param $discord
44
     * @param $logger
45
     */
46
    public function init($config, $discord, $logger)
47
    {
48
        $this->config = $config;
49
        $this->discord = $discord;
50
        $this->logger = $logger;
51
        $this->guild = $config['bot']['guild'];
52
        $this->channelConfig = $config['plugins']['fileReader']['channelConfig'];
53
        $this->db = $config['plugins']['fileReader']['db'];
54
        if (!is_file($this->db)) {
55
            touch($this->db);
56
        }
57
    }
58
59
    /**
60
     *
61
     */
62
    public function tick()
63
    {
64
        if (filemtime($this->db) >= $this->lastCheck) {
65
            $data = file($this->db);
66
            if ($data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
67
                $ping = '';
68
                foreach ($data as $row) {
69
                    $row = str_replace('^@', '', $row);
70
                    if ($row == '' || $row == ' ') {
71
                        continue;
72
                    }
73
74
                    $ping .= $row . '  ';
75
                }
76
77
                // Remove |  from the line or whatever else is at the last two characters in the string
78
                $message = trim(substr($ping, 0, -2));
79
                foreach ($this->channelConfig as $chanName => $chanConfig) {
80
                    if ($chanConfig['searchString'] == false) { // If no match was found, and searchString is false, just use that
81
                        $message = $chanConfig['textStringPrepend'] . " \n " . $message . '  ' . $chanConfig['textStringAppend'];
82
                        $channelID = $chanConfig['channelID'];
83
                    } elseif (stristr($message, $chanConfig['searchString'])) {
84
                        $message = $chanConfig['textStringPrepend'] . " \n " . $message . '  ' . $chanConfig['textStringAppend'];
85
                        $channelID = $chanConfig['channelID'];
86
                    }
87
                }
88
89
                if (!isset($channelID)) { // Make sure it's always set.
90
                    $channelID = null;
91
                }
92
                $begin = mb_substr($message, 0, 15);
93
                if (strstr($begin, '#')) {
94
                    $message = 'skip';
95
                }
96
                if ($channelID == '' || $channelID == null) {
97
                    $message = 'skip';
98
                }
99
                if ($message != 'skip') {
100
                    $this->logger->addInfo("fileReader: Ping sent to front of queue for {$channelID}");
101
                    priorityQueueMessage($message, $channelID, $this->guild);
102
                }
103
            }
104
            $h = fopen($this->db, 'wb+');
105
            fclose($h);
106
            chmod($this->db, 0777);
107
        }
108
        clearstatcache();
109
        $this->lastCheck = time();
110
    }
111
}
112