ClassNameWithoutSuffixInflector::inflect()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 4
nc 4
nop 2
crap 4
1
<?php
2
3
namespace DMT\WebservicesNl\Client\Command\Handler\MethodNameInflector;
4
5
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
6
7
/**
8
 * Class ClassNameWithoutSuffixInflector
9
 *
10
 * @package DMT\WebservicesNl\Client
11
 */
12
class ClassNameWithoutSuffixInflector implements MethodNameInflector
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $suffix;
18
19
    /**
20
     * @var int
21
     */
22
    protected $suffixLength = 0;
23
24
    /**
25
     * ClassNameWithoutSuffixInflector constructor.
26
     *
27
     * @param string $suffix
28
     */
29 10
    public function __construct(string $suffix = 'Command')
30
    {
31 10
        $this->suffix = $suffix;
32 10
        $this->suffixLength = strlen($suffix);
33 10
    }
34
35
    /**
36
     * Return the method name to call on the command handler and return it.
37
     *
38
     * Examples:
39
     *  - \CompleteTaskCommand     => $handler->completeTask()
40
     *  - \My\App\DoThingCommand   => $handler->doThing()
41
     *
42
     * @param object $command
43
     * @param object $commandHandler
44
     *
45
     * @return string
46
     */
47 6
    public function inflect($command, $commandHandler)
48
    {
49 6
        $commandName = get_class($command);
50
51 6
        $start = strpos($commandName, '\\') !== false ? strrpos($commandName, '\\') + 1 : 0;
52 6
        $length = strpos($commandName, $this->suffix, -$this->suffixLength) !== false ? $this->suffixLength : null;
53
54 6
        return lcfirst($length ? substr($commandName, $start, -$length) : substr($commandName, $start));
55
    }
56
}
57