<commit_before><?php
  /* Copyright (C) 2011 by iRail vzw/asbl
   * © 2015 by Open Knowledge Belgium vzw/asbl
   *
   * This class foresees in basic HTTP functionality. It will get all the POST vars and put it in a request.
   *
   * @author <NAME>
   */

use MongoDB\Collection;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;

class APIPost
{
    private $postData;
    private $resourcename;
    private $log;

    public function __construct($resourcename, $postData)
    {
        //Default timezone is Brussels
        date_default_timezone_set('Europe/Brussels');

        $this->resourcename = $resourcename;
        $this->postData = json_decode($postData);

        try {
            $this->log = new Logger('irapi');
            //Create a formatter for the logs
            $logFormatter = new LineFormatter("%context%\n", 'Y-m-d\TH:i:s');
            $streamHandler = new StreamHandler(__DIR__ . '/../storage/irapi.log', Logger::INFO);
            $streamHandler->setFormatter($logFormatter);
            $this->log->pushHandler($streamHandler);
        } catch (Exception $e) {
            $this->buildError($e);
        }
    }

    public function writeToMongo($ip)
    {
        try {
            if ($this->resourcename == 'occupancy') {
                $this->occupancyToMongo($ip);
            }
        } catch (Exception $e) {
            $this->buildError($e);
        }
    }

    private function occupancyToMongo($ip)
    {
        if($this->postData->vehicle && $this->postData->from && $this->postData->to && $this->postData->occupancy) {
            try {
                $m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
                $ips = new MongoDB\Collection($m, 'spitsgids', 'IPsUsersLastMinute');

                // Delete the ips who are longer there than 1 minute
                $epochMinuteAgo = time() - 60;
                $ips->deleteMany(array('timestamp' => array('$lt' => $epochMinuteAgo)));

                // Find if the same IP posted the last minute
                $ipLastMinute = $ips->findOne(array('ip' => $ip));

                // If it didn't put it in the table and execute the post
                if(is_null($ipLastMinute)) {
                    $ips->insertOne(array('ip' => $ip, 'timestamp' => time()));

                    $postInfo = array(
                        'vehicle' => $this->postData->vehicle,
                        'from' => $this->postData->from,
                        'to' => $this->postData->to,
                        'occupancy' => $this->postData->occupancy,
                        'date' => date('Ymd')
                    );

                    // Insert the feedback
                    $feedback = new MongoDB\Collection($m, 'spitsgids', 'feedback');
                    $feedback->insertOne($postInfo);

                    // Log the post in the iRail log file
                    array_push($postInfo, $ip);
                    $this->writeLog($postInfo);
<commit_msg>Applied fixes from StyleCI<commit_after><?php
  /* Copyright (C) 2011 by iRail vzw/asbl
   * © 2015 by Open Knowledge Belgium vzw/asbl
   *
   * This class foresees in basic HTTP functionality. It will get all the POST vars and put it in a request.
   *
   * @author <NAME>
   */

use MongoDB\Collection;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;

class APIPost
{
    private $postData;
    private $resourcename;
    private $log;

    public function __construct($resourcename, $postData)
    {
        //Default timezone is Brussels
        date_default_timezone_set('Europe/Brussels');

        $this->resourcename = $resourcename;
        $this->postData = json_decode($postData);

        try {
            $this->log = new Logger('irapi');
            //Create a formatter for the logs
            $logFormatter = new LineFormatter("%context%\n", 'Y-m-d\TH:i:s');
            $streamHandler = new StreamHandler(__DIR__ . '/../storage/irapi.log', Logger::INFO);
            $streamHandler->setFormatter($logFormatter);
            $this->log->pushHandler($streamHandler);
        } catch (Exception $e) {
            $this->buildError($e);
        }
    }

    public function writeToMongo($ip)
    {
        try {
            if ($this->resourcename == 'occupancy') {
                $this->occupancyToMongo($ip);
            }
        } catch (Exception $e) {
            $this->buildError($e);
        }
    }

    private function occupancyToMongo($ip)
    {
        if ($this->postData->vehicle && $this->postData->from && $this->postData->to && $this->postData->occupancy) {
            try {
                $m = new MongoDB\Driver\Manager("mongodb://localhost:27017");
                $ips = new MongoDB\Collection($m, 'spitsgids', 'IPsUsersLastMinute');

                // Delete the ips who are longer there than 1 minute
                $epochMinuteAgo = time() - 60;
                $ips->deleteMany(array('timestamp' => array('$lt' => $epochMinuteAgo)));

                // Find if the same IP posted the last minute
                $ipLastMinute = $ips->findOne(array('ip' => $ip));

                // If it didn't put it in the table and execute the post
                if (is_null($ipLastMinute)) {
                    $ips->insertOne(array('ip' => $ip, 'timestamp' => time()));

                    $postInfo = array(
                        'vehicle' => $this->postData->vehicle,
                        'from' => $this->postData->from,
                        'to' => $this->postData->to,
                        'occupancy' => $this->postData->occupancy,
                        'date' => date('Ymd')
                    );

                    // Insert the feedback
                    $feedback = new MongoDB\Collection($m, 'spitsgids', 'feedback');
                    $feedback->insertOne($postInfo);

                    // Log the post in the iRail log file
                    array_push($postInfo, $ip);
                    $this->writeLog($postInfo);

{'fraction_non_alphanumeric': 0.1341684522828416, 'fraction_numerical': 0.00527443547057854, 'mean_word_length': 1.7834862385321102, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 87, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '57205', 'n_tokens_mistral': 1801, 'n_tokens_neox': 1664, 'n_words': 395}