Completed
Pull Request — master (#132)
by
unknown
03:47
created

Factory::getJobRunner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.1852

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 2
cts 6
cp 0.3333
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
crap 3.1852
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers\JobRunner;
4
5
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpApplication;
6
use eXpansion\Framework\Core\DataProviders\Listener\ListenerInterfaceExpTimer;
7
use eXpansion\Framework\Core\Helpers\Structures\HttpRequest;
8
use eXpansion\Framework\Core\Helpers\Structures\HttpResult;
9
use eXpansion\Framework\Core\Services\Console;
10
use oliverde8\AsynchronousJobs\Job;
11
use oliverde8\AsynchronousJobs\Job\CallbackCurl;
12
use oliverde8\AsynchronousJobs\JobRunner;
13
use Psr\Log\LoggerInterface;
14
15
16
/**
17
 * Class Factory
18
 *
19
 * @author    de Cramer Oliver<[email protected]>
20
 * @copyright 2017 Smile
21
 * @package Tests\eXpansion\Framework\Core\Helpers\JobRunner
22
 */
23
class Factory implements ListenerInterfaceExpTimer
24
{
25
26
27
    /**
28
     * @var LoggerInterface
29
     */
30
    private $logger;
31
    /**
32
     * @var Console
33
     */
34
    private $console;
35
36 2
    public function __construct(LoggerInterface $logger, Console $console)
37
    {
38
39 2
        $this->logger = $logger;
40 2
        $this->console = $console;
41 2
    }
42
43
    /**
44
     * @return JobRunner
45
     */
46 2
    public function getJobRunner()
47
    {
48
        try {
49 2
            return JobRunner::getInstance('expansion', PHP_BINARY, 'var/tmp/asynchronous/');
50
        } catch (\Exception $ex) {
51
            $this->console->writeln('PHP exec is not enabled, therefore all http transport is disabled!');
52
            $this->logger->critical('PHP exec is not enabled, therefore all http transport is disabled!');
53
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method getJobRunner() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
54
        }
55
    }
56
57
    /**
58
     * @param $url
59
     * @param $callback
60
     * @param null|mixed $additionalData
61
     * @param array $options
62
     *
63
     * @param array|\stdClass $parameters one dimensional array or \stdClass with post key-value pairs
64
     * @return CallbackCurl
65
     */
66 1
    public function createCurlJob($url, $callback, $additionalData = null, $options = [], $parameters = [])
67
    {
68 1
        $curlJob = new HttpRequest();
69 1
        $curlJob->setCallback($callback);
70 1
        $curlJob->setUrl($url);
71 1
        $curlJob->setOptions($options);
72 1
        if (is_object($parameters)) {
73
            $parameters = (array)$parameters;
74
        }
75 1
        $curlJob->setParameters($parameters);
76 1
        $curlJob->setAdditionalData($additionalData);
77
78 1
        return $curlJob;
79
    }
80
81
    /**
82
     * @param Job $job
83
     */
84 1
    public function startJob(Job $job)
85
    {
86 1
        $job->start();
87 1
    }
88
89
90
    public function onPreLoop()
91
    {
92
93
    }
94
95 1
    public function onPostLoop()
96
    {
97 1
        $this->getJobRunner()->proccess();
98 1
    }
99
100
    public function onEverySecond()
101
    {
102
103
    }
104
}
105