Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
16.67% covered (danger)
16.67%
1 / 6
CRAP
26.67% covered (danger)
26.67%
4 / 15
Product
0.00% covered (danger)
0.00%
0 / 1
16.67% covered (danger)
16.67%
1 / 6
40.94
26.67% covered (danger)
26.67%
4 / 15
 variants
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getPriceAttribute
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 media
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 scopeSearch
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 anonymous function
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getUrl
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 5
<?php
namespace Mtc\Shop;
use Cache;
use Illuminate\Database\Eloquent\Model;
use Mtc\Core\Admin\Media;
use Mtc\Core\Admin\Node;
use Mtc\Core\Admin\Nodeable;
use Mtc\Shop\PriceMethods\PerProduct;
class Product extends Model
{
    use Nodeable;
    protected $fillable = [
        'price_method',
        'stock_enabled',
        'stock_status',
    ];
    protected $attributes = [
        'price_method' => PerProduct::class,
    ];
    public function variants()
    {
        return $this->hasMany(Variant::class);
    }
    public function getPriceAttribute()
    {
        $price_method = $this->price_method;
        if (!class_exists($price_method)) {
            throw new \Exception("Price method not found: {$price_method}");
        }
        return new $price_method($this);
    }
    public function media()
    {
        return $this->morphMany(Media::class, 'parent');
    }
    public function scopeSearch($query, $term)
    {
        if (!empty($term)) {
            $query->whereHas('node', function($query) use ($term) {
                $query->search($term);
            });
        }
        return $query;
    }
    public function getUrl($absolute = true, $node = false)
    {
        // As this may be called ahead of the node being saved, we need to
        // pass through.
        if ($node instanceOf Node === false) {
            $node = $this->node;
        }
        return Cache::remember(
            "shop.product:{$this->id}:url",
            30,
            function() use ($absolute, $node) {
                return route('shop.product', [$this, $node], $absolute);
            }
        );
    }
}