ProcessManager   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 10
dl 0
loc 151
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setConfigRuntime() 0 5 1
A createDefault() 0 5 1
A addTransports() 0 8 1
A siteProcess() 0 6 1
A process() 0 4 1
A shell() 0 4 1
A add() 0 5 1
A hasTransport() 0 4 1
A getTransport() 0 11 2
A getTransportFactory() 0 9 3
1
<?php
2
3
namespace Consolidation\SiteProcess;
4
5
use Consolidation\SiteProcess\Factory\VagrantTransportFactory;
6
use Psr\Log\LoggerInterface;
7
use Consolidation\SiteAlias\SiteAliasInterface;
8
use Consolidation\SiteProcess\Factory\SshTransportFactory;
9
use Consolidation\SiteProcess\Factory\DockerComposeTransportFactory;
10
use Consolidation\SiteProcess\Factory\TransportFactoryInterface;
11
use Consolidation\SiteProcess\Transport\LocalTransport;
12
use Symfony\Component\Process\Process;
13
use Consolidation\Config\Config;
14
use Consolidation\Config\ConfigInterface;
15
use Consolidation\Config\ConfigAwareInterface;
16
use Consolidation\Config\ConfigAwareTrait;
17
use Consolidation\SiteAlias\SiteAliasWithConfig;
18
19
/**
20
 * ProcessManager will create a SiteProcess to run a command on a given
21
 * site as indicated by a SiteAlias.
22
 *
23
 * ProcessManager also manages a collection of transport factories, and
24
 * will produce transport instances as needed for provided site aliases.
25
 */
26
class ProcessManager implements ConfigAwareInterface
27
{
28
    use ConfigAwareTrait;
29
30
    /** @var ConfigInterface */
31
    protected $configRuntime;
32
33
    protected $transportFactories = [];
34
35
    public function __construct()
36
    {
37
        $this->config = new Config();
38
        $this->configRuntime = new Config();
39
    }
40
41
    /**
42
     * setConfigRuntime sets the config object that holds runtime config
43
     * items, i.e. config set from the commandline rather than a config file.
44
     * Configuration priority (highest to lowest) is:
45
     *   - config runtime
46
     *   - site alias
47
     *   - config files
48
     */
49
    public function setConfigRuntime(ConfigInterface $configRuntime)
50
    {
51
        $this->configRuntime = $configRuntime;
52
        return $this;
53
    }
54
55
    /**
56
     * createDefault creates a Transport manager and add the default transports to it.
57
     */
58
    public static function createDefault()
59
    {
60
        $processManager = new self();
61
        return static::addTransports($processManager);
62
    }
63
64
    /**
65
     * addTransports adds the avaiable transports to the
66
     * provided process manager.
67
     */
68
    public static function addTransports(ProcessManager $processManager)
69
    {
70
        $processManager->add(new SshTransportFactory());
71
        $processManager->add(new DockerComposeTransportFactory());
72
        $processManager->add(new VagrantTransportFactory());
73
74
        return $processManager;
75
    }
76
77
    /**
78
     * Return a site process configured with an appropriate transport
79
     *
80
     * @param SiteAliasInterface $siteAlias Target for command
81
     * @param array $args Command arguments
82
     * @param array $options Associative array of command options
83
     * @param array $optionsPassedAsArgs Associtive array of options to be passed as arguments (after double-dash)
84
     * @return Process
85
     */
86
    public function siteProcess(SiteAliasInterface $siteAlias, $args = [], $options = [], $optionsPassedAsArgs = [])
87
    {
88
        $transport = $this->getTransport($siteAlias);
89
        $process = new SiteProcess($siteAlias, $transport, $args, $options, $optionsPassedAsArgs);
90
        return $process;
91
    }
92
93
    /**
94
     * Create a Process instance from a commandline string.
95
     * @param array $command The command to run and its arguments listed as separate entries
96
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
97
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
98
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
99
     * @param int|float|null $timeout The timeout in seconds or null to disable
100
     * @return Process
101
     */
102
    public function process($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
103
    {
104
        return new ProcessBase($command, $cwd, $env, $input, $timeout);
105
    }
106
107
    /**
108
     * Create a Process instance from a commandline string.
109
     * @param string $command The commandline string to run
110
     * @param string|null $cwd     The working directory or null to use the working dir of the current PHP process
111
     * @param array|null $env     The environment variables or null to use the same environment as the current PHP process
112
     * @param mixed|null $input   The input as stream resource, scalar or \Traversable, or null for no input
113
     * @param int|float|null $timeout The timeout in seconds or null to disable
114
     * @return Process
115
     */
116
    public function shell($command, $cwd = null, array $env = null, $input = null, $timeout = 60)
117
    {
118
        return ProcessBase::fromShellCommandline($command, $cwd, $env, $input, $timeout);
119
    }
120
121
    /**
122
     * add a transport factory to our factory list
123
     * @param TransportFactoryInterface $factory
124
     */
125
    public function add(TransportFactoryInterface $factory)
126
    {
127
        $this->transportFactories[] = $factory;
128
        return $this;
129
    }
130
131
    /**
132
     * hasTransport determines if there is a transport that handles the
133
     * provided site alias.
134
     *
135
     * @param SiteAliasInterface $siteAlias
136
     * @return boolean
137
     */
138
    public function hasTransport(SiteAliasInterface $siteAlias)
139
    {
140
        return $this->getTransportFactory($siteAlias) !== null;
141
    }
142
143
    /**
144
     * getTransport returns a transport that is applicable to the provided site alias.
145
     *
146
     * @param SiteAliasInterface $siteAlias
147
     * @return TransportInterface
148
     */
149
    public function getTransport(SiteAliasInterface $siteAlias)
150
    {
151
        $factory = $this->getTransportFactory($siteAlias);
152
153
        $siteAliasWithConfig = SiteAliasWithConfig::create($siteAlias, $this->config, $this->configRuntime);
154
155
        if ($factory) {
156
            return $factory->create($siteAliasWithConfig);
157
        }
158
        return new LocalTransport();
159
    }
160
161
    /**
162
     * getTransportFactory returns a factory for the provided site alias.
163
     *
164
     * @param SiteAliasInterface $siteAlias
165
     * @return TransportFactoryInterface
166
     */
167
    protected function getTransportFactory(SiteAliasInterface $siteAlias)
168
    {
169
        foreach ($this->transportFactories as $factory) {
170
            if ($factory->check($siteAlias)) {
171
                return $factory;
172
            }
173
        }
174
        return null;
175
    }
176
}
177