ProcessProposals.php 2.73 KB
Newer Older
1 2 3 4
<?php

namespace App\Console\Commands;

5
use App\Project;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Check for changes to proposals';

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

    /**
     * Execute the console command.
     */
    public function handle()
    {
40
        $details = [];
41 42 43
        $files = Storage::files('ffs-proposals');
        foreach ($files as $file) {
            if (strpos($file,'.md')) {
44 45 46
                $detail['name'] = $file;
                $detail['values'] = $this->getAmountFromText($file);
                $details[] = $detail['values']['title'];
47
                $project = Project::where('title', $detail['values']['title'])->first();
48 49
                if ($project) {
                    $project->filename = $file;
50 51 52
                    if ($project->state === 'IDEA') {
                        $project->state = 'FUNDING-REQUIRED';
                    }
53 54 55
                    $project->target_amount = $detail['values']['amount'];
                    $project->save();
                }
56 57
            }
        }
58 59 60 61
        foreach ($details as $det) {
            $this->line($det);
        }

62 63 64
    }

    /**
65
     * Gets the ffs variables out the top of the file
66 67
     *
     * @param string $filename
68
     * @return array
69 70 71 72 73 74
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    public function getAmountFromText($filename = 'additional-gui-dev.md')
    {
        $input = Storage::get($filename);
        $lines = preg_split('/\r\n|\r|\n/', $input);
75 76 77 78
        $values = [];
        $flag = false;
        $isPayoutLine = false;

79
        foreach($lines as $line) {
80 81 82 83 84 85 86
            if ($line === '---') {
                if ($flag === true) {
                    break;
                }
                $flag = true;
                continue;
            }
87
            $details = explode(':', $line);
88 89 90 91 92 93 94 95 96 97 98
            if (count($details) < 2) {
                continue;
            }
            if ($details[0] === 'payouts') {
                $isPayoutLine = true;
                continue;
            }
            if ($isPayoutLine) {
                $name = trim(str_replace('-','', $details[0]));
                $values['payouts'][][$name] = ltrim($details[1]);
                continue;
99
            }
100
            $values[$details[0]] = ltrim($details[1]);
101
        }
102
        return $values;
103 104
    }
}