jake-coder
12/20/2019 - 5:52 PM

Jobs, Entities, Queues, etc.

// Jobs are used to process file
// They have a handle that is similar to a constructor

// For the Sendgrid API we created an artisan command to trigger a job
// Then the job triggers the entity that returns the data from Sendgrid
// Then we pass the data to the database

class ImportStats implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $today = date('Y-m-d');
        $entity = new EventStatsEntity;
        $stats = $entity->getAllEvents($today);

        foreach ($stats as $day) {
            $eventStats = new EventStats;
            $eventStats->fill($day['stats'][0]['metrics']);
            $eventStats->created_at = $day['date'];
            $eventStats->save();
        }
    }
}
// Entities are located in Utilities
// They act like a model
// In this case we used an Entity to get data from the Sendgrid API

<?php

namespace App\Utilities\Sendgrid;

use App\Utilities\Sendgrid\Entity;

class EventStats extends Entity
{
    protected $fillable = ['created_at', 'blocks', 'bounce_drops', 'bounces', 'clicks', 'deferred', 'delivered',
                            'invalid_emails', 'opens', 'processed', 'requests', 'spam_report_drops', 'spam_reports',
                            'unique_clicks', 'unique_opens', 'unsubscribe_drops', 'unsubscribes'];

    public function __construct()
    {
        $this->sg = app('sendgrid');
    }

    public function getAllEvents($startDate)
    {
        $today = date('Y-m-d');
        $response = $this->sg->client->stats()->get(null, ['start_date' => $startDate]);
        
        if ($response->statusCode() != '200') {
            throw new SendgridException($response->body());
        }

        $parsedBody = json_decode($response->body(), true);
        return $parsedBody;
    }

    public function store(Request $request)
    {
        $stats = $this->getAllEvents($startDate);

        foreach ($stats as $day) {
            $eventStats = new EventStats;
            $eventStats->fill($day);
            $eventStats->save();
        }
    }

    public function save()
    {
        // Do I want to save anything here?
        // Will the process be to save and then send to the database or just send the data
    }
}
// The Model will send the data to the database

<?php

namespace App\Models\SendGrid;

use App\Models\Customer\Customer;
use Illuminate\Database\Eloquent\Model;

class EventStats extends Model
{
    protected $guarded = ['id'];
    protected $table = 'sendgrid.stats';
    const UPDATED_AT = false;
    public $timestamps = false;

////////////////////////////////////////////////////////////////// RELATIONSHIPS
    /**
     * @return Illuminate\Database\Elqouent\Relations\HasOne
     */
    public function customer()
    {
        return $this->hasOne(Customer::class, 'email', 'customer.email');
    }
}
// The command file is the artisan command
// This is calling the job
// Commands are located in app\Config\Commands

<?php

namespace App\Console\Commands;

use App\Jobs\Sendgrid\ImportStats;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;

class SendgridGetStats extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'sendgrid:getstats';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Retrieves Sendgrid Stats';

    public function handle()
    {
        $job = new ImportStats;
        if ($this->option('queue')) {
            $this->info('Retrieving Sendgrid Stats');
            $queue = ($this->option('queue')) ? $this->option('queue') : 'default';
            $job->dispatch()->onQueue($queue);
            return;
        }
        $job->handle();
    }


    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [
            //[$name, $shortcut, $mode, $description, $defaultValue]
            ['queue', null, InputOption::VALUE_OPTIONAL, 'The queue to push the jobs on', null],
        ];
    }
}