<?php

namespace App\Console\Commands;

use App\Link;
use Illuminate\Console\Command;

class SplitCronData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cron:data';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Split Cron Data Command';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = public_path('links.txt');
        if (!file_exists($file)) {
            file_put_contents($file, "");
            chmod($file, 0777);
        } else {
            /*file_put_contents($file, "");*/
        }

        $fileLink = fopen($file, "a+");
        $links = Link::select('id', 'url', 'status')->isMagicwands()->isValid()->orderBy('id')->limit(50)->get();
        foreach ($links as $link) {
            fwrite($fileLink, "{$link->id}|{$link->url}|{$link->status}\r\n");
        }
        fclose($fileLink);
        logger("Push more urls.");
    }
}
