Passed
Branch master (29ca76)
by Adrian Florin
03:58 queued 01:20
created

File::getContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3
1
<?php
2
/**
3
 * This file is part of the NeedleProject\FileIo package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace NeedleProject\FileIo;
9
10
use NeedleProject\FileIo\Content\Content;
11
use NeedleProject\FileIo\Content\ContentInterface;
12
use NeedleProject\FileIo\Exception\FileNotFoundException;
13
use NeedleProject\FileIo\Exception\PermissionDeniedException;
14
15
/**
16
 * Class File
17
 *
18
 * @package NeedleProject\FileIo
19
 * @author Adrian Tilita <[email protected]>
20
 * @copyright 2016-2017 Adrian Tilita
21
 * @license https://opensource.org/licenses/MIT MIT Licence
22
 */
23
class File
24
{
25
    /**
26
     * @var null|string
27
     */
28
    private $filename = null;
29
30
    /**
31
     * File constructor.
32
     *
33
     * @param string $filename
34
     */
35 14
    public function __construct(string $filename)
36
    {
37 14
        $this->filename = $filename;
38 14
    }
39
40
    /**
41
     * States whether the file actually exists on disk
42
     * @return bool
43
     */
44 7
    public function exists(): bool
45
    {
46 7
        return file_exists($this->filename);
47
    }
48
49
    /**
50
     * States whether the file is readable
51
     * @return bool
52
     */
53 4
    public function isReadable(): bool
54
    {
55 4
        return is_readable($this->filename);
56
    }
57
58
    /**
59
     * @return bool
60
     */
61 5
    public function isWritable(): bool
62
    {
63 5
        return is_writable($this->filename);
64
    }
65
66
    /**
67
     * Write content to the current file
68
     *
69
     * @param \NeedleProject\FileIo\Content\ContentInterface $content
70
     * @return \NeedleProject\FileIo\File
71
     * @throws \NeedleProject\FileIo\Exception\PermissionDeniedException
72
     */
73 3
    public function write(ContentInterface $content): File
74
    {
75 3
        if ($this->isWritable() === false) {
76 1
            throw new PermissionDeniedException("The current file is not writable!");
77
        }
78 2
        file_put_contents($this->filename, $content->get());
79 2
        return $this;
80
    }
81
82
    /**
83
     * @return \NeedleProject\FileIo\Content\ContentInterface
84
     * @throws \NeedleProject\FileIo\Exception\FileNotFoundException
85
     * @throws \NeedleProject\FileIo\Exception\PermissionDeniedException
86
     */
87 3
    public function getContent(): ContentInterface
88
    {
89 3
        if ($this->exists() === false) {
90 1
            throw new FileNotFoundException(sprintf("%s does not exists!", $this->filename));
91
        }
92 2
        if ($this->isReadable() === false) {
93 1
            throw new PermissionDeniedException(
94 1
                sprintf("You do not have permissions to read file %s!", $this->filename)
95
            );
96
        }
97 1
        return new Content(file_get_contents($this->filename));
98
    }
99
100
    /**
101
     * Deletes the current file
102
     */
103 2
    public function delete(): bool
104
    {
105 2
        if ($this->exists() === false) {
106 1
            return false;
107
        }
108 1
        return unlink($this->filename);
109
    }
110
}
111