Passed
Branch master (8a55cf)
by Igor
12:47 queued 09:06
created

WriteToFile::__construct()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 8.1426

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 10
cts 14
cp 0.7143
rs 8.5226
c 0
b 0
f 0
cc 7
nc 9
nop 3
crap 8.1426
1
<?php
2
3
namespace ClickHouseDB\Query;
4
5
use ClickHouseDB\Exception\QueryException;
6
7
class WriteToFile
8
{
9
    /**
10
     *
11
     */
12
    const FORMAT_TabSeparated          = 'TabSeparated';
13
    const FORMAT_TabSeparatedWithNames = 'TabSeparatedWithNames';
14
    const FORMAT_CSV                   = 'CSV';
15
16
    private $support_format = ['TabSeparated', 'TabSeparatedWithNames', 'CSV'];
17
    /**
18
     * @var string
19
     */
20
    private $file_name = null;
21
22
    /**
23
     * @var string
24
     */
25
    private $format = 'CSV';
26
27
    /**
28
     * @var bool
29
     */
30
    private $gzip = false;
31
    /**
32
     * WriteToFile constructor.
33
     * @param string $file_name
34
     * @param bool $overwrite
35
     * @param null $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
36
     */
37 1
    public function __construct($file_name, $overwrite = true, $format = null) {
38
39
40 1
        if (!$file_name)
41
        {
42
            throw new QueryException('Bad file path');
43
        }
44
45 1
        if (is_file($file_name))
46
        {
47 1
            if (!$overwrite)
48
            {
49
                throw new QueryException('File exists: ' . $file_name);
50
            }
51 1
            if (!unlink($file_name))
52
            {
53
                throw new QueryException('Can`t delete: ' . $file_name);
54
            }
55
        }
56 1
        $dir = dirname($file_name);
57 1
        if (!is_writable($dir))
58
        {
59
            throw new QueryException('Can`t writable dir: ' . $dir);
60
        }
61 1
        if ($format)
62
        {
63 1
            $this->setFormat($format);
64
        }
65 1
        $this->file_name = $file_name;
66 1
    }
67
68
    /**
69
     * @return bool
70
     */
71 1
    public function getGzip()
72
    {
73 1
        return $this->gzip;
74
    }
75
76
    /**
77
     * @param bool $flag
78
     */
79
    public function setGzip($flag)
80
    {
81
        $this->gzip = $flag;
82
    }
83
84
    /**
85
     * @param string $format
86
     */
87 1
    public function setFormat($format)
88
    {
89 1
        if (!in_array($format, $this->support_format))
90
        {
91
            throw new QueryException('Unsupport format: ' . $format);
92
        }
93 1
        $this->format = $format;
94 1
    }
95
    /**
96
     * @return int
97
     */
98 1
    public function size()
99
    {
100 1
        return filesize($this->file_name);
101
    }
102
103
    /**
104
     * @return string
105
     */
106 1
    public function fetchFile()
107
    {
108 1
        return $this->file_name;
109
    }
110
111
    /**
112
     * @return string
113
     */
114 1
    public function fetchFormat()
115
    {
116 1
        return $this->format;
117
    }
118
119
}
120