81 lines
2.3 KiB
PHP
81 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace KupShop\RecommendersBundle\Recommenders;
|
|
|
|
use KupShop\CatalogBundle\ProductList\ProductCollection;
|
|
|
|
abstract class AbstractRecommender implements IRecommender
|
|
{
|
|
protected static string $type;
|
|
protected static string $name;
|
|
protected static string $context = 'default';
|
|
protected static bool $random = false;
|
|
protected static ?string $in_store = 'supplier'; // in_store, supplier, all...
|
|
|
|
public function __construct(protected RecommendersLocator $recommendersLocator)
|
|
{
|
|
}
|
|
|
|
public static function getName(array $data = []): string
|
|
{
|
|
return static::$name;
|
|
}
|
|
|
|
public static function getType(): string
|
|
{
|
|
return static::$type;
|
|
}
|
|
|
|
public function getTemplate(): string
|
|
{
|
|
return 'recommenders/recommender.'.static::$type.'.tpl';
|
|
}
|
|
|
|
public static function getRandom(): bool
|
|
{
|
|
return static::$random;
|
|
}
|
|
|
|
public static function getInStore(): ?string
|
|
{
|
|
return static::$in_store;
|
|
}
|
|
|
|
public function getConfigurationVariables(array $data): array
|
|
{
|
|
$config = ($data['data'] ?? []);
|
|
|
|
if (is_string($config)) {
|
|
$config = json_decode($config, true);
|
|
}
|
|
|
|
if (!is_array($config) && !is_object($config)) {
|
|
$config = [];
|
|
}
|
|
|
|
if (!empty($data['products_count'])) {
|
|
$config['count'] = $data['products_count'];
|
|
}
|
|
if ($this->getInStore()) {
|
|
$config['in_store'] = $config['in_store'] ?? $this->getInStore();
|
|
}
|
|
if ($fallback = $config['fallback'] ?? null) {
|
|
unset($config['fallback']);
|
|
if (!empty($fallback['type']) && (($fallback['enabled'] ?? 'N') == 'Y')) {
|
|
$recommender = $this->recommendersLocator->getRecommender($fallback['type']);
|
|
$fallbackConfig = $recommender->getConfigurationVariables($fallback);
|
|
$config['fallback'] = [array_merge($fallbackConfig, ['type' => $fallback['type']])];
|
|
}
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
public function execute(array $data, array $config): ProductCollection
|
|
{
|
|
$params = array_merge($config, $data, ['type' => $this::$type]);
|
|
|
|
return (new \InsertProductsTypes())->getProducts($params);
|
|
}
|
|
}
|