Completed
Push — master ( c4f2a3...46ece2 )
by Elf
06:39
created

MacroRegistrar::register()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 3
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Apps;
4
5
use Illuminate\Contracts\Container\Container;
6
7
class MacroRegistrar
8
{
9
    /**
10
     * Register needed macros.
11
     *
12
     * @param  \Illuminate\Contracts\Container\Container  $container
13
     * @return void
14
     */
15 7
    public function registerMacros(Container $container)
16
    {
17 7
        $this->register(
18 7
            $container['url'],
19 7
            'getRootControllerNamespace',
20
            function () {
21
                /* @var $this \Illuminate\Routing\UrlGenerator */
22 2
                return $this->rootNamespace;
23 7
            }
24
        );
25
26 7
        $this->register(
27 7
            $container['router'],
28 7
            'hasMiddlewareGroup',
29 7
            function ($name) {
30
                /* @var $this \Illuminate\Routing\Router */
31 2
                return array_key_exists($name, $this->middlewareGroups);
32 7
            }
33
        );
34 7
    }
35
36
    /**
37
     * Register a macro to the class.
38
     *
39
     * @param  string|object  $class
40
     * @param  string  $method
41
     * @param  object|callable  $macro
42
     * @return void
43
     */
44 8
    public function register($class, $method, $macro)
45
    {
46 8
        if (! method_exists($class, $method)) {
47 8
            $class = is_object($class) ? get_class($class) : $class;
48
49 8
            call_user_func_array([$class, 'macro'], [$method, $macro]);
50
        }
51 8
    }
52
}
53