<?php

namespace inc;

class UpdateIdChecker
{

    public $file = '';
    public $data = [];

    public function __construct($id)
    {
        if ($id == null) {
            return true;
        }
        $this->init();


        if ($this->CheckExists($id)) {
            die('alerdy msg id');
        }
        $this->insertId($id);
    }

    private function init()
    {
        $this->file = realpath(dirname(__FILE__) . '/../Log/update.json');
        $this->data = json_decode(file_get_contents($this->file));
    }

    public function CheckExists($id)
    {
        if (in_array($id, $this->data, true)) {
            return true;
        }
        return false;
    }

    public function insertId($id)
    {
        array_push($this->data, $id);
        $this->saveData();
    }

    private function saveData()
    {
        $dt = $this->data;
        if ($this->data == null) {
            $dt = [];
        }
        file_put_contents($this->file, json_encode($dt));
    }
}