<commit_before><?php
/**
 * Simple issue tracker for Redmine with text message notifications
 *
 * PHP version 5
 *
 * @category Utils
 * @author <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/MIT MIT
 * @version 1.0
 * @link https://www.github.com/terenaa/redmine-issue-tracker
 */

namespace terenaa\IssueTracker;

use terenaa\SmsGateway\SmsGateway;
use terenaa\SmsGateway\SmsGatewayException;


/**
 * Class IssueTracker
 * @package terenaa\IssueTracker
 */
class IssueTracker
{
    protected $config;

    /**
     * IssueTracker constructor.
     */
    public function __construct()
    {
        $this->getConfig();
    }

    /**
     * Runs tracker in infinite loop with specified intervals
     */
    public function run()
    {
        while (true) {
            try {
                $entry = $this->getLastEntry();

                echo date('Y-m-d H:i:s ');

                if ($this->getCache() != $entry['number']) {
                    exec("notify-send --urgency=critical --expire-time=5000 --icon=/usr/share/icons/Numix-Circle/scalable/apps/apport.svg '{$entry['title']}'");
                    $this->setCache($entry['number']);

                    echo $entry['title'] . PHP_EOL;

                    if ($this->config['notify']) {
                        try {
                            $sms = new SmsGateway();
                            $sms->send($this->config['phone'], $entry['title']);
                        } catch (SmsGatewayException $e) {
                            echo $e->getMessage() . PHP_EOL;
                        }
                    }
                }
            } catch (IssueTrackerException $e) {
                echo $e->getMessage() . PHP_EOL;
            }

            sleep($this->config['refresh']);
        }
    }

    /**
     * Gets atom feed from Redmine
     *
     * @return \SimpleXMLElement
     * @throws \terenaa\IssueTracker\IssueTrackerException
     */
    protected function getAtomFeed()
    {
        if (!($feed = simplexml_load_file("{$this->config['protocol']}://{$this->config['domain']}/projects/{$this->config['project']}/issues.atom?key={$this->config['auth_key']}"))) {
            throw new IssueTrackerException('Cannot load atom feed.');
        }

        return $feed;
    }

    /**
     * Gets last entry from Redmine atom feed
     *
     * @return array
     * @throws \terenaa\IssueTracker\IssueTrackerException
     */
    protected function getLastEntry()
    {
        $entry = $this->getAtomFeed()->entry[0];
        $fullTitle = preg_replace('/\((.*?)\)/', "({$entry->author->name})", $entry->title) . ' (' . date('Y-m-d H:i:s', strtotime($entry->updated)) . ')';
        $issueNumber = substr($entry->id, strpos($entry->id, '/') + 1);

        return array(
            'title' => $fullTitle,
            'number' => $issueNumber
        );
    }

    /**
     * Gets configuration settings from file
     */
    protected function getConfig()
    {
        $this->config = parse_ini_file(__DIR__ . '/../../config/config.ini');
    }

    /**
     * Gets last issue id from cache file
     *
     * @return null|string
     */
    protected function getCache()
    {
        $file = __DIR__ . '/../../config/issue.cache';

        if (file_exists($file)) {
            return file_get_contents($file);
        }

        return null;
    }

    /**
     * Saves last issue id into cache file
     *
     * @param $issue
     */
    protected function setCache($issue)
    {
        file_put_contents(__DIR__ . '/../../config/issue.cache', $issue);
    }
}
<commit_msg>Checking if config file exists<commit_after><?php
/**
 * Simple issue tracker for Redmine with text message notifications
 *
 * PHP version 5
 *
 * @category Utils
 * @author <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/MIT MIT
 * @version 1.0
 * @link https://www.github.com/terenaa/redmine-issue-tracker
 */

namespace terenaa\IssueTracker;

use terenaa\SmsGateway\SmsGateway;
use terenaa\SmsGateway\SmsGatewayException;


/**
 * Class IssueTracker
 * @package terenaa\IssueTracker
 */
class IssueTracker
{
    protected $config;

    /**
     * IssueTracker constructor.
     */
    public function __construct()
    {
        try {
            $this->getConfig();
        } catch (IssueTrackerException $e) {
            echo $e->getMessage() . PHP_EOL;
        }
    }

    /**
     * Runs tracker in infinite loop with specified intervals
     */
    public function run()
    {
        while (true) {
            try {
                $entry = $this->getLastEntry();

                echo date('Y-m-d H:i:s ');

                if ($this->getCache() != $entry['number']) {
                    exec("notify-send --urgency=critical --expire-time=5000 --icon=/usr/share/icons/Numix-Circle/scalable/apps/apport.svg '{$entry['title']}'");
                    $this->setCache($entry['number']);

                    echo $entry['title'] . PHP_EOL;

                    if ($this->config['notify']) {
                        try {
                            $sms = new SmsGateway();
                            $sms->send($this->config['phone'], $entry['title']);
                        } catch (SmsGatewayException $e) {
                            echo $e->getMessage() . PHP_EOL;
                        }
                    }
                }
            } catch (IssueTrackerException $e) {
                echo $e->getMessage() . PHP_EOL;
            }

            sleep($this->config['refresh']);
        }
    }

    /**
     * Gets atom feed from Redmine
     *
     * @return \SimpleXMLElement
     * @throws \terenaa\IssueTracker\IssueTrackerException
     */
    protected function getAtomFeed()
    {
        if (!($feed = simplexml_load_file("{$this->config['protocol']}://{$this->config['domain']}/projects/{$this->config['project']}/issues.atom?key={$this->config['auth_key']}"))) {
            throw new IssueTrackerException('Cannot load atom feed.');
        }

        return $feed;
    }

    /**
     * Gets last entry from Redmine atom feed
     *
     * @return array
     * @throws \terenaa\IssueTracker\IssueTrackerException
     */
    protected function getLastEntry()
    {
        $entry = $this->getAtomFeed()->entry[0];
        $fullTitle = preg_replace('/\((.*?)\)/', "({$entry->author->name})", $entry->title) . ' (' . date('Y-m-d H:i:s', strtotime($entry->updated)) . ')';
        $issueNumber = substr($entry->id, strpos($entry->id, '/') + 1);

        return array(
            'title' => $fullTitle,
            'number' => $issueNumber
        );
    }

    /**
     * Gets configuration settings from file
     */
    protected function getConfig()
    {
        if (!$this->config = parse_ini_file(__DIR__ . '/../../config/config.ini')) {
            throw new IssueTrackerException('Config.ini file missing.');
        };
    }

    /**
     * Gets last issue id from cache file
     *
     * @return null|string
     */
    protected function getCache()
    {
        $file = __DIR__ . '/../../config/issue.cache';

        if (file_exists($file)) {
            return file_get_contents($file);
        }

        return null;
    }

    /**
     * Saves last issue id into cache file
     *
     * @param $issue
     */
    protected function setCache($issue)
    {
        file_put_contents(__DIR__ . '/../../config/issue.cache', $issue);
    }
}

{'fraction_non_alphanumeric': 0.15567567567567567, 'fraction_numerical': 0.0024324324324324323, 'mean_word_length': 1.7875706214689266, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 60, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1979041', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2120, 'n_words': 417}