Passed
Push — master ( b43a24...1ec6ac )
by Marcin
42s
created

Config::getLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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