71 lines
1.7 KiB
PHP
71 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Empik\Marketplace\Utils;
|
|
|
|
use Configuration;
|
|
use Empik\Marketplace\Adapter\ConfigurationAdapter;
|
|
use Empik\Marketplace\Exception\LogicException;
|
|
|
|
class IdentifierExtractor
|
|
{
|
|
const IDENTIFIER_EAN = 'EAN';
|
|
const IDENTIFIER_SKU = 'SHOP_SKU';
|
|
|
|
/** @var string */
|
|
protected $identifierType;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->identifierType = Configuration::get(ConfigurationAdapter::CONF_OFFER_IDENTIFIER);
|
|
}
|
|
|
|
/**
|
|
* @return bool|false|string
|
|
*/
|
|
public function getIdentifierType()
|
|
{
|
|
return $this->identifierType;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* @throws LogicException
|
|
*/
|
|
public function getFilterKey()
|
|
{
|
|
switch ($this->identifierType) {
|
|
case self::IDENTIFIER_EAN:
|
|
$key = 'p.ean13';
|
|
break;
|
|
case self::IDENTIFIER_SKU:
|
|
$key = 'p.reference';
|
|
break;
|
|
default:
|
|
throw new LogicException(sprintf('Unknown identifier type: "%s"', $this->identifierType));
|
|
}
|
|
|
|
return $key;
|
|
}
|
|
|
|
/**
|
|
* @param array $product
|
|
* @return mixed|string
|
|
* @throws LogicException
|
|
*/
|
|
public function extract(array $product)
|
|
{
|
|
switch ($this->identifierType) {
|
|
case self::IDENTIFIER_EAN:
|
|
$identifier = isset($product['ean13']) ? $product['ean13'] : '';
|
|
break;
|
|
case self::IDENTIFIER_SKU:
|
|
$identifier = isset($product['reference']) ? $product['reference'] : '';
|
|
break;
|
|
default:
|
|
throw new LogicException(sprintf('Unknown identifier type: "%s"', $this->identifierType));
|
|
}
|
|
|
|
return $identifier;
|
|
}
|
|
}
|