<?php

class CallProtectionCronJob extends AbstractCronJob
{

    public function run()
    {
        $job = Job::model()->findByAttributes(array('type' => Job::CallProtectionImport));

        if (!$job) {
            $job = new Job();
            $job->type = Job::CallProtectionImport;
            $job->meta = array();
            $job->status = Job::Inserted;
            $job->save();

            $scorelistEntryStatus = ScoreListEntryStatus::model()->findByAttributes(array());
            if (!$scorelistEntryStatus) {
                $scorelistEntryStatus = new ScoreListEntryStatus();
            }
            $scorelistEntryStatus->status = 'runs';
            $scorelistEntryStatus->message = __('CALL_PROTECTION_IMPORT_RUNNING_TEXT');
            $scorelistEntryStatus->datetime = PhonalisaDate::newServerDate();
            $scorelistEntryStatus->save();

            Log::v('CallProtection import started.', 'SystemOverview');
        }
        CallProtection::importScoreEntries('CallProtectionCronJob');
    }

    public function getCronRule()
    {
        if (!CallProtection::getAPIKey()) {
            return '';
        }

        return cfg('CALL_PROTECTION_CRON_JOB');
    }
}
