-
Notifications
You must be signed in to change notification settings - Fork 5
/
AsyncServiceFactory.php
101 lines (84 loc) · 2.17 KB
/
AsyncServiceFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace Krlove\AsyncServiceCallBundle;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\PhpExecutableFinder;
/**
* Class AsyncServiceFactory
* @package Krlove\AsyncServiceCallBundle
*/
class AsyncServiceFactory
{
/**
* @var Filesystem
*/
protected $filesystem;
/**
* @var string
*/
protected $rootDir;
/**
* @var string
*/
protected $consolePath;
/**
* @var string
*/
protected $phpPath;
/**
* AsyncServiceFactory constructor.
* @param Filesystem $filesystem
* @param string $rootDir
* @param string $consolePath
* @param string $phpPath
*/
public function __construct(
Filesystem $filesystem,
$rootDir,
$consolePath,
$phpPath
) {
$this->filesystem = $filesystem;
$this->rootDir = $rootDir;
$this->consolePath = $consolePath;
$this->phpPath = $phpPath;
}
/**
* @return AsyncService
*/
public function createAsyncService()
{
$consolePath = $this->resolveConsolePath();
$phpPath = $this->resolvePhpPath();
return new AsyncService($consolePath, $phpPath);
}
/**
* @return string
*/
protected function resolveConsolePath()
{
$consolePath = $this->consolePath;
if (!$this->filesystem->isAbsolutePath($consolePath)) {
$consolePath = $this->rootDir . '/../' . $consolePath;
}
if (!$this->filesystem->exists($consolePath)) {
throw new FileNotFoundException(sprintf('File %s doesn\'t exist', $consolePath));
}
return $consolePath;
}
/**
* @return string
*/
protected function resolvePhpPath()
{
$phpPath = $this->phpPath;
if ($phpPath === null) {
$finder = new PhpExecutableFinder();
$phpPath = $finder->find();
}
if (!$this->filesystem->exists($phpPath)) {
throw new FileNotFoundException(sprintf('PHP executable %s doesn\'t exist', $phpPath));
}
return $phpPath;
}
}