Please::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 11
nc 4
nop 2
1
<?php
2
3
namespace ganglio;
4
5
class Please
6
{
7
    private $result;
8
9
    public $isSuccess;
10
    public $isFailure;
11
12
    public function __construct($callable, $args)
13
    {
14
        if (!is_array($args)) {
15
            $args = [$args];
16
        }
17
        try {
18
            $this->result = call_user_func_array($callable, $args);
19
            $this->isSuccess = true;
20
            $this->isFailure = false;
21
        } catch (\Exception $e) {
22
            $this->result = $e;
23
            $this->isSuccess = false;
24
            $this->isFailure = true;
25
        }
26
    }
27
28
    public function get()
29
    {
30
        return $this->result;
31
    }
32
33
    public function onSuccess($callable)
34
    {
35
        if ($this->isSuccess) {
36
            return new Please($callable, $this->result);
37
        }
38
    }
39
40
    public function onFailure($callable)
41
    {
42
        if ($this->isFailure) {
43
            return new Please($callable, $this->result);
44
        }
45
    }
46
47
    public function on($successCallable, $failureCallable)
48
    {
49
        if ($this->isSuccess) {
50
            return new Please($successCallable, $this->result);
51
        }
52
53
        if ($this->isFailure) {
54
            return new Please($failureCallable, $this->result);
55
        }
56
    }
57
}
58