lightster/hodor

View on GitHub
src/Hodor/JobQueue/JobQueue.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

namespace Hodor\JobQueue;

use Exception;
use Hodor\Config\LoaderFactory;

class JobQueue
{
    /**
     * @var string
     */
    private $config_file;

    /**
     * @var Config
     */
    private $config;

    /**
     * @var QueueManager
     */
    private $queue_manager;

    /**
     * @param string $job_name the name of the job to run
     * @param array $params the parameters to pass to the job
     * @param array $options the options to use when running the job
     */
    public function push($job_name, array $params = [], array $options = [])
    {
        $buffer_queue = $this->getQueueManager()->getBufferQueueFactory()->getBufferQueueForJob(
            $job_name,
            $params,
            $options
        );

        $buffer_queue->push(
            $job_name,
            $params,
            $options
        );
    }

    public function beginBatch()
    {
        $this->getQueueManager()->getBufferQueueFactory()->beginBatch();
    }

    public function publishBatch()
    {
        $this->getQueueManager()->getBufferQueueFactory()->publishBatch();
    }

    public function discardBatch()
    {
        $this->getQueueManager()->getBufferQueueFactory()->discardBatch();
    }

    /**
     * @param string $config_file
     */
    public function setConfigFile($config_file)
    {
        $this->config_file = $config_file;
    }

    /**
     * @param Config $config
     */
    public function setConfig(Config $config)
    {
        $this->config = $config;
    }

    /**
     * @return Config
     * @throws Exception
     */
    public function getConfig()
    {
        if ($this->config) {
            return $this->config;
        }

        if ($this->config_file) {
            $config_loader_factory = new LoaderFactory();
            $this->config = $config_loader_factory->loadFromFile($this->config_file);
        } else {
            throw new Exception(
                "Config could not be found or generated by JobQueueFacade."
            );
        }

        return $this->config;
    }

    /**
     * @return QueueManager
     */
    private function getQueueManager()
    {
        if ($this->queue_manager) {
            return $this->queue_manager;
        }

        $this->queue_manager = new QueueManager($this->getConfig());

        return $this->queue_manager;
    }
}