Passed
Push — master ( 9e5fe0...88d9d6 )
by Tom
04:35
created

DestructibleString   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 60
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A rmDir() 0 5 1
A __toString() 0 3 1
A rm() 0 5 1
A __destruct() 0 3 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Value\SideEffect;
6
7
/**
8
 * Class DestructibleString
9
 *
10
 * Representative String w/ lifetime. E.g. representing a path to a temporary
11
 * directory which is getting removed on destruction.
12
 *
13
 * @package Ktomk\Pipelines
14
 */
15
class DestructibleString
16
{
17
    /**
18
     * @var string
19
     */
20
    private $string;
21
22
    /**
23
     * @var callable
24
     */
25
    private $callback;
26
27
    /**
28
     * @see LibFs::rmDir
29
     *
30
     * @param string $string
31
     *
32
     * @return DestructibleString
33
     */
34 1
    public static function rmDir($string)
35
    {
36 1
        return new self(
37 1
            $string,
38 1
            'Ktomk\Pipelines\LibFs::rmDir'
39
        );
40
    }
41
42
    /**
43
     * @param string $string
44
     *
45
     * @return self
46
     */
47 1
    public static function rm($string)
48
    {
49 1
        return new self(
50 1
            $string,
51 1
            'Ktomk\Pipelines\LibFs::rm'
52
        );
53
    }
54
55
    /**
56
     * DestructibleString constructor.
57
     *
58
     * @param string $string
59
     * @param callable $callback
60
     */
61 3
    public function __construct($string, $callback)
62
    {
63 3
        $this->string = $string;
64 3
        $this->callback = $callback;
65 3
    }
66
67 3
    public function __destruct()
68
    {
69 3
        call_user_func($this->callback, $this->string);
70 3
    }
71
72 4
    public function __toString()
73
    {
74 4
        return $this->string;
75
    }
76
}
77