Passed
Push — master ( c69355...293396 )
by Marcin
36s
created

Config   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 97.67%

Importance

Changes 0
Metric Value
wmc 16
eloc 39
dl 0
loc 200
rs 10
c 0
b 0
f 0
ccs 42
cts 43
cp 0.9767

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogger() 0 3 1
A setPort() 0 5 1
A setDocType() 0 5 1
A setBinary() 0 5 1
A setFormat() 0 5 1
A getTimeout() 0 3 1
A getConnectionString() 0 3 1
A setTimeout() 0 5 1
A getDocType() 0 3 1
A setLogger() 0 5 1
A __construct() 0 15 4
A setHost() 0 5 1
A getFormat() 0 3 1
1
<?php
2
3
/**
4
 * Created by Marcin.
5
 * Date: 03.03.2019
6
 * Time: 14:22
7
 */
8
9
namespace Mrcnpdlk\Api\Unoconv;
10
11
use Mrcnpdlk\Api\Unoconv\Enum\DocType;
12
use Mrcnpdlk\Api\Unoconv\Enum\FormatType;
13
use Mrcnpdlk\Api\Unoconv\Exception\ConfigurationException;
14
use /* @noinspection PhpUndefinedClassInspection */
15
    Psr\Log\LoggerInterface;
16
use /* @noinspection PhpUndefinedClassInspection */
17
    Psr\Log\NullLogger;
18
19
/**
20
 * Class Config
21
 */
22
class Config
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $binary = '/usr/bin/unoconv';
28
    /**
29
     * @var string
30
     */
31
    protected $host = 'localhost';
32
    /**
33
     * Port to listen on (as listener) or to connect to (as client).
34
     *
35
     * @var int
36
     */
37
    protected $port = 2002;
38
    /**
39
     * Specify the LibreOffice document type of the backend format. Possible document types are: document, graphics, presentation,
40
     * spreadsheet.
41
     *
42
     * @var DocType
43
     */
44
    protected $docType;
45
    /**
46
     * Specify the output format for the document. You can get a list of possible output formats per document type by using the --show
47
     * option.
48
     *
49
     * @var FormatType
50
     */
51
    protected $format;
52
    /**
53
     * When unoconv starts its own listener, try to connect to it for an amount of seconds before giving up. Increasing this may help when
54
     * you receive random errors caused by the listener not being ready to accept conversion jobs.
55
     *
56
     * @var int
57
     */
58
    protected $timeout = 30;
59
    /**
60
     * @var string
61
     */
62
    protected $options = 'urp;StarOffice.ComponentContext';
63
    /** @noinspection PhpUndefinedClassInspection */
64
    /**
65
     * @var \Psr\Log\LoggerInterface
66
     */
67
    protected $logger;
68
69
    /**
70
     * Config constructor.
71
     * Default values;
72
     *
73
     * @param array $config
74
     *
75
     * @throws \Mrcnpdlk\Api\Unoconv\Exception\ConfigurationException
76
     */
77 9
    public function __construct(array $config = [])
78
    {
79 9
        $this->docType = DocType::DOCUMENT();
80 9
        $this->format  = FormatType::PDF();
81
        /* @noinspection PhpUndefinedClassInspection */
82 9
        $this->logger = new NullLogger();
83
84 9
        foreach ($config as $key => $value) {
85 5
            $funName = sprintf('set%s', ucfirst($key));
86 5
            if (method_exists($this, $funName)) {
87 4
                $this->$funName($value);
88 1
            } elseif (property_exists($this, $key)) {
89
                $this->{$key} = $value;
90
            } else {
91 5
                throw new ConfigurationException(sprintf('Property "%s" not defined in Config class "%s"', $key, __CLASS__));
92
            }
93
        }
94 8
    }
95
96
    /**
97
     * @return string
98
     */
99 5
    public function getConnectionString(): string
100
    {
101 5
        return sprintf('%s --connection="socket,host=%s,port=%s;%s"', $this->binary, $this->host, $this->port, $this->options);
102
    }
103
104
    /**
105
     * @return DocType
106
     */
107 6
    public function getDocType(): DocType
108
    {
109 6
        return $this->docType;
110
    }
111
112
    /**
113
     * @return FormatType
114
     */
115 6
    public function getFormat(): FormatType
116
    {
117 6
        return $this->format;
118
    }
119
120
    /** @noinspection PhpUndefinedClassInspection */
121
122
    /**
123
     * @return \Psr\Log\LoggerInterface
124
     */
125 5
    public function getLogger(): LoggerInterface
126
    {
127 5
        return $this->logger;
128
    }
129
130
    /**
131
     * @return int
132
     */
133 6
    public function getTimeout(): int
134
    {
135 6
        return $this->timeout;
136
    }
137
138
    /**
139
     * @param string $binary
140
     *
141
     * @return $this
142
     */
143 4
    public function setBinary(string $binary): self
144
    {
145 4
        $this->binary = $binary;
146
147 4
        return $this;
148
    }
149
150
    /**
151
     * @param DocType $docType
152
     *
153
     * @return $this
154
     */
155 1
    public function setDocType(DocType $docType): self
156
    {
157 1
        $this->docType = $docType;
158
159 1
        return $this;
160
    }
161
162
    /**
163
     * @param FormatType $format
164
     *
165
     * @return $this
166
     */
167 1
    public function setFormat(FormatType $format): self
168
    {
169 1
        $this->format = $format;
170
171 1
        return $this;
172
    }
173
174
    /**
175
     * @param string $host
176
     *
177
     * @return $this
178
     */
179 1
    public function setHost(string $host): self
180
    {
181 1
        $this->host = $host;
182
183 1
        return $this;
184
    }
185
186
    /** @noinspection PhpUndefinedClassInspection */
187
188
    /**
189
     * @param \Psr\Log\LoggerInterface $logger
190
     *
191
     * @return $this
192
     */
193 1
    public function setLogger(LoggerInterface $logger): self
194
    {
195 1
        $this->logger = $logger;
196
197 1
        return $this;
198
    }
199
200
    /**
201
     * @param int $port
202
     *
203
     * @return $this
204
     */
205 1
    public function setPort(int $port): self
206
    {
207 1
        $this->port = $port;
208
209 1
        return $this;
210
    }
211
212
    /**
213
     * @param int $timeout
214
     *
215
     * @return $this
216
     */
217 1
    public function setTimeout(int $timeout): self
218
    {
219 1
        $this->timeout = $timeout;
220
221 1
        return $this;
222
    }
223
}
224