Completed
Push — master ( f2e5b1...ca7425 )
by Dane
09:24
created

Pasteboard::set()   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 1
crap 1
1
<?php
2
3
namespace Dlindberg\Pasteboard;
4
5
class Pasteboard
6
{
7
    private static $clipboard = null;
8
9 9
    public static function set($value)
10
    {
11 9
        return self::action('pbcopy', $value);
12
    }
13
14 9
    public static function get()
15
    {
16 9
        return self::action('pbpaste');
17
    }
18
19 5
    public static function setArray($array, $options = array())
20
    {
21 5
        $config = self::configureArray($options);
22 5
        self::storedClipboard('get', $config['reset']);
23 5
        foreach ($array as $value) {
24 5
            if (!self::setArrayValue($value, $config)) {
25 5
                return false;
26
            }
27
        }
28 5
        self::storedClipboard('set', $config['reset']);
29
30 5
        return true;
31
    }
32
33 9
    private static function action($action, $value = null)
34
    {
35 9
        $output = false;
36 9
        $do = proc_open(
37 9
            $action,
38
            array(
39 9
                0 => array("pipe", "r"),
40
                1 => array("pipe", "w"),
41
                2 => array("pipe", "w"),
42
            ),
43 9
            $pipes
44
        );
45 9
        if (is_resource($do)) {
46
            switch ($action) {
47 9
                case 'pbcopy':
48 9
                    if (isset($value)) {
49 9
                        fwrite($pipes[0], $value);
50
                    }
51 9
                    break;
52 9
                case 'pbpaste':
53 9
                    $output = stream_get_contents($pipes[1]);
54 9
                    break;
55
            }
56 9
            foreach ($pipes as $k => $v) {
57 9
                fclose($pipes[$k]);
58
            }
59 9
            $status = proc_close($do);
60
        }
61 9
        if (isset($status) && $status === 0 && $output === false) {
62 9
            $output = true;
63 9
        } elseif (isset($status) && $status === 0 && mb_strlen($output) === 0) {
64 9
            $output = false;
65
        }
66
67 9
        return $output;
68
    }
69
70 5
    private static function storedClipboard($do, $test = true)
71
    {
72 5
        if ($test) {
73 1
            self::action($do, self::$clipboard);
74
        }
75 5
    }
76
77 5
    private static function configureArray($initial)
78
    {
79
        $config = array(
80 5
            'reset' => false,
81
            'depth' => 0,
82
            'wait'  => 1,
83
        );
84 5
        if (isset($initial['reset'])) {
85 1
            $config['reset'] = $initial['reset'];
86
        }
87 5
        if (isset($initial['depth'])) {
88 3
            $config['depth'] = $initial['depth'];
89
        }
90 5
        if (isset($initial['wait'])) {
91 4
            $config['wait'] = $initial['wait'];
92
        }
93 5
        if (isset($initial['heartbeat'])) {
94 3
            $config['heartbeat'] = $initial['heartbeat'];
95
        } else {
96 5
            $config['heartbeat'] = function ($result) use ($config)
97
            {
98 5
                $return = false;
99 5
                if ($result) {
100 5
                    sleep($config['wait']);
101 5
                    $return = true;
102
                }
103
104 5
                return $return;
105
            };
106
        }
107
108 5
        return $config;
109
    }
110
111 5
    private static function setArrayValue($value, $config)
112
    {
113 5
        if (!is_array($value)) {
114 5
            $return = $config['heartbeat'](self::set($value));
115 3
        } elseif ($config['depth'] != 0) {
116 3
            $return = self::setArray($value,
117 3
                array('depth' => $config['depth'] - 1, 'heartbeat' => $config['heartbeat'],));
118
        } else {
119 2
            $return = true;
120
        }
121
122 5
        return $return;
123
    }
124
}
125