Completed
Push — 16.x ( 0056bb...bd3602 )
by Tim
02:11 queued 10s
created

GenericFileHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 55
c 0
b 0
f 0
wmc 6
lcom 0
cbo 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B removeLineFromFile() 0 42 6
1
<?php
2
3
/**
4
 * TechDivision\Import\Handlers\PidFileHandler
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2020 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Handlers;
22
23
use TechDivision\Import\Exceptions\LineNotFoundException;
24
25
/**
26
 * A generic file handler implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2020 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class GenericFileHandler implements GenericFileHandlerInterface
35
{
36
37
    /**
38
     * Remove's the passed line from the file with the passed name.
39
     *
40
     * @param string   $line The line to be removed
41
     * @param resource $fh   The file handle of the file the line has to be removed
42
     *
43
     * @return void
44
     * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed
45
     */
46
    public function removeLineFromFile(string $line, $fh) : void
47
    {
48
49
        // initialize the array for the PIDs found in the PID file
50
        $lines = array();
51
52
        // initialize the flag if the line has been found
53
        $found = false;
54
55
        // rewind the file pointer
56
        rewind($fh);
57
58
        // read the lines with the PIDs from the PID file
59
        while (($buffer = fgets($fh, 4096)) !== false) {
60
            // remove the new line
61
            $buffer = trim($buffer);
62
            // if the line is the one to be removed, ignore the line
63
            if ($line === $buffer) {
64
                $found = true;
65
                continue;
66
            }
67
68
            // add the found PID to the array
69
            $lines[] = $buffer;
70
        }
71
72
        // query whether or not, we found the line
73
        if (!$found) {
74
            throw new LineNotFoundException(sprintf('Line %s can not be found', $line));
75
        }
76
77
        // empty the file and rewind the file pointer
78
        ftruncate($fh, 0);
79
        rewind($fh);
80
81
        // append the existing lines to the file
82
        foreach ($lines as $ln) {
83
            if (fwrite($fh, $ln . PHP_EOL) === false) {
84
                throw new \Exception(sprintf('Can\'t write %s to file', $ln));
85
            }
86
        }
87
    }
88
}
89