Completed
Push — master ( 4e4f47...806721 )
by Ivannis Suárez
02:27
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Bus\Middlewares\Handler\Resolver\HandlerMethodName;
13
14
/**
15
 * MethodWithShortObjectNameAndSuffixResolver class.
16
 *
17
 * Examples:
18
 *  - Cubiche\Application\Command\ChangeTitleCommand => $fooTaskCommandHandler->changeTitleHandler()
19
 *  - Cubiche\Application\Query\PostListQuery => $postListQueryHandler->postListHandler()
20
 *
21
 * @author Ivannis Suárez Jerez <[email protected]>
22
 */
23
class MethodWithShortObjectNameAndSuffixResolver extends MethodWithObjectNameResolver
24
{
25
    /**
26
     * @var string
27
     */
28
    private $suffixToRemove;
29
30
    /**
31
     * @var string
32
     */
33
    private $suffixToAdd;
34
35
    /**
36
     * @var int
37
     */
38
    private $suffixLength;
39
40
    /**
41
     * @param string $suffixToRemove The string to remove from end of each class name
42
     * @param string $suffixToAdd    The string to add to the end of each class name
43
     */
44
    public function __construct($suffixToRemove = 'Message', $suffixToAdd = 'Handler')
45
    {
46
        $this->suffixToRemove = $suffixToRemove;
47
        $this->suffixToAdd = $suffixToAdd;
48
        $this->suffixLength = strlen($suffixToRemove);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function resolve($className)
55
    {
56
        $methodName = parent::resolve($className);
57
        if (substr($methodName, $this->suffixLength * -1) !== $this->suffixToRemove) {
58
            throw new \Exception(sprintf('Invalid suffix %s', $this->suffixToRemove));
59
        }
60
61
        return substr($methodName, 0, strlen($methodName) - $this->suffixLength).$this->suffixToAdd;
62
    }
63
}
64