HasUrlRecordManagerTrait::compileURL()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 19
c 1
b 0
f 0
nc 128
nop 3
dl 0
loc 31
ccs 0
cts 19
cp 0
crap 72
rs 8.2111
1
<?php
2
3
namespace Nip\Records\Traits\HasUrl;
4
5
/**
6
 * Trait HasUrlRecordManagerTrait
7
 * @package Nip\Records\Traits\HasUrl
8
 */
9
trait HasUrlRecordManagerTrait
10
{
11
12
    /**
13
     * @param string $name
14
     * @param $arguments
15
     * @return bool
16
     */
17 1
    protected function isCallUrl($name, $arguments)
18
    {
19 1
        if (substr($name, 0, 3) == "get" && substr($name, -3) == "URL") {
20
            $action = substr($name, 3, -3);
21
            $params = isset($arguments[0]) ? $arguments[0] : [];
22
            $module = isset($arguments[1]) ? $arguments[1] : null;
23
24
            return $this->compileURL($action, $params, $module);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->compileURL...tion, $params, $module) also could return the type string which is incompatible with the documented return type boolean.
Loading history...
25
        }
26
27 1
        return false;
28
    }
29
30
    /**
31
     * @param string $action
32
     * @param array $params
33
     * @param null $module
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $module is correct as it would always require null to be passed?
Loading history...
34
     * @return string|null
35
     */
36
    public function compileURL($action, $params = [], $module = null)
37
    {
38
        $controller = $this->getController();
0 ignored issues
show
Bug introduced by
It seems like getController() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
        /** @scrutinizer ignore-call */ 
39
        $controller = $this->getController();
Loading history...
39
40
        if (substr($action, 0, 5) == 'Async') {
41
            $controller = 'async-' . $controller;
42
            $action = substr($action, 5);
43
        }
44
45
        if (substr($action, 0, 5) == 'Modal') {
46
            $controller = 'modal-' . $controller;
47
            $action = substr($action, 5);
48
        }
49
50
        $params['action'] = (!empty($action)) ? $action : 'index';
51
        $params['controller'] = $controller;
52
53
        $params['action'] = inflector()->unclassify($params['action']);
54
        $params['action'] = ($params['action'] == 'index') ? null : $params['action'];
55
56
        $params['controller'] = $controller ? $controller : $this->getController();
57
        $params['module'] = $module ? $module : request()->getModuleName();
0 ignored issues
show
introduced by
$module is of type null, thus it always evaluated to false.
Loading history...
58
59
        $routeName = $params['module'] . '.' . $params['controller'] . '.' . $params['action'];
60
        if (app()->get('router')->hasRoute($routeName)) {
61
            unset($params['module'], $params['controller'], $params['action']);
62
        } else {
63
            $routeName = $params['module'] . '.default';
64
        }
65
66
        return app()->get('router')->assembleFull($routeName, $params);
67
    }
68
}
69