Passed
Push — main ( 6a8c3e...b8efbe )
by James Ekow Abaka
09:56 queued 08:09
created

ChangeReverser::reverseMethod()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 1
nop 1
dl 0
loc 15
rs 8.8333
c 0
b 0
f 0
1
<?php
2
namespace yentu;
3
4
class ChangeReverser
5
{
6
    private $driver;
7
8
    public function setDriver($driver)
9
    {
10
        $this->driver = $driver;
11
    }
12
13
    public function call($method, $arguments) 
14
    {
15
        $reversedMethod = $this->reverseMethod($method);
16
        $reversedArguments = $this->reverseArguments($arguments[0]);
17
        return $this->driver->$reversedMethod($reversedArguments);
18
    }    
19
    
20
    private function reverseMethod($method)
21
    {
22
        return preg_replace_callback(
23
            "/^(?<action>add|drop|reverse|execute|insert)/", 
24
            function($matches){
25
                switch($matches['action'])
26
                {
27
                    case 'add': return 'drop';
28
                    case 'drop': return 'add';
29
                    case 'reverse': return 'execute';
30
                    case 'execute': return 'reverse';
31
                    case 'insert': return 'delete';
32
                    case 'delete': return 'insert';
33
                }
34
            }, $method
35
        );
36
    }
37
    
38
    private function reverseArguments($arguments)
39
    {
40
        if(isset($arguments['from']) && isset($arguments['to']))
41
        {
42
            return array(
43
                'to' => $arguments['from'],
44
                'from' => $arguments['to']
45
            );
46
        }
47
        else
48
        {
49
            return $arguments;
50
        }
51
    }
52
}
53