Completed
Push — master ( 72f544...07d9db )
by Sebastian
06:33
created

File   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 61
wmc 10
lcom 1
cbo 0
ccs 24
cts 26
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setOut() 0 11 3
A setupOut() 0 8 3
A write() 0 4 1
A close() 0 6 3
1
<?php
2
namespace phpbu\App\Log;
3
4
use InvalidArgumentException;
5
6
/**
7
 * Class that can write to a file or a socket.
8
 *
9
 *
10
 * @package    phpbu
11
 * @subpackage Log
12
 * @author     Sebastian Feldmann <[email protected]>
13
 * @copyright  Sebastian Feldmann <[email protected]>
14
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
15
 * @link       http://phpbu.de/
16
 */
17
class File
18
{
19
    /**
20
     * @var resource
21
     */
22
    protected $out;
23
24
    /**
25
     * @var string
26
     */
27
    protected $outTarget;
28
29
    /**
30
     * Set output target.
31
     *
32
     * @param mixed $out
33
     */
34 5
    public function setOut($out)
35
    {
36 5
        if (empty($out)) {
37 1
            throw new InvalidArgumentException('Out can\'t be empty');
38
        }
39 4
        if (is_string($out)) {
40 3
            $this->setupOut($out);
41 1
        } else {
42
            $this->out = $out;
43 1
        }
44 1
    }
45
46
    /**
47
     * Setup the out resource
48 2
     *
49 1
     * @param string $out
50 1
     */
51 2
    protected function setupOut($out)
52
    {
53 2
        if (strpos($out, 'php://') === false && !is_dir(dirname($out))) {
54 2
            mkdir(dirname($out), 0777, true);
55 1
        }
56
        $this->out       = fopen($out, 'wt');
57 3
        $this->outTarget = $out;
58
    }
59
60
    /**
61
     * @param string $buffer
62 3
     */
63
    public function write($buffer)
64 3
    {
65 3
        fwrite($this->out, $buffer);
66
    }
67
68
    /**
69
     * Close output if it's not to a php stream.
70 2
     */
71
    public function close()
72 2
    {
73 1
        if ($this->out && strncmp($this->outTarget, 'php://', 6) !== 0) {
74 1
            fclose($this->out);
75 2
        }
76
    }
77
}
78