Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Slivo-fr committed May 9, 2019
1 parent a0c6d88 commit f55982e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Settings

// Server configuration
public static $SMTP_SERVER = 'smtp.example.com';
public static $SMTP_PORT = '465';
public static $SMTP_PORT = 465;
public static $SMTP_USER = '[email protected]';
public static $SMTP_PASSWORD = 'my_password';

Expand Down
38 changes: 21 additions & 17 deletions src/CurlWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,30 @@ class CurlWrapper
* @return bool|string
* @throws Exception
*/
static function curlRequest($url, $method = self::METHOD_GET, $data = null)
static public function curlRequest($url, $method = self::METHOD_GET, $data = null)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, Settings::$HTTP_HEADER);
if ($ch != false) {
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, Settings::$HTTP_HEADER);

if ($method == self::METHOD_POST) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
if ($method == self::METHOD_POST) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}

$output = curl_exec($ch);
self::handleErrors($output, $ch);
curl_close($ch);

$output = curl_exec($ch);
self::handleErrors($output, $ch);
curl_close($ch);
return $output;
}

return $output;
throw new Exception('Unable to initiate curl');
}

/**
Expand All @@ -47,7 +51,7 @@ static function curlRequest($url, $method = self::METHOD_GET, $data = null)
* @return bool|string
* @throws Exception
*/
static function get($url) {
static public function get($url) {
return self::curlRequest($url);
}

Expand All @@ -58,7 +62,7 @@ static function get($url) {
* @return bool|string
* @throws Exception
*/
static function post($url, $data) {
static public function post($url, $data) {
return self::curlRequest($url, self::METHOD_POST, $data);
}

Expand All @@ -67,7 +71,7 @@ static function post($url, $data) {
* @param $ch
* @throws Exception
*/
static function handleErrors($output, $ch) {
static protected function handleErrors($output, $ch) {

if($output === false)
{
Expand Down
14 changes: 11 additions & 3 deletions src/Killbot.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function run()

// Processing previously failed kills
$files = $this->getPendingFiles();

foreach ($files as $file) {

Logger::log('Retrying pending kill '.$file, 'INFO');
Expand Down Expand Up @@ -79,7 +80,7 @@ private function processKill($data = null)
if ($this->isWatchedKill($data, $watchedEntities)) {

if (Settings::$DEBUG) {
Logger::storeKillJson($data->{'package'}->{'killID'}, $rawOutput);
Logger::storeKillJson($data->{'package'}->{'killID'}, json_encode($data));
}

$jsonAttachments = $this->formatKillData($data, $watchedEntities);
Expand All @@ -100,7 +101,7 @@ private function processKill($data = null)
Utils::writeFile(
$rawOutput,
Utils::getUnprocessedPath(),
Uuid::uuid4() . '.kill',
Uuid::uuid4()->toString() . '.kill',
'w'
);
}
Expand Down Expand Up @@ -207,6 +208,7 @@ private function isSystemWatched($killmail, $watchedEntities)
*
* @param $killData
* @param $slackHook
* @throws Exception
*/
private function pushToSlack($killData, $slackHook)
{
Expand Down Expand Up @@ -333,7 +335,13 @@ protected function getPendingFiles(): array
{
$unprocessedPath = Utils::getUnprocessedPath();
Utils::createPath($unprocessedPath);
$files = array_diff(scandir($unprocessedPath), array('..', '.'));
$files = scandir($unprocessedPath);

if ($files) {
$files = array_diff($files, array('..', '.'));
} else {
$files = [];
}

return $files;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Killbot;

use Exception;

class Logger
{

Expand All @@ -11,8 +13,9 @@ class Logger
*
* @param $killId
* @param $data
* @throws Exception
*/
static function storeKillJson($killId, $data)
static public function storeKillJson($killId, $data)
{

$path = Utils::getKillPath();
Expand All @@ -21,7 +24,7 @@ static function storeKillJson($killId, $data)
Utils::writeFile($data, $path, $filename, 'w');
}

static function log($string, $type = 'ERROR')
static public function log($string, $type = 'ERROR')
{

$path = Utils::getLogPath();
Expand Down
11 changes: 9 additions & 2 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Killbot;


use Exception;
use Settings;

class Utils
Expand All @@ -14,14 +15,20 @@ class Utils
* @param string $path
* @param string $filename
* @param string $mode
* @throws Exception
*/
public static function writeFile($data, string $path, string $filename, string $mode)
{
self::createPath($path);

$file = fopen($path . $filename, $mode);
fwrite($file, $data);
fclose($file);

if ($file) {
fwrite($file, $data);
fclose($file);
} else {
throw new Exception('Unable to write file ' .$filename);
}
}

public static function createPath($path)
Expand Down

0 comments on commit f55982e

Please sign in to comment.