b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / dev2.destoffenstraat.com / vendor / amasty / base / Model / Feed /
Filename/home/dev2.destoffenstraat.com/vendor/amasty/base/Model/Feed/AdsProvider.php
Size4.55 kb
Permissionrw-r--r--
Ownerroot : root
Create time17-Aug-2025 10:26
Last modified16-Aug-2022 09:35
Last accessed23-Aug-2025 03:56
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package Magento 2 Base Package
*/

namespace Amasty\Base\Model\Feed;

use Amasty\Base\Model\Feed\FeedTypes\Ads;
use Magento\Framework\Module\FullModuleList;

/**
* Class AdsProvider provide ads data
*/
class AdsProvider
{
/**
* @var FullModuleList
*/
private $moduleList;

/**
* @var Ads
*/
private $adsFeed;

/**
* @var array
*/
private $upsellModuleAds = [];

public function __construct(
FullModuleList $moduleList,
Ads $adsFeed
) {
$this->moduleList = $moduleList;
$this->adsFeed = $adsFeed;
}

/**
* @param string $moduleCode
*
* @return array
*/
public function getDisplayAdvertise($moduleCode)
{
$adsData = $this->adsFeed->execute();

return $this->getActiveAdvertise($adsData, $moduleCode);
}

/**
* Sorting $sortAds by priority in CSV
*
* @param array $currentElement
* @param array $nextElement
*
* @return int
*/
protected function sortByPriority($currentElement, $nextElement)
{
if ($currentElement['priority'] == $nextElement['priority']) {
return 0;
}

return ($currentElement['priority'] < $nextElement['priority']) ? -1 : 1;
}

/**
* @param array $adsData
* @param string $moduleCode
*
* @return array
*/
private function getActiveAdvertise($adsData, $moduleCode)
{
$upsellModuleAds = $this->getUpsell($adsData, $moduleCode);

foreach ($upsellModuleAds as $advertise) {
if (isset($advertise['upsell_module_code']) && !empty($advertise['upsell_module_code'])
&& !$this->moduleList->has($advertise['upsell_module_code'])
) {
return $advertise;
}
}

return [];
}

/**
* @param array $adsData
* @param string $moduleCode
*
* @return array
*/
private function getUpsell($adsData, $moduleCode)
{
if (isset($this->upsellModuleAds[$moduleCode])) {
return $this->upsellModuleAds[$moduleCode];
}
// If you need to return information sorted by priority, uncomment the following line
// return $this->upsellModuleAds[$moduleCode] = $this->getUpsellSortedByPriority($adsData, $moduleCode);
return $this->upsellModuleAds[$moduleCode] = $this->getUpsellRandom($adsData, $moduleCode);
}

/**
* @param array $adsData
* @param string $moduleCode
*
* @return array
*/
private function getUpsellSortedByPriority($adsData, $moduleCode)
{
$sortAds = [];
$emptyPriority = [];

foreach ($adsData as $moduleAds) {
if ((isset($moduleAds['module_code']) && $moduleAds['module_code'] === $moduleCode)
|| ($this->isAllowedEverywhere($moduleAds) && 'Amasty_Base' !== $moduleCode)
) {
if (isset($moduleAds['priority'])) {
$moduleAds['priority'] = str_replace(' ', '', $moduleAds['priority']);

if ($moduleAds['priority'] === '' || !is_numeric($moduleAds['priority'])) {
$emptyPriority[] = $moduleAds;
} else {
$priority = (int)$moduleAds['priority'];

while (isset($sortAds[$priority])) {
$priority++;
}
$sortAds[$priority] = $moduleAds;
}
}
}
}

usort($sortAds, [$this, 'sortByPriority']);

if (!empty($emptyPriority)) {
end($sortAds); // move the internal pointer to the end of the array
$lastKeySortAds = key($sortAds) + 1;

foreach ($emptyPriority as $emptyPriorityElement) {
$sortAds[$lastKeySortAds] = $emptyPriorityElement;
$lastKeySortAds++;
}
}

return $sortAds;
}

/**
* @param array $adsData
* @param string $moduleCode
*
* @return array
*/
private function getUpsellRandom($adsData, $moduleCode)
{
$sortAds = $this->getUpsellSortedByPriority($adsData, $moduleCode);
shuffle($sortAds);

return $sortAds;
}

/**
* @param array $moduleAds
*
* @return bool
*/
private function isAllowedEverywhere($moduleAds)
{
return isset($moduleAds['module_code']) && '*' === $moduleAds['module_code'];
}
}