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

ChangeReverser   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 46
rs 10
c 2
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setDriver() 0 3 1
A call() 0 5 1
A reverseArguments() 0 12 3
B reverseMethod() 0 15 7
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