Passed
Push — master ( 79092c...29ca76 )
by Adrian Florin
02:43
created

File::getContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
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
    public function __construct(string $filename)
36
    {
37
        $this->filename = $filename;
38
    }
39
40
    /**
41
     * States whether the file actually exists on disk
42
     * @return bool
43
     */
44
    public function exists(): bool
45
    {
46
        return file_exists($this->filename);
47
    }
48
49
    /**
50
     * States whether the file is readable
51
     * @return bool
52
     */
53
    public function isReadable(): bool
54
    {
55
        return is_readable($this->filename);
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function isWritable(): bool
62
    {
63
        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
    public function write(ContentInterface $content): File
74
    {
75
        if ($this->isWritable() === false) {
76
            throw new PermissionDeniedException("The current file is not writable!");
77
        }
78
        file_put_contents($this->filename, $content->get());
79
        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
    public function getContent(): ContentInterface
88
    {
89
        if ($this->exists() === false) {
90
            throw new FileNotFoundException(sprintf("%s does not exists!", $this->filename));
91
        }
92
        if ($this->isReadable() === false) {
93
            throw new PermissionDeniedException(
94
                sprintf("You do not have permissions to read file %s!", $this->filename)
95
            );
96
        }
97
        return new Content(file_get_contents($this->filename));
98
    }
99
100
    /**
101
     * Deletes the current file
102
     */
103
    public function delete(): bool
104
    {
105
        if ($this->exists() === false) {
106
            return false;
107
        }
108
        return unlink($this->filename);
109
    }
110
}
111