Test Failed
Pull Request — master (#16)
by Divine Niiquaye
02:34
created

ResourceHandler::namespace()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Flight Routing.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Flight\Routing\Handlers;
19
20
/**
21
 * An extendable HTTP Verb-based route handler to provide a RESTful API for a resource.
22
 *
23
 * @author Divine Niiquaye Ibok <[email protected]>
24
 */
25
final class ResourceHandler
26
{
27
    /** @var string|object */
28
    private $classResource;
29
30
    /** @var string */
31
    private $actionResource;
32
33
    /**
34
     * @param class-string|object $class  of class string or class object
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|object at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|object.
Loading history...
35
     * @param string              $action The method name eg: action -> getAction
36
     */
37
    public function __construct($class, string $action = 'action')
38
    {
39
        $this->classResource = $class;
40
        $this->actionResource = \ucfirst($action);
41
    }
42
43
    /**
44
     * Append a missing namespace to resource class.
45
     *
46
     * @internal
47
     */
48
    public function namespace(string $namespace): self
49
    {
50
        $resource = $this->classResource;
51
52
        if (\is_string($resource) && '\\' === $resource[0]) {
53
            $this->classResource = $namespace . $resource;
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return array<int,object|string>
61
     */
62
    public function __invoke(string $requestMethod): array
63
    {
64
        return [$this->classResource, \strtolower($requestMethod) . $this->actionResource];
65
    }
66
}
67