Test Failed
Branch master (effa58)
by Divine Niiquaye
02:13
created

ResourceHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
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.4 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
    private string $actionResource;
31
32
    /**
33
     * @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...
34
     * @param string              $action The method name eg: action -> getAction
35
     */
36
    public function __construct($class, string $action = 'action')
37
    {
38
        $this->classResource = $class;
39
        $this->actionResource = \ucfirst($action);
40
    }
41
42
    /**
43
     * Append a missing namespace to resource class.
44
     *
45
     * @internal
46
     */
47
    public function namespace(string $namespace): self
48
    {
49
        $resource = $this->classResource;
50
51
        if (\is_string($resource) && '\\' === $resource[0]) {
52
            $this->classResource = $namespace . $resource;
53
        }
54
55
        return $this;
56
    }
57
58
    /**
59
     * @return array<int,object|string>
60
     */
61
    public function __invoke(string $requestMethod): array
62
    {
63
        return [$this->classResource, \strtolower($requestMethod) . $this->actionResource];
64
    }
65
}
66