Skip to content

Commit

Permalink
Added the Channelable behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Sep 7, 2023
1 parent ce74a5d commit 7912799
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
- Added the `withImages` method to the product search class (eager loads media)
- Added the `channelables` table for being many-to-many polymorphic relationships with channels and arbitrary models
- Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection
- Added the `Channelable` behavior to Foundation Product, MasterProduct and Taxonomy classes
- Added and extended Foundation Channel model that contains the known relationships to the "channelable" models

## 3.x Series

Expand Down
26 changes: 26 additions & 0 deletions src/Channel/Traits/Channelable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* Contains the Channelable trait.
*
* @copyright Copyright (c) 2023 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2023-09-07
*
*/

namespace Vanilo\Channel\Traits;

use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Vanilo\Channel\Models\Channel;

trait Channelable
{
public function channels(): MorphToMany
{
return $this->morphToMany(Channel::class, 'channelable');
}
}
57 changes: 57 additions & 0 deletions src/Foundation/Models/Channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/**
* Contains the Channel class.
*
* @copyright Copyright (c) 2023 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2023-09-07
*
*/

namespace Vanilo\Foundation\Models;

use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Vanilo\Category\Models\TaxonomyProxy;
use Vanilo\Channel\Models\Channel as BaseChannel;
use Vanilo\MasterProduct\Models\MasterProductProxy;
use Vanilo\Payment\Models\PaymentMethodProxy;
use Vanilo\Product\Models\ProductProxy;
use Vanilo\Properties\Models\PropertyProxy;
use Vanilo\Shipment\Models\ShippingMethodProxy;

class Channel extends BaseChannel
{
public function products(): MorphToMany
{
return $this->morphToMany(ProductProxy::modelClass(), 'channelables');
}

public function masterProducts(): MorphToMany
{
return $this->morphToMany(MasterProductProxy::modelClass(), 'channelables');
}

public function shippingMethods(): MorphToMany
{
return $this->morphToMany(ShippingMethodProxy::modelClass(), 'channelables');
}

public function paymentMethods(): MorphToMany
{
return $this->morphToMany(PaymentMethodProxy::modelClass(), 'channelables');
}

public function taxonomies(): MorphToMany
{
return $this->morphToMany(TaxonomyProxy::modelClass(), 'channelables');
}

public function properties(): MorphToMany
{
return $this->morphToMany(PropertyProxy::modelClass(), 'channelables');
}
}
2 changes: 2 additions & 0 deletions src/Foundation/Models/MasterProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Vanilo\Category\Traits\HasTaxons;
use Vanilo\Channel\Traits\Channelable;
use Vanilo\Foundation\Traits\LoadsMediaConversionsFromConfig;
use Vanilo\MasterProduct\Models\MasterProduct as BaseMasterProduct;
use Vanilo\Properties\Traits\HasPropertyValues;
Expand All @@ -27,6 +28,7 @@
class MasterProduct extends BaseMasterProduct implements HasMedia
{
use BelongsToTaxCategory;
use Channelable;
use InteractsWithMedia;
use HasImagesFromMediaLibrary;
use LoadsMediaConversionsFromConfig;
Expand Down
2 changes: 2 additions & 0 deletions src/Foundation/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Vanilo\Category\Traits\HasTaxons;
use Vanilo\Channel\Traits\Channelable;
use Vanilo\Contracts\Buyable;
use Vanilo\Foundation\Traits\LoadsMediaConversionsFromConfig;
use Vanilo\Product\Models\Product as BaseProduct;
Expand All @@ -30,6 +31,7 @@ class Product extends BaseProduct implements Buyable, HasMedia
{
use BelongsToTaxCategory;
use BuyableModel;
use Channelable;
use InteractsWithMedia;
use HasImagesFromMediaLibrary;
use LoadsMediaConversionsFromConfig;
Expand Down
2 changes: 2 additions & 0 deletions src/Foundation/Models/Taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Vanilo\Category\Models\Taxonomy as BaseTaxonomy;
use Vanilo\Channel\Traits\Channelable;
use Vanilo\Contracts\HasImages;
use Vanilo\Foundation\Traits\LoadsMediaConversionsFromConfig;
use Vanilo\Support\Traits\HasImagesFromMediaLibrary;

class Taxonomy extends BaseTaxonomy implements HasMedia, HasImages
{
use Channelable;
use InteractsWithMedia;
use HasImagesFromMediaLibrary;
use LoadsMediaConversionsFromConfig;
Expand Down
5 changes: 5 additions & 0 deletions src/Foundation/Providers/ModuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
use Vanilo\Category\Contracts\Taxonomy as TaxonomyContract;
use Vanilo\Category\Models\TaxonomyProxy;
use Vanilo\Category\Models\TaxonProxy;
use Vanilo\Channel\Contracts\Channel as ChannelContract;
use Vanilo\Channel\Models\ChannelProxy;
use Vanilo\Checkout\Contracts\CheckoutDataFactory as CheckoutDataFactoryContract;
use Vanilo\Foundation\Factories\CheckoutDataFactory;
use Vanilo\Foundation\Factories\OrderFactory;
use Vanilo\Foundation\Models\Address;
use Vanilo\Foundation\Models\Cart;
use Vanilo\Foundation\Models\CartItem;
use Vanilo\Foundation\Models\Channel;
use Vanilo\Foundation\Models\Customer;
use Vanilo\Foundation\Models\MasterProduct;
use Vanilo\Foundation\Models\MasterProductVariant;
Expand Down Expand Up @@ -70,6 +73,7 @@ public function boot()

// Use the foundation's extended model classes
$registerRouteModels = config('concord.register_route_models', true);
$this->concord->registerModel(ChannelContract::class, Channel::class, $registerRouteModels);
$this->concord->registerModel(ProductContract::class, Product::class, $registerRouteModels);
$this->concord->registerModel(AddressContract::class, Address::class, $registerRouteModels);
$this->concord->registerModel(CustomerContract::class, Customer::class, $registerRouteModels);
Expand All @@ -93,6 +97,7 @@ public function boot()
'order' => OrderProxy::modelClass(),
'cart' => CartProxy::modelClass(),
'shipment' => ShipmentProxy::modelClass(),
'channel' => ChannelProxy::modelClass(),
]);

ShippingFeeCalculators::register(FlatFeeCalculator::ID, FlatFeeCalculator::class);
Expand Down

0 comments on commit 7912799

Please sign in to comment.