Passed
Push — master ( 12242c...e9b436 )
by Iman
08:01
created

RouteSituations   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 20
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A hasMethod() 0 10 1
A getNormalizedArgs() 0 15 3
A __call() 0 4 1
1
<?php
2
3
namespace Imanghafoori\HeyMan\WatchingStrategies\Routes;
4
5
use Imanghafoori\HeyMan\Situations\BaseSituation;
6
7
final class RouteSituations extends BaseSituation
8
{
9
    public function hasMethod($method)
10
    {
11
        return in_array($method, [
12
            'whenYouVisitUrl',
13
            'whenYouSendPost',
14
            'whenYouSendPatch',
15
            'whenYouSendPut',
16
            'whenYouSendDelete',
17
            'whenYouCallAction',
18
            'whenYouHitRouteName',
19
        ]);
20
    }
21
22
    public function __call($method, $args)
23
    {
24
        $args = $this->getNormalizedArgs($method, $args);
25
        $this->setManager(RouterEventManager::class, $args);
26
    }
27
28
    /**
29
     * @param $method
30
     * @param $args
31
     *
32
     * @return array
33
     */
34
    private function getNormalizedArgs($method, $args): array
35
    {
36
        $normalizer = resolve(RouteNormalizer::class);
37
        $method = str_replace('whenYou', '', $method);
38
        if ($method == 'CallAction') {
39
            return $normalizer->normalizeAction($args);
40
        }
41
        if ($method == 'HitRouteName') {
42
            return $args;
43
        }
44
45
        $method = str_replace('VisitUrl', 'SendGet', $method);
46
        $method = str_replace('Send', '', $method);
47
48
        return $normalizer->normalizeUrl($args, strtoupper($method));
49
    }
50
}
51