FilesystemAwareTrait::setError()
last analyzed

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Storage
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Storage\Traits;
16
17
use Phossa2\Storage\Interfaces\FilesystemInterface;
18
use Phossa2\Storage\Interfaces\FilesystemAwareInterface;
19
use Phossa2\Storage\Message\Message;
20
21
/**
22
 * FilesystemAwareTrait
23
 *
24
 * Implementation of FilesystemAwareInterface
25
 *
26
 * @package Phossa2\Storage
27
 * @author  Hong Zhang <[email protected]>
28
 * @see     FilesystemAwareInterface
29
 * @version 2.0.0
30
 * @since   2.0.0 added
31
 */
32
trait FilesystemAwareTrait
33
{
34
    /**
35
     * @var    FilesystemInterface
36
     * @access protected
37
     */
38
    protected $filesystem;
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function setFilesystem(FilesystemInterface $filesystem)
44
    {
45
        $this->filesystem = $filesystem;
46
        return $this;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function getFilesystem()/*# : FilesystemInterface */
53
    {
54
        return $this->filesystem;
55
    }
56
57
    /**
58
     * Check filesystem readable or not
59
     *
60
     * @param  string $path path to check
61
     * @return bool
62
     * @access protected
63
     */
64
    protected function isFilesystemReadable(/*# string */ $path)/*# : bool */
65
    {
66
        if ($this->getFilesystem()->isReadable()) {
67
            return true;
68
        } else {
69
            return $this->setError(
70
                Message::get(Message::STR_FS_NONREADABLE, $path),
71
                Message::STR_FS_NONREADABLE
72
            );
73
        }
74
    }
75
76
    /**
77
     * Check filesystem writable or not
78
     *
79
     * @param  string $path path to check
80
     * @return bool
81
     * @access protected
82
     */
83
    protected function isFilesystemWritable(/*# string */ $path)/*# : bool */
84
    {
85
        if ($this->getFilesystem()->isWritable()) {
86
            return true;
87
        } else {
88
            return $this->setError(
89
                Message::get(Message::STR_FS_NONWRITABLE, $path),
90
                Message::STR_FS_NONWRITABLE
91
            );
92
        }
93
    }
94
95
    /**
96
     * Check filesystem file deletable or not
97
     *
98
     * @param  string $path path to check
99
     * @return bool
100
     * @access protected
101
     */
102
    protected function isFilesystemDeletable(/*# string */ $path)/*# : bool */
103
    {
104
        if ($this->getFilesystem()->isDeletable()) {
105
            return true;
106
        } else {
107
            return $this->setError(
108
                Message::get(Message::STR_FS_NONDELETABLE, $path),
109
                Message::STR_FS_NONDELETABLE
110
            );
111
        }
112
    }
113
114
    abstract public function setError(
115
        /*# string */ $message = '',
116
        /*# string */ $code = ''
117
    )/*# : bool */;
118
}
119