Completed
Branch releases/v0.2 (d913c4)
by Luke
02:17
created

CSVelte::assertFileIsReadable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * CSVelte: Slender, elegant CSV for PHP
4
 *
5
 * Inspired by Python's CSV module and Frictionless Data and the W3C's CSV
6
 * standardization efforts, CSVelte was written in an effort to take all the
7
 * suck out of working with CSV.
8
 *
9
 * @version   v0.2
10
 * @copyright Copyright (c) 2016 Luke Visinoni <[email protected]>
11
 * @author    Luke Visinoni <[email protected]>
12
 * @license   https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT)
13
 */
14
namespace CSVelte;
15
16
use CSVelte\Reader;
17
use CSVelte\Flavor;
18
use CSVelte\IO\File;
19
use CSVelte\IO\Stream;
20
21
use CSVelte\Exception\IOException;
22
23
/**
24
 * CSVelte Facade
25
 *
26
 * This class consists of static factory methods for easily generating commonly
27
 * used objects such as readers and writers, as well as convenience methods for
28
 * commonly used functionality such as exporting CSV data to a file.
29
 *
30
 * @package CSVelte
31
 * @subpackage Factory/Adapter
32
 * @since v0.1
33
 */
34
class CSVelte
35
{
36
    /**
37
     * CSVelte\Reader Factory
38
     *
39
     * Factory method for creating a new CSVelte\Reader object
40
     * Used to create a local file CSV reader object.
41
     *
42
     * @param string The filename to read
43
     * @param CSVelte\Flavor An explicit flavor object that will be passed to the reader
44
     * @return CSVelte\Reader An iterator for specified CSV file
45
     * @throws CSVelte\Exception\PermissionDeniedException
46
     * @throws CSVelte\Exception\FileNotFoundException
47
     * @access public
48
     */
49 5
    public static function reader($filename, $flavor = null)
50
    {
51 5
        self::assertFileIsReadable($filename);
52 3
        $file = new File($filename);
53 3
        return new Reader($file, $flavor);
54
    }
55
56
    /**
57
     * String Reader Factory
58
     *
59
     * Factory method for creating a new CSVelte\Reader object for reading
60
     * from a PHP string
61
     *
62
     * @param string The CSV data to read
63
     * @param CSVelte\Flavor An explicit flavor object that will be passed to the reader
64
     * @return CSVelte\Reader An iterator for provided CSV data
65
     * @access public
66
     */
67 1
    public static function stringReader($str, $flavor = null)
68
    {
69 1
        return new Reader($str, $flavor);
70
    }
71
72
    /**
73
     * CSVelte\Writer Factory
74
     *
75
     * Factory method for creating a new CSVelte\Writer object for writing
76
     * CSV data to a file. If file doesn't exist, it will be created. If it
77
     * already contains data, it will be overwritten.
78
     *
79
     * @param string The filename to write to.
80
     * @param CSVelte\Flavor An explicit flavor object for the writer to use
81
     * @return CSVelte\Writer A writer object for writing to given filename
82
     * @access public
83
     */
84 2
    public static function writer($filename, $flavor = null)
85
    {
86 2
        $file = new Stream($filename, 'w+');
87 2
        return new Writer($file, $flavor);
88
    }
89
90
    /**
91
     * Export CSV data to local file
92
     *
93
     * Facade method for exporting data to given filename. IF file doesn't exist
94
     * it will be created. If it does exist it will be overwritten.
95
     *
96
     * @param string The filename to export data to
97
     * @param Iterator|array Data to write to CSV file
98
     * @param CSVelte\Flavor An explicit flavor object that will be passed to the writer
99
     * @return int Number of rows written
100
     * @access public
101
     */
102 2
    public static function export($filename, $data, $flavor = null)
103
    {
104 2
        $file = new Stream($filename, 'w+');
105 2
        $writer = new Writer($file, $flavor);
106 2
        return $writer->writeRows($data);
107
    }
108
109
    /**
110
     * Assert that file is readable
111
     *
112
     * Assert that a particular file exists and is readable (user has permission
113
     * to read/access it)
114
     *
115
     * @param string The name of the file you wish to check
116
     * @return void
117
     * @access protected
118
     * @throws CSVelte\Exception\IOException
119
     * @internal
120
     */
121 5
    protected static function assertFileIsReadable($filename)
122
    {
123 5
        self::assertFileExists($filename);
124 4
        if (!is_readable($filename)) {
125 1
            throw new IOException('Permission denied for: ' . $filename, IOException::ERR_FILE_PERMISSION_DENIED);
126
        }
127 3
    }
128
129
    /**
130
     * Assert that a particular file exists
131
     *
132
     * @param string The name of the file you wish to check
133
     * @return void
134
     * @access protected
135
     * @throws CSVelte\Exception\IOException
136
     * @internal
137
     */
138 5
    protected static function assertFileExists($filename)
139
    {
140 5
        if (!file_exists($filename)) {
141 1
            throw new IOException('File does not exist: ' . $filename, IOException::ERR_FILE_NOT_FOUND);
142
        }
143 4
    }
144
}
145