WriterTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 88
rs 10
c 0
b 0
f 0
ccs 31
cts 31
cp 1
wmc 14
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A write() 0 9 3
A setOptions() 0 14 2
D validateWriterFile() 0 34 9
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Infrastructure\Io\File;
38
39
/**
40
 * File writer trait
41
 *
42
 * @package     Apparat\Resource
43
 * @subpackage  Apparat\Resource\Infrastructure
44
 * @property string $file File path
45
 */
46
trait WriterTrait
47
{
48
    /**
49
     * File options
50
     *
51
     * @var int
52
     */
53
    protected $options;
54
55
    /**
56
     * Write data
57
     *
58
     * @param string $data Data to write
59
     * @return int Bytes written
60
     */
61 7
    public function write($data)
62
    {
63
        // If the parent directory does not exist but may be created
64 7
        if (!@is_dir(dirname($this->file)) && ($this->options & Writer::FILE_CREATE_DIRS)) {
65 1
            mkdir(dirname($this->file), 0777, true);
66
        }
67
68 7
        return file_put_contents($this->file, $data);
69
    }
70
71
    /**
72
     * Set the file options
73
     *
74
     * @param int $options File options
75
     * @throws InvalidWriterArgumentException If the writer options are invalid
76
     */
77 16
    protected function setOptions($options)
78
    {
79 16
        $options = intval($options);
80 16
        $allOptions = Writer::FILE_CREATE | Writer::FILE_CREATE_DIRS | Writer::FILE_OVERWRITE;
81
82 16
        if (($options & $allOptions) != $options) {
83 1
            throw new InvalidWriterArgumentException(
84 1
                sprintf('Invalid file writer option "%s"', $options & ~$allOptions),
85 1
                InvalidWriterArgumentException::INVALID_WRITER_OPTIONS
86
            );
87
        }
88
89 15
        $this->options = $options;
90 15
    }
91
92
    /**
93
     * Validate the writer file
94
     *
95
     * @throws InvalidWriterArgumentException If the file cannot be created
96
     * @throws InvalidWriterArgumentException If the file cannot be replaced
97
     * @throws InvalidWriterArgumentException If the directory cannot be created
98
     */
99 15
    protected function validateWriterFile()
100
    {
101
        // If the parent directory does not exist and cannot be created
102 15
        if (!@is_dir(dirname($this->file)) && !($this->options & Writer::FILE_CREATE_DIRS)) {
103 1
            throw new InvalidWriterArgumentException(
104 1
                sprintf('Parent directory "%s" cannot be created', dirname($this->file)),
105 1
                InvalidWriterArgumentException::RESOURCE_CONTAINER_CANNOT_BE_CREATED
106
            );
107
        }
108
109
        // If the file does not exist and cannot be created
110 14
        if (!@file_exists($this->file) && !($this->options & Writer::FILE_CREATE)) {
111 1
            throw new InvalidWriterArgumentException(
112 1
                sprintf('File "%s" cannot be created', $this->file),
113 1
                InvalidWriterArgumentException::RESOURCE_CANNOT_BE_CREATED
114
            );
115
        }
116
117
        // If the file exists but cannot be overwritten
118 13
        if (@file_exists($this->file) &&
119
            (
120 4
                !@is_file($this->file) ||
121 4
                !@is_writeable(
122 4
                    $this->file
123
                ) ||
124 13
                !($this->options & Writer::FILE_OVERWRITE)
125
            )
126
        ) {
127 2
            throw new InvalidWriterArgumentException(
128 2
                sprintf('File "%s" cannot be overwritten', $this->file),
129 2
                InvalidWriterArgumentException::RESOURCE_CANNOT_BE_REPLACED
130
            );
131
        }
132 11
    }
133
}
134