Passed
Branch master (97b455)
by
unknown
06:14
created

rssReader::tick()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 14
rs 9.4285
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 rssReader
30
 */
31
class rssReader
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
    /**
34
     * @var
35
     */
36
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
37
    /**
38
     * @var
39
     */
40
    var $db;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
41
    /**
42
     * @var
43
     */
44
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
45
    /**
46
     * @var
47
     */
48
    var $channelConfig;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $channelConfig.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
49
    /**
50
     * @var int
51
     */
52
    var $lastCheck = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $lastCheck.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
53
    /**
54
     * @var
55
     */
56
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
57
    public $guild;
58
59
    /**
60
     * @param $config
61
     * @param $discord
62
     * @param $logger
63
     */
64
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
65
    {
66
        $this->config = $config;
67
        $this->discord = $discord;
68
        $this->logger = $logger;
69
        $this->guild = $config["bot"]["guild"];
70
        $this->rssFeeds = $config["plugins"]["rssReader"]["rssFeeds"];
0 ignored issues
show
Bug introduced by
The property rssFeeds does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
71
        $this->toDiscordChannel = $config["plugins"]["rssReader"]["channelID"];
0 ignored issues
show
Bug introduced by
The property toDiscordChannel does not seem to exist. Did you mean discord?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
72
        $lastCheck = getPermCache("rssLastChecked");
73
        if ($lastCheck == NULL) {
74
            // Schedule it for right now if first run
75
            setPermCache("rssLastChecked", time() - 5);
76
        }
77
    }
78
79
    /**
80
     *
81
     */
82
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
83
    {
84
        $discord = $this->discord;
85
        $lastCheck = getPermCache("rssLastChecked");
86
        $feeds = $this->rssFeeds;
87
        $toChannel = $this->toDiscordChannel;
0 ignored issues
show
Bug introduced by
The property toDiscordChannel does not seem to exist. Did you mean discord?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
88
89
        if ($lastCheck <= time()) {
90
            $this->logger->addInfo("RSS Reader: Checking RSS feeds for updated news..");
91
            $this->getRss($feeds, $toChannel, $discord);
92
            setPermCache("rssLastChecked", time() + 900);
93
            $this->logger->addInfo("RSS Reader: All feeds checked..");
94
        }
95
    }
96
97
    function getRss($feeds, $toChannel, $discord)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
98
    {
99
        foreach ($feeds as $rssUrl){
100
            //Check that url is set
101
            if (!isset($rssUrl) || $rssUrl == ""){
102
                continue;
103
            }
104
105
            $rss = simplexml_load_file($rssUrl); // XML parser
106
            $feedLink = $rss->channel->link;
107
            $latestTopicDate = getPermCache("rssFeed{$feedLink}");
108
109
            //Check if feed has been checked before
110
            if ($latestTopicDate == NULL) {
111
                // Set date to now to avoid posting old articles
112
                setPermCache("rssFeed{$feedLink}", time());
113
                continue;
114
            }
115
116
            //Find item to check if feed is formatted
117
            $itemTitle = (string) $rss->channel->item->title;
118
            $itemUrl = (string) $rss->channel->item->link;
119
            $itemDate = strtotime($rss->channel->item->pubDate);
120
121
            //Check to see if feed is formatted correctly
122
            if ($itemTitle == NULL || $itemUrl == NULL || $itemDate == NULL) {
123
                $this->logger->addInfo("RSS Reader Error: {$rssUrl} is not a properly formatted RSS feed.");
124
                continue;
125
            }
126
127
            //Find item to post
128
            foreach($rss->channel->item as $item){
129
                //Get item details
130
                $itemTitle = (string) $item->title;
131
                $itemUrl = (string) $item->link;
132
                $itemPubbed = $item->pubDate;
133
                $itemDate = strtotime($item->pubDate);
134
135
                //Check if item is old
136
                if ($itemDate <= $latestTopicDate) {
137
                    continue;
138
                }
139
140
                //Set message
141
                $msg = "\n**{$itemTitle}**\n\n*{$itemPubbed}*\n{$itemUrl}";
142
143
                //Send message
144
                $channelID = $toChannel;
145
                $guild = $discord->guilds->get('id', $this->guild);
146
                $channel = $guild->channels->get('id', $channelID);
147
                $channel->sendMessage($msg, false);
148
                $this->logger->addInfo("RSS Reader Item Posted: {$itemTitle}");
149
            }
150
            setPermCache("rssFeed{$feedLink}", $itemDate);
151
        }
152
    }
153
}
154