FileHandlerTrait   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 26
c 4
b 1
f 0
dl 0
loc 109
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prependBom() 0 7 3
A setEncoding() 0 5 1
A releaseHandle() 0 9 2
A getEncoding() 0 3 1
A setUseBom() 0 5 1
A initHandle() 0 14 5
A __destruct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of YaEtl
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Traits;
11
12
use fab2s\Bom\Bom;
13
use fab2s\YaEtl\YaEtlException;
14
15
/**
16
 * Trait FileHandlerTrait
17
 */
18
trait FileHandlerTrait
19
{
20
    /**
21
     * @var resource
22
     */
23
    protected $handle;
24
25
    /**
26
     * @var string
27
     */
28
    protected $encoding;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $useBom;
34
35
    /**
36
     * make sure we do not hold un-necessary handles
37
     */
38
    public function __destruct()
39
    {
40
        $this->releaseHandle();
41
    }
42
43
    /**
44
     * @return string|null
45
     */
46
    public function getEncoding(): ?string
47
    {
48
        return $this->encoding;
49
    }
50
51
    /**
52
     * @param string $encoding
53
     *
54
     * @return static
55
     */
56
    public function setEncoding(string $encoding): self
57
    {
58
        $this->encoding = $encoding;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param bool $useBom
65
     *
66
     * @return static
67
     */
68
    public function setUseBom(bool $useBom): self
69
    {
70
        $this->useBom = $useBom;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $string
77
     *
78
     * @return string
79
     */
80
    public function prependBom(string $string): string
81
    {
82
        if ($this->encoding && ($bom = Bom::getEncodingBom($this->encoding))) {
83
            return $bom . $string;
84
        }
85
86
        return $string;
87
    }
88
89
    /**
90
     * release handle
91
     *
92
     * @return static
93
     */
94
    public function releaseHandle(): self
95
    {
96
        if (is_resource($this->handle)) {
97
            fclose($this->handle);
98
        }
99
100
        $this->handle = null;
101
102
        return $this;
103
    }
104
105
    /**
106
     * @param resource|string $input
107
     * @param string          $mode
108
     *
109
     * @throws YaEtlException
110
     *
111
     * @return static
112
     */
113
    protected function initHandle($input, string $mode): self
114
    {
115
        if (is_resource($input)) {
116
            $this->handle = $input;
117
        } elseif (is_file($input)) {
118
            $this->handle = fopen($input, $mode) ?: null;
119
            if (!$this->handle) {
120
                throw new YaEtlException('Handle could not be opened in mode:' . $mode);
121
            }
122
        } else {
123
            throw new YaEtlException('$input is either not a resource or not a file');
124
        }
125
126
        return $this;
127
    }
128
}
129