Completed
Push — master ( bc7e8d...c556fe )
by Julien
14s
created

DocumentFactory::makeFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace TheCodingMachine\Gotenberg;
4
5
use GuzzleHttp\Psr7\LazyOpenStream;
6
use Psr\Http\Message\StreamInterface;
7
use Safe\Exceptions\FilesystemException;
8
use function GuzzleHttp\Psr7\stream_for;
9
use function Safe\fopen;
10
use function Safe\fwrite;
11
12
final class DocumentFactory
13
{
14
    /**
15
     * @param string $fileName
16
     * @param string $filePath
17
     * @return Document
18
     */
19
    public static function makeFromPath(string $fileName, string $filePath): Document
20
    {
21
        return new Document($fileName, new LazyOpenStream($filePath, 'r'));
22
    }
23
24
    /**
25
     * @param string $fileName
26
     * @param StreamInterface $fileStream
27
     * @return Document
28
     */
29
    public static function makeFromStream(string $fileName, StreamInterface $fileStream): Document
30
    {
31
        return new Document($fileName, $fileStream);
32
    }
33
34
    /**
35
     * @param string $fileName
36
     * @param string $string
37
     * @return Document
38
     * @throws FilesystemException
39
     */
40
    public static function makeFromString(string $fileName, string $string): Document
41
    {
42
        $fileStream = fopen('php://memory', 'rb+');
43
        fwrite($fileStream, $string);
44
45
        return new Document($fileName, stream_for($fileStream));
46
    }
47
}
48