Completed
Push — master ( 83bd2d...9517e2 )
by Arne
03:10
created

ArchivR::getConnection()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace Archivr;
4
5
use Archivr\ConnectionAdapter\ConnectionAdapterFactoryContainer;
6
use Archivr\ConnectionAdapter\ConnectionAdapterInterface;
7
use Archivr\ConnectionAdapter\FlysystemConnectionAdapter;
8
use Archivr\Exception\ConfigurationException;
9
use Archivr\LockAdapter\ConnectionBasedLockAdapter;
10
use Archivr\LockAdapter\LockAdapterFactoryContainer;
11
use Archivr\LockAdapter\LockAdapterInterface;
12
use League\Flysystem\Adapter\Local;
13
use League\Flysystem\Filesystem;
14
15
class ArchivR
16
{
17
    use TildeExpansionTrait;
18
19
    /**
20
     * @var Configuration
21
     */
22
    protected $configuration;
23
24
    /**
25
     * @var ConnectionAdapterFactoryContainer
26
     */
27
    protected $connectionAdapterFactoryContainer;
28
29
    /**
30
     * @var LockAdapterFactoryContainer
31
     */
32
    protected $lockAdapterFactoryContainer;
33
34
    /**
35
     * @var Vault[]
36
     */
37
    protected $vaults = [];
38
39
    public function __construct(Configuration $configuration)
40
    {
41
        $this->configuration = $configuration;
42
        $this->connectionAdapterFactoryContainer = new ConnectionAdapterFactoryContainer([
43
44
            'path' => function(ConnectionConfiguration $connectionConfiguration)
45
            {
46
                $path = $connectionConfiguration->getSetting('path');
47
                $path = $this->expandTildePath($path);
48
49
                if (!is_dir($path) || !is_writable($path))
50
                {
51
                    throw new ConfigurationException(sprintf('Path "%s" does not exist or is not writable.', $path));
52
                }
53
54
                $adapter = new Local($path);
55
                $filesystem = new Filesystem($adapter);
56
57
                return new FlysystemConnectionAdapter($filesystem);
58
            },
59
        ]);
60
        $this->lockAdapterFactoryContainer = new LockAdapterFactoryContainer([
61
62
            'connection' => function(ConnectionConfiguration $connectionConfiguration)
63
            {
64
                $connection = $this->getConnection($connectionConfiguration->getTitle());
65
66
                return new ConnectionBasedLockAdapter($connection);
67
            }
68
        ]);
69
    }
70
71
    public function getConfiguration(): Configuration
72
    {
73
        return $this->configuration;
74
    }
75
76
    public function getConnectionAdapterFactoryContainer(): AbstractServiceFactoryContainer
77
    {
78
        return $this->connectionAdapterFactoryContainer;
79
    }
80
81
    public function getLockAdapterFactoryContainer(): AbstractServiceFactoryContainer
82
    {
83
        return $this->lockAdapterFactoryContainer;
84
    }
85
86 View Code Duplication
    public function getConnection(string $vaultTitle): ConnectionAdapterInterface
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $connectionConfiguration = $this->configuration->getConnectionConfigurationByTitle($vaultTitle);
89
90
        if ($connectionConfiguration === null)
91
        {
92
            throw new \InvalidArgumentException(sprintf('Unknown connection title: "%s".', $vaultTitle));
93
        }
94
95
        $connection = $this->connectionAdapterFactoryContainer->get($connectionConfiguration->getVaultAdapter(), $connectionConfiguration);
96
97
        if ($connection === null)
98
        {
99
            throw new ConfigurationException(sprintf('Unknown connection adapter: "%s".', $connectionConfiguration->getVaultAdapter()));
100
        }
101
102
        return $connection;
103
    }
104
105 View Code Duplication
    public function getLockAdapter(string $vaultTitle): LockAdapterInterface
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $connectionConfiguration = $this->configuration->getConnectionConfigurationByTitle($vaultTitle);
108
109
        if ($connectionConfiguration === null)
110
        {
111
            throw new \InvalidArgumentException(sprintf('Unknown connection title: "%s".', $vaultTitle));
112
        }
113
114
        $lockAdapter = $this->lockAdapterFactoryContainer->get($connectionConfiguration->getLockAdapter(), $connectionConfiguration);
115
116
        if ($lockAdapter === null)
117
        {
118
            throw new ConfigurationException(sprintf('Unknown lock adapter: "%s".', $connectionConfiguration->getLockAdapter()));
119
        }
120
121
        return $lockAdapter;
122
    }
123
124
    public function buildOperationCollection(): OperationCollection
125
    {
126
        $return = new OperationCollection();
127
128
        foreach ($this->getVaults() as $vault)
129
        {
130
            $return->append($vault->getOperationCollection());
131
        }
132
133
        return $return;
134
    }
135
136
    /**
137
     * @return Vault[]
138
     */
139
    public function getVaults(): array
140
    {
141
        return array_map(function(ConnectionConfiguration $connectionConfiguration) {
142
143
            return $this->getVault($connectionConfiguration->getTitle());
144
145
        }, $this->configuration->getConnectionConfigurations());
146
    }
147
148
    public function getVault(string $vaultTitle): Vault
149
    {
150
        if (!isset($this->vaults[$vaultTitle]))
151
        {
152
            $vault = new Vault(
153
                $this->configuration->getLocalPath(),
154
                $this->getConnection($vaultTitle)
155
            );
156
            $vault->setLockAdapter($this->getLockAdapter($vaultTitle));
157
158
            $this->vaults[$vaultTitle] = $vault;
159
        }
160
161
        return $this->vaults[$vaultTitle];
162
    }
163
164
    public function synchronize(SynchronizationProgressListenerInterface $progressionListener = null): OperationResultCollection
165
    {
166
        $return = new OperationResultCollection();
167
168
        foreach ($this->getVaults() as $vault)
169
        {
170
            $return->append($vault->synchronize($progressionListener));
171
        }
172
173
        return $return;
174
    }
175
}
176