File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Products/ProductGroup/ProductGroupsController.php
<?php
namespace App\Products\ProductGroup;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\SectionController;
use App\Categories\Kms\CategoryModelService;
use App\Properties\Kms\PropertyService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
class ProductGroupsController extends SectionController
{
protected string $slug = "product_groups";
protected string $classModelName = ProductGroup::class;
protected ?string $forTranslationModelName = ProductGroupTranslation::class;
/** @var CategoryModelService */
private $categoryService;
private $propertyService;
public function __construct()
{
$section = new ProductGroupSection($this->slug);
$this->modelService = new ProductGroupModelService();
$this->categoryService = new CategoryModelService();
$this->propertyService = new PropertyService();
parent::__construct($section);
}
/**
* A function that MUST delegate to services to accomplish loading.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom Keys must be ValueFrom integers. Values must be Attributes that have the ValueFrom integers
* @return Collection The same collection as you've passed in. Only "filled".
*/
protected function load(Model $model, Collection $attributesByValueFrom = null): Collection
{
if($attributesByValueFrom === null) return new Collection();
$attributesByValueFrom = parent::load($model, $attributesByValueFrom);
$this->categoryService->load($model, $attributesByValueFrom->collapse());
$this->siteService->load($model, $attributesByValueFrom->get(Attribute::ValueFromItself));
$this->propertyService->load($model, $attributesByValueFrom->get(Attribute::ValueFromItself));
return $attributesByValueFrom;
}
/**
* A function that MUST delegate to services to accomplish saving.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom Keys must be ValueFrom integers. Values must be Attributes that have the ValueFrom integers
* @return Model
*/
protected function save(Model $model, Collection $attributesByValueFrom = null): Model
{
if($attributesByValueFrom === null) return $model;
$model = parent::save($model, $attributesByValueFrom);
$model = $this->categoryService->save($model, $attributesByValueFrom->collapse());
$model = $this->siteService->save($model, $attributesByValueFrom->get(Attribute::ValueFromItself));
$model = $this->propertyService->save($model, $attributesByValueFrom->get(Attribute::ValueFromItself));
return $model;
}
}