Completed
Push — master ( 7233ee...46d0e2 )
by Greg
01:19
created

TransportManager::createDefault()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Consolidation\SiteProcess;
4
5
use Psr\Log\LoggerInterface;
6
use Consolidation\SiteAlias\AliasRecord;
7
use Consolidation\SiteProcess\Factory\SshTransportFactory;
8
use Consolidation\SiteProcess\Factory\DockerComposeTransportFactory;
9
use Consolidation\SiteProcess\Factory\TransportFactoryInterface;
10
use Consolidation\SiteProcess\Transport\LocalTransport;
11
12
/**
13
 * TransportManager manages a collection of transport factories, and
14
 * will produce transport instances as needed for provided site aliases.
15
 */
16
class TransportManager
17
{
18
    protected $transportFactories = [];
19
20
    /**
21
     * createDefault creates a Transport manager and add the default transports to it.
22
     */
23
    public static function createDefault()
24
    {
25
        $transportManager = new self();
26
27
        $transportManager->add(new SshTransportFactory());
28
        $transportManager->add(new DockerComposeTransportFactory());
29
30
        return $transportManager;
31
    }
32
33
    /**
34
     * add a transport factory to our factory list
35
     * @param TransportFactoryInterface $factory
36
     */
37
    public function add(TransportFactoryInterface $factory)
38
    {
39
        $this->transportFactories[] = $factory;
40
    }
41
42
    /**
43
     * hasTransport determines if there is a transport that handles the
44
     * provided site alias.
45
     */
46
    public function hasTransport(AliasRecord $siteAlias)
47
    {
48
        return $this->getTransportFactory($siteAlias) !== false;
49
    }
50
51
    /**
52
     * getTransport returns a transport that is applicable to the provided site alias.
53
     */
54
    public function getTransport(AliasRecord $siteAlias)
55
    {
56
        $factory = $this->getTransportFactory($siteAlias);
57
        if ($factory) {
58
            return $factory->create($siteAlias);
59
        }
60
        return new LocalTransport();
61
    }
62
63
    /**
64
     * getTransportFactory returns a factory for the provided site alias.
65
     */
66
    protected function getTransportFactory(AliasRecord $siteAlias)
67
    {
68
        foreach ($this->transportFactories as $factory) {
69
            if ($factory->check($siteAlias)) {
70
                return $factory;
71
            }
72
        }
73
        return false;
74
    }
75
}
76