Passed
Push — master ( b0b5e8...1db8b5 )
by Php Easy Api
03:32
created

Macro::checkMacroInstanceOf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Support;
4
5
use Resta\Contracts\MacroAbleContracts;
6
use Resta\Foundation\ApplicationProvider;
7
8
class Macro extends ApplicationProvider
9
{
10
    /**
11
     * @var bool $isMacro
12
     */
13
    protected $isMacro = false;
14
15
    /**
16
     * @var $macro
0 ignored issues
show
Documentation Bug introduced by
The doc comment $macro at position 0 could not be parsed: Unknown type name '$macro' at position 0 in $macro.
Loading history...
17
     */
18
    protected $macro;
19
20
    /**
21
     * @var $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment $class at position 0 could not be parsed: Unknown type name '$class' at position 0 in $class.
Loading history...
22
     */
23
    protected $class;
24
25
    /**
26
     * check conditions for macro
27
     *
28
     * @param bool $static
29
     * @return bool
30
     */
31
    protected function checkMacroConditions($static=false)
32
    {
33
        return is_string($this->macro) &&
34
            Utils::isNamespaceExists($this->macro) &&
35
            $this->checkMacroInstanceOf($static);
36
    }
37
38
    /**
39
     * check macro instanceOf or static class
40
     *
41
     * @param bool $static
42
     * @return bool
43
     */
44
    protected function checkMacroInstanceOf($static=false)
45
    {
46
        if($static){
47
            return true;
48
        }
49
        return $this->app->resolve($this->macro) instanceof MacroAbleContracts;
50
    }
51
52
    /**
53
     * get macro object
54
     *
55
     * @param null|string $method
56
     * @param callable $callback
57
     * @return mixed
58
     */
59
    public function get($method=null,callable $callback)
60
    {
61
        if($this->isMacro){
62
63
            if(is_null($method) && Utils::isNamespaceExists($this->macro)){
64
                return $this->app->resolve($this->macro);
65
            }
66
67
            if(method_exists($resolve = $this->app->resolve($this->macro),$method)){
68
                return $resolve->macro($this->class);
69
            }
70
        }
71
        return call_user_func($callback);
72
    }
73
74
    /**
75
     * is availability macro for class
76
     *
77
     * @param bool $static
78
     * @param $class
79
     * @return $this
80
     */
81
    public function isMacro($class,$static=false)
82
    {
83
        // if the macro class is a valid object,
84
        // then this macro will return a boolean value if it has the specified methode.
85
        if($this->checkMacroConditions($static)){
86
87
            $this->isMacro  = true;
88
            $this->class    = $class;
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * check been runnable with which macro
96
     *
97
     * @param $macro
98
     * @param $concrete
99
     * @param $method
100
     * @return mixed
101
     */
102
    public function with($macro,$concrete,$method=null)
103
    {
104
        if($this->macro === null){
105
            return $this($macro)->isMacro($concrete)->get($method,function() use($concrete){
106
                return $concrete;
107
            });
108
        }
109
    }
110
111
    /**
112
     * check been runnable with which static macro
113
     *
114
     * @param $macro
115
     * @param $concrete
116
     * @param $method
117
     * @return mixed
118
     */
119
    public function withStatic($macro,$concrete)
120
    {
121
        if($this->macro === null){
122
123
            return $this($macro)->isMacro($concrete,true)->get(null,is_callable($concrete) ?
124
                $concrete : function() use($concrete){
125
                    return $concrete;
126
                });
127
        }
128
    }
129
130
    /**
131
     * invoke construct for macro
132
     *
133
     * @param null $macro
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $macro is correct as it would always require null to be passed?
Loading history...
134
     * @return $this
135
     */
136
    public function __invoke($macro=null)
137
    {
138
        if($macro!==null){
0 ignored issues
show
introduced by
The condition $macro !== null is always false.
Loading history...
139
            $this->macro = $macro;
140
        }
141
        return $this;
142
    }
143
}