Skip to content

Commit

Permalink
prepare job for missing corporation contact labels according to ccp-z…
Browse files Browse the repository at this point in the history
…oetrope feedback esi/esi-issues#779 (#68)
  • Loading branch information
warlof authored and leonjza committed Apr 28, 2018
1 parent 40bd8df commit b8864e6
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Jobs/Contacts/Corporation/Labels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015, 2016, 2017, 2018 Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

namespace Seat\Eveapi\Jobs\Contacts\Corporation;

use Seat\Eveapi\Jobs\EsiBase;
use Seat\Eveapi\Models\Contacts\CorporationContactLabel;

/**
* Class Labels.
* @package Seat\Eveapi\Jobs\Contacts\Corporation
*/
class Labels extends EsiBase
{
/**
* @var string
*/
protected $method = 'get';

/**
* @var string
*/
protected $endpoint = '/corporations/{corporation_id}/contacts/labels/';

/**
* @var string
*/
protected $version = 'v1';

/**
* @var string
*/
protected $scope = 'esi-corporations.read_contacts.v1';

/**
* @var array
*/
protected $tags = ['corporation', 'contacts', 'labels'];

/**
* Execute the job.
*
* @return void
* @throws \Exception
* @throws \Throwable
*/
public function handle()
{

if (! $this->authenticated()) return;

$labels = $this->retrieve([
'corporation_id' => $this->getCorporationId(),
]);

if ($labels->isCachedLoad()) return;

collect($labels)->each(function ($label) {

CorporationContactLabel::firstOrNew([
'corporation_id' => $this->getCorporationId(),
'label_id' => $label->label_id,
])->fill([
'label_name' => $label->label_name,
])->save();
});

CorporationContactLabel::where('corporation_id', $this->getCorporationId())
->whereNotIn('label_id', collect($labels)->pluck('label_id')->flatten()->all())
->delete();
}
}
45 changes: 45 additions & 0 deletions src/Models/Contacts/CorporationContactLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015, 2016, 2017, 2018 Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

namespace Seat\Eveapi\Models\Contacts;

use Illuminate\Database\Eloquent\Model;
use Seat\Eveapi\Traits\HasCompositePrimaryKey;

/**
* Class CorporationContactLabel.
* @package Seat\Eveapi\Models\Contacts
*/
class CorporationContactLabel extends Model
{
use HasCompositePrimaryKey;

/**
* @var bool
*/
protected static $unguarded = true;

/**
* @var array
*/
protected $primaryKey = ['corporation_id', 'label_id'];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015, 2016, 2017, 2018 Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCorporationContactLabelsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{

Schema::create('corporation_contact_labels', function (Blueprint $table) {

$table->bigInteger('corporation_id');
$table->bigInteger('label_id');
$table->string('label_name');

$table->primary(['corporation_id', 'label_id']);

$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{

Schema::dropIfExists('corporation_contact_labels');
}
}

0 comments on commit b8864e6

Please sign in to comment.