Completed
Push — master ( dbcf6b...d53ab6 )
by Peter
44:13 queued 37:37
created

TransportFactory::createTransportFromConfig()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 8.394

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 7
cts 20
cp 0.35
rs 9.44
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 8.394
1
<?php
2
3
namespace TreeHouse\IoBundle\Import\Feed;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Transport\FileTransport;
7
use TreeHouse\Feeder\Transport\FtpTransport;
8
use TreeHouse\Feeder\Transport\HttpTransport;
9
use TreeHouse\Feeder\Transport\TransportInterface;
10
11
class TransportFactory
12
{
13
    const TYPE_FILE = 'file';
14
    const TYPE_HTTP = 'http';
15
    const TYPE_FTP = 'ftp';
16
17
    /**
18
     * @param array $config
19
     *
20
     * @throws \LogicException
21
     *
22
     * @return TransportInterface
23
     */
24 4
    public static function createTransportFromConfig(array $config)
25
    {
26 4
        $config = new ParameterBag($config);
27
28 4
        switch ($config->get('type')) {
29 4
            case self::TYPE_HTTP:
30
                $transport = static::createTransportFromUrl(
31
                    $config->get('url'),
32
                    $config->get('user'),
33
                    $config->get('pass')
34
                );
35
36
                return $transport;
37 4
            case self::TYPE_FTP:
38
                return FtpTransport::create(
39
                    $config->get('host'),
40
                    $config->get('user'),
41
                    $config->get('pass'),
42
                    $config->get('file'),
43
                    [
44
                        'mode' => $config->get('mode', 'binary'),
45
                        'pasv' => $config->get('pasv', true),
46
                    ]
47
                );
48 4
            case self::TYPE_FILE:
49 4
                return static::createTransportFromFile($config->get('file'));
50
            default:
51
                throw new \LogicException(sprintf('Unsupported transport type "%s"', $config->get('type')));
52
        }
53
    }
54
55
    /**
56
     * @param string $file
57
     *
58
     * @throws \RuntimeException When the file does not exist
59
     *
60
     * @return FileTransport
61
     */
62 4
    public static function createTransportFromFile($file)
63
    {
64 4
        if (false === file_exists($file)) {
65
            throw new \RuntimeException(sprintf('File "%s" does not exist', $file));
66
        }
67
68 4
        return FileTransport::create($file);
69
    }
70
71
    /**
72
     * @param string $url
73
     * @param string $user
74
     * @param string $pass
75
     *
76
     * @return HttpTransport
77
     */
78
    public static function createTransportFromUrl($url, $user = null, $pass = null)
79
    {
80
        return HttpTransport::create($url, $user, $pass);
81
    }
82
83
    /**
84
     * @param TransportInterface $transport
85
     *
86
     * @throws \LogicException
87
     *
88
     * @return array
89
     */
90 4
    public static function createConfigFromTransport(TransportInterface $transport)
91
    {
92 4
        if ($transport instanceof FileTransport) {
93 4
            return static::createConfigFromFile($transport->getConnection()['file']);
94
        }
95
96
        if ($transport instanceof HttpTransport) {
97
            return static::createConfigFromUrl($transport->getUrl(), $transport->getUser(), $transport->getPass());
98
        }
99
100
        if ($transport instanceof FtpTransport) {
101
            return [
102
                'type' => self::TYPE_FTP,
103
                'host' => $transport->getHost(),
104
                'user' => $transport->getUser(),
105
                'pass' => $transport->getPass(),
106
                'file' => $transport->getConnection()['file'],
107
                'mode' => $transport->getMode(),
108
                'pasv' => $transport->getPasv(),
109
            ];
110
        }
111
112
        throw new \LogicException(sprintf('Unable to create config for transport of type "%s"', get_class($transport)));
113
    }
114
115
    /**
116
     * @param string $file
117
     *
118
     * @return array
119
     */
120 4
    public static function createConfigFromFile($file)
121
    {
122 4
        return ['type' => self::TYPE_FILE, 'file' => $file];
123
    }
124
125
    /**
126
     * @param string $url
127
     * @param string $user
128
     * @param string $pass
129
     *
130
     * @return array
131
     */
132
    public static function createConfigFromUrl($url, $user = null, $pass = null)
133
    {
134
        $config = ['type' => self::TYPE_HTTP, 'url' => $url];
135
136
        if (null !== $user) {
137
            $config['user'] = $user;
138
        }
139
140
        if (null !== $pass) {
141
            $config['pass'] = $pass;
142
        }
143
144
        return $config;
145
    }
146
}
147