Tokens.php 8.75 KB
<?php

namespace App\Models;

use App\Models\Pivots\DictionaryCampaign;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Tokens
 *
 * @property int $id
 * @property string $token
 * @property string $login
 * @property string $api
 * @property string|null $type
 * @property int $created_by
 * @property \Illuminate\Support\Carbon|null $check_changes
 * @property \Illuminate\Support\Carbon $check_changes_at
 * @property \Illuminate\Support\Carbon|null $check_changes_campaign
 * @property \Illuminate\Support\Carbon $check_changes_campaign_at
 * @property \Illuminate\Support\Carbon|null $check_changes_ad_group
 * @property \Illuminate\Support\Carbon $check_changes_ad_group_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property int $limit
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaigns
 * @property-read int|null $campaigns_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsAdGroups
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\AdGroup[] $campaignsAdGroupsForUpdatedSelf
 * @property-read int|null $campaigns_ad_groups_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsNotEnabledNotDisabled
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledDisabled
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Campaigns[] $campaignsEnabledNotDisabledUpdatedChildren
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $cities
 * @property-read int|null $cities_count
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaigns
 * @property-read int|null $dictionary_campaigns_count
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsForExternal
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForNotExternal
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsNotEnabledForExternalNotDisabled
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalDisabled
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalNeedUpdated
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf
 * @property-read \Illuminate\Database\Eloquent\Collection|DictionaryCampaign[] $dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsForManaged
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dictionary[] $campaignsNotForManaged
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Limits[] $limits
 * @property-read int|null $limits_count
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens filter(array $filters)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens query()
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereApi($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCreatedBy($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereLimit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereLogin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCheckChanges($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCheckCampaign($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereCheckAdGroup($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Tokens whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Tokens extends Model
{
    CONST MAIN = 'main';
    CONST GOAL = 'goal';

    protected $fillable = [
        'token',
        'login',
        'type',
        'check_changes',
        'check_changes_campaign',
        'check_changes_ad_group',
    ];

    protected $casts = [
        'check_changes' => 'datetime',
        'check_changes_campaign' => 'datetime',
        'check_changes_ad_group' => 'datetime',
    ];

    public function getCheckChangesAtAttribute()
    {
        return $this->check_changes
            ? $this->check_changes
            : Carbon::now()->addDays(-1);
    }

    public function getCheckChangesCampaignAtAttribute()
    {
        return $this->check_changes_campaign
            ? $this->check_changes_campaign
            : Carbon::now()->addDays(-1);
    }

    public function getCheckChangesAdGroupAtAttribute()
    {
        return $this->check_changes_ad_group
            ? $this->check_changes_ad_group
            : Carbon::now()->addDays(-1);
    }

    public function scopeFilter($query, array $filters)
    {
        $query->when($filters['login'] ?? null, function ($query, $search) {
            $query->where('login', 'like', '%'.$search.'%');
        })->when($filters['type'] ?? null, function ($query, $type) {
            $query->where('type', $type);
        })->when($filters['api'] ?? null, function ($query, $api) {
            $query->where('api', $api);
        });
    }

    public function isMain()
    {
        return $this->type === $this::MAIN;
    }

    public function limits()
    {
        return $this->hasMany(Limits::class, 'token', 'id')
            ->orderBy('updated_at', 'DESC');
    }

    public function campaigns()
    {
        return $this->hasMany(Campaigns::class, 'token', 'id')
            ->orderBy('updated_at', 'DESC');
    }

    public function campaignsAdGroups()
    {
        return $this->hasManyThrough(AdGroup::class, Campaigns::class, 'token', 'campaign_id');
    }

    public function campaignsAdGroupsForUpdatedSelf()
    {
        return $this->campaignsAdGroups()->forUpdatedSelf();
    }

    public function campaignsNotEnabledNotDisabled()
    {
        return $this->campaigns()->forEnabled(false)->notDisabled();
    }

    public function campaignsEnabledDisabled()
    {
        return $this->campaigns()->forEnabled()->disabled();
    }

    public function campaignsEnabledNotDisabledUpdatedChildren()
    {
        return $this->campaigns()->forEnabled()->notDisabled()->forUpdatedChildren();
    }

    public function cities()
    {
        return $this->hasMany(Dictionary::class, 'token_id', 'id')
            ->orderBy('updated_at', 'DESC');
    }

    public function dictionaryCampaigns()
    {
        return $this->hasManyThrough(DictionaryCampaign::class, Dictionary::class, 'token_id', 'dictionary_id')
            ->orderBy(DictionaryCampaign::getModel()->getTable() . '.created_at', 'ASC')
            ->orderBy(DictionaryCampaign::getModel()->getTable() . '.' . DictionaryCampaign::getModel()->getKeyName(), 'ASC');
    }

    public function dictionaryCampaignsForExternal()
    {
        return $this->dictionaryCampaigns()->forExternal();
    }

    public function dictionaryCampaignsEnabledForNotExternal()
    {
        return $this->dictionaryCampaigns()->enabled()->forNotExternal();
    }

    public function dictionaryCampaignsNotEnabledForExternalNotDisabled()
    {
        return $this->dictionaryCampaigns()->enabled(false)->forExternal()->notDisabled();
    }

    public function dictionaryCampaignsEnabledForExternalDisabled()
    {
        return $this->dictionaryCampaigns()->enabled()->forExternal()->disabled();
    }

    public function dictionaryCampaignsEnabledForExternalNeedUpdated()
    {
        return $this->dictionaryCampaigns()->enabled()->forExternal()->needUpdated();
    }

    public function dictionaryCampaignsEnabledForExternalSynchronizedUpdatedSelf()
    {
        return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedSelf();
    }

    public function dictionaryCampaignsEnabledForExternalSynchronizedUpdatedChildren()
    {
        return $this->dictionaryCampaignsForExternal()->enabled()->synchronized()->forUpdatedChildren();
    }

    public function campaignsForManaged()
    {
        return $this->campaigns()->forManaged();
    }

    public function campaignsNotForManaged()
    {
        return $this->campaigns()->forManaged(false);
    }
}