Completed
Push — master ( 57ca71...6cb409 )
by Dane
05:41
created

Pasteboard::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dlindberg\Pasteboard;
4
5
class Pasteboard
6
{
7
8 9
    public static function set($value)
9
    {
10 9
        return self::action('pbcopy', $value);
11
    }
12
13 9
    public static function get()
14
    {
15 9
        return self::action('pbpaste');
16
    }
17
18 5
    public static function setArray($array, $options = array())
19
    {
20 5
        if (isset($options['depth'])) {
21 3
            $depth = $options['depth'];
22
        } else {
23 2
            $depth = 0;
24
        }
25 5
        if (isset($options['wait'])) {
26 4
            $wait = $options['wait'];
27
        } else {
28 4
            $wait = 1;
29
        }
30 5
        if (isset($options['heartbeat'])) {
31 3
            $heartbeat = $options['heartbeat'];
32
        } else {
33 5
            $heartbeat = function ($result) use ($wait) {
34 5
                $return = false;
35 5
                if ($result) {
36 5
                    sleep($wait);
37 5
                    $return = true;
38
                }
39
40 5
                return $return;
41 5
            };
42
        }
43 5
        $initial = null;
44 5
        if (isset($options['reset']) && $options['reset']) {
45 1
            $initial = self::get();
46
        }
47 5
        foreach ($array as $value) {
48 5
            if (!is_array($value)) {
49 5
                if (!$heartbeat(self::set($value))) {
50 5
                    return false;
51
                }
52 3
            } elseif ($depth != 0) {
53 3
                if (!self::setArray($value, array('depth' => $depth - 1, 'heartbeat' => $heartbeat,))) {
54 5
                    return false;
55
                }
56
            }
57
        }
58 5
        if (isset($options['reset']) && $options['reset']) {
59 1
            self::set($initial);
60
        }
61
62 5
        return true;
63
    }
64
65 9
    private static function action($action, $value = null)
66
    {
67 9
        $output = false;
68 9
        $do = proc_open(
69 9
            $action,
70
            array(
71 9
                0 => array("pipe", "r"),
72
                1 => array("pipe", "w"),
73
                2 => array("pipe", "w"),
74
            ),
75 9
            $pipes
76
        );
77 9
        if (is_resource($do)) {
78
            switch ($action) {
79 9
                case 'pbcopy':
80 9
                    if (isset($value)) {
81 9
                        fwrite($pipes[0], $value);
82
                    }
83 9
                    break;
84 9
                case 'pbpaste':
85 9
                    $output = stream_get_contents($pipes[1]);
86 9
                    break;
87
            }
88 9
            foreach ($pipes as $k => $v) {
89 9
                 fclose($pipes[$k]);
90
            }
91 9
            $status = proc_close($do);
92
        }
93 9
        if (isset($status) && $status === 0 && $output === false) {
94 9
            $output = true;
95 9
        } elseif (isset($status) && $status === 0 && mb_strlen($output) === 0) {
96 9
            $output = false;
97
        }
98 9
        return $output;
99
    }
100
}
101