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

TransportManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 60
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createDefault() 0 9 1
A add() 0 4 1
A hasTransport() 0 4 1
A getTransport() 0 8 2
A getTransportFactory() 0 9 3
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