Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/DataSync/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/DataSync/Model/JobStack.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Hervé Guétin <[email protected]> <@herveguetin>
 * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr)
 */

namespace Soon\DataSync\Model;

use Magento\Framework\ObjectManagerInterface;
use Soon\DataSync\Model\Job\JobInterface;

class JobStack implements JobStackInterface
{
    /**
     * @var ObjectManagerInterface
     */
    private $objectManager;
    /**
     * @var array
     */
    private $queue = [];
    /**
     * @var array
     */
    private $jobs;

    public function __construct(
        ObjectManagerInterface $objectManager,
        array $jobs = []
    )
    {
        $this->objectManager = $objectManager;
        $this->jobs = $jobs;

        $this->sort();
    }

    private function sort()
    {
        uasort($this->jobs, function ($a, $b) {
            return $a['sort_order'] > $b['sort_order'];
        });
    }

    /**
     * @param array $options
     * @return array|JobInterface[]
     */
    public function addAll(array $options = []): array
    {
        array_map(function ($code) use ($options) {
            $this->add($code, $options);
        }, array_keys($this->jobs));

        return $this->queue();
    }

    /**
     * @param string $code
     * @param array $options
     * @return JobInterface
     */
    public function add(string $code, array $options = []): JobInterface
    {
        if ($this->canRun($code)) {
            $job = $this->jobs[$code]['type'];
            $this->queue[] = ['code' => $code, 'job' => $job, 'options' => $options];

            return $job;
        }
    }

    /**
     * @param string $code
     * @return bool
     * @throws \Exception
     */
    private function canRun($code)
    {
        if (!isset($this->jobs[$code])) {
            throw new \Exception(sprintf('Job with code %s does not exist', $code));
        }

        return (bool)$this->jobs[$code]['active'];
    }

    /**
     * @return JobInterface[]
     */
    public function queue(): array
    {
        return $this->queue;
    }

    /**
     * @param string $code
     * @throws \Exception
     */
    public function run(string $code)
    {
        $jobKey = array_search($code, array_column($this->queue, 'code'));

        if ($jobKey === false) {
            throw new \Exception("{$code} is not in queue.");
        }

        /** @var JobInterface $job */
        $job = $this->queue[$jobKey]['job'];
        $job->setOptions($this->queue[$jobKey]['options']);

        return $job->run();
    }
}

Spamworldpro Mini