Pasteboard::defaultHeartbeat()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dlindberg\Pasteboard;
4
5
class Pasteboard
6
{
7
    private static $clipboard = "foo";
8
    private static $process;
9
    private static $pipes;
10
11
    /**
12
     * @param $value
13
     * @return bool|string
14
     */
15 9
    public static function set($value)
16
    {
17 9
        return self::action('pbcopy', $value);
18
    }
19
20
    /**
21
     * @return bool|string
22
     */
23 9
    public static function get()
24
    {
25 9
        return self::action('pbpaste');
26
    }
27
28
    /**
29
     * @param       $array
30
     * @param array $options
31
     * @return bool
32
     */
33 5
    public static function setArray($array, $options = array())
34
    {
35 5
        $config = self::configureArray($options);
36 5
        self::storedClipboard('pbpaste', $config['reset']);
37 5
        foreach ($array as $value) {
38 5
            if (!self::setArrayValue($value, $config)) {
39 5
                return false;
40
            }
41
        }
42 5
        self::storedClipboard('pbcopy', $config['reset']);
43
44 5
        return true;
45
    }
46
47
    /**
48
     * @param string $action
49
     * @param string $value
50
     * @return bool|string
51
     */
52 9
    private static function action($action, $value = null)
53
    {
54 9
        $output = false;
55 9
        if (self::openProcess($action)) {
56 9
            $output = self::doAction($action, $value);
57 9
            self::closeProcess();
58
        }
59
60 9
        return $output;
61
    }
62
63
    /**
64
     * @param $process
65
     * @return bool
66
     */
67 9
    private static function openProcess($process)
68
    {
69 9
        $return = false;
70 9
        self::$process = proc_open(
71 9
            $process,
72
            array(
73 9
                0 => array("pipe", "r"),
74
                1 => array("pipe", "w"),
75
                2 => array("pipe", "w"),
76
            ),
77 9
            self::$pipes
78
        );
79 9
        if (is_resource(self::$process)) {
80 9
            $return = true;
81
        }
82
83 9
        return $return;
84
    }
85
86
    /**
87
     * @return int
88
     */
89 9
    private static function closeProcess()
90
    {
91 9
        foreach (self::$pipes as $k => $v) {
92 9
            fclose(self::$pipes[$k]);
93
        }
94
95 9
        return proc_close(self::$process);
96
    }
97
98
    /**
99
     * @param $action
100
     * @param $value
101
     * @return bool|string
102
     */
103 9
    private static function doAction($action, $value)
104
    {
105 9
        $output = true;
106
        switch ($action) {
107 9
            case 'pbcopy':
108 9
                fwrite(self::$pipes[0], $value);
109 9
                break;
110 9
            case 'pbpaste':
111 9
                $output = stream_get_contents(self::$pipes[1]);
112 9
                if (mb_strlen($output) < 1) {
113 9
                    $output = false;
114
                }
115 9
                break;
116
        }
117
118 9
        return $output;
119
    }
120
121
    /**
122
     * @param string $do
123
     * @param bool   $test
124
     */
125 5
    private static function storedClipboard($do, $test = true)
126
    {
127 5
        if ($test) {
128 1
            self::$clipboard = self::action($do, self::$clipboard);
129
        }
130 5
    }
131
132
    /**
133
     * @param $options
134
     * @return mixed
135
     */
136 5
    private static function configureArray($options)
137
    {
138 5
        $config = array();
139 5
        $config['reset'] = self::setOption('reset', false, $options);
140 5
        $config['depth'] = self::setOption('depth', 0, $options);
141 5
        $config['wait'] = self::setOption('wait', 1, $options);
142 5
        $config['heartbeat'] = self::setOption('heartbeat', self::defaultHeartbeat($config['wait']), $options);
143
144 5
        return $config;
145
    }
146
147
    /**
148
     * @param $wait
149
     * @return \Closure
150
     */
151
    private static function defaultHeartbeat($wait)
152
    {
153 5
        return function ($result) use ($wait) {
154 5
            $return = false;
155 5
            if ($result) {
156 5
                sleep($wait);
157 5
                $return = true;
158
            }
159
160 5
            return $return;
161 5
        };
162
    }
163
164
    /**
165
     * @param string $name
166
     * @param        $default
167
     * @param        $requested
168
     * @return mixed
169
     */
170 5
    private static function setOption($name, $default, $requested)
171
    {
172 5
        if (isset($requested[$name])) {
173 4
            return $requested[$name];
174
        } else {
175 5
            return $default;
176
        }
177
    }
178
179
    /**
180
     * @param $value
181
     * @param $config
182
     * @return bool
183
     */
184 5
    private static function setArrayValue($value, $config)
185
    {
186 5
        if (!is_array($value)) {
187 5
            $return = $config['heartbeat'](self::set($value));
188 3
        } elseif ($config['depth'] != 0) {
189 3
            $return = self::setArray(
190 3
                $value,
191 3
                array('depth' => $config['depth'] - 1, 'heartbeat' => $config['heartbeat'],)
192
            );
193
        } else {
194 2
            $return = true;
195
        }
196
197 5
        return $return;
198
    }
199
}
200