UrlManager   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 46
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseRequest() 0 7 2
A getRules() 0 9 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-rest/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-rest
7
 */
8
9
namespace flipbox\craft\rest;
10
11
use Craft;
12
use craft\events\RegisterUrlRulesEvent;
13
14
/**
15
 * @inheritdoc
16
 *
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class UrlManager extends \yii\web\UrlManager
21
{
22
    /**
23
     * The event name when registering RESTful Url Rules
24
     */
25
    const EVENT_REGISTER_REST_URL_RULES = 'registerRestUrlRules';
26
27
    /**
28
     * @var array the default configuration of URL rules. Individual rule configurations
29
     * specified via [[rules]] will take precedence when the same property of the rule is configured.
30
     */
31
    public $ruleConfig = ['class' => UrlRule::class];
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function __construct(array $config = [])
37
    {
38
        $config['rules'] = $this->getRules();
39
        parent::__construct($config);
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    private function getRules()
46
    {
47
        $event = new RegisterUrlRulesEvent();
48
        $this->trigger(
49
            self::EVENT_REGISTER_REST_URL_RULES,
50
            $event
51
        );
52
53
        return array_filter($event->rules);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function parseRequest($request)
60
    {
61
        if (!$result = parent::parseRequest($request)) {
62
            Craft::warning("Unable to parse request: " . $request->getUrl());
63
        }
64
65
        return $result;
66
    }
67
}
68