MethodActionGenerator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromObject() 0 13 4
A configure() 0 4 1
A get() 0 4 1
A getId() 0 3 1
A actionId() 0 3 1
1
<?php
2
namespace rtens\domin\reflection;
3
4
use rtens\domin\ActionRegistry;
5
use rtens\domin\reflection\types\TypeFactory;
6
7
class MethodActionGenerator {
8
9
    const SEPERATOR = '~';
10
11
    private $actions;
12
    private $types;
13
    private $parser;
14
15
    public function __construct(ActionRegistry $actions, TypeFactory $types, CommentParser $parser) {
16
        $this->actions = $actions;
17
        $this->types = $types;
18
        $this->parser = $parser;
19
    }
20
21
    public function fromObject($object) {
22
        $class = new \ReflectionClass($object);
23
24
        foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
25
            if ($method->isStatic() || substr($method->name, 0, 1) == '_') {
26
                continue;
27
            }
28
            $this->actions->add(self::getId($method),
29
                new GenericMethodAction($object, $method->name, $this->types, $this->parser));
30
        }
31
32
        return $this;
33
    }
34
35
    public function configure($object, $method, callable $callback) {
36
        $callback($this->get($object, $method));
37
        return $this;
38
    }
39
40
    private function get($object, $method) {
41
        $method = new \ReflectionMethod(get_class($object), $method);
42
        return $this->actions->getAction(self::getId($method));
43
    }
44
45
    private static function getId(\ReflectionMethod $method) {
46
        return $method->getDeclaringClass()->getShortName() . self::SEPERATOR . $method->name;
47
    }
48
49
    public static function actionId($class, $method) {
50
        return self::getId(new \ReflectionMethod($class, $method));
51
    }
52
}