Completed
Push — master ( c4135a...b9e764 )
by
unknown
42s queued 12s
created

RoutingHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 23
c 1
b 0
f 0
dl 0
loc 69
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRouteFromDestination() 0 18 3
A __construct() 0 4 1
A resolveModuleName() 0 10 2
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerShop\Yves\ShopApplication\Twig;
9
10
use LogicException;
11
use Silex\Application;
12
use SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface;
13
14
class RoutingHelper implements RoutingHelperInterface
15
{
16
    /**
17
     * @var \Silex\Application
18
     */
19
    protected $app;
20
21
    /**
22
     * @var \Spryker\Shared\Kernel\Store
23
     */
24
    protected $store;
25
26
    /**
27
     * @var \SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface
28
     */
29
    protected $utilTextService;
30
31
    /**
32
     * @param \Silex\Application $app
33
     * @param \SprykerShop\Yves\ShopApplication\Dependency\Service\ShopApplicationToUtilTextServiceInterface $utilTextService
34
     */
35
    public function __construct(Application $app, ShopApplicationToUtilTextServiceInterface $utilTextService)
36
    {
37
        $this->app = $app;
38
        $this->utilTextService = $utilTextService;
39
    }
40
41
    /**
42
     * @param string $destination
43
     *
44
     * @throws \LogicException
45
     *
46
     * @return string
47
     */
48
    public function getRouteFromDestination($destination)
49
    {
50
        if (strpos($destination, '::') !== false) {
51
            [$controllerNamespaceName, $actionName] = explode('::', $destination);
52
        } elseif (strpos($destination, ':') !== false) {
53
            [$serviceName, $actionName] = explode(':', $destination);
54
            $controllerNamespaceName = get_class($this->app[$serviceName]);
55
        } else {
56
            throw new LogicException('Cannot parse destination');
57
        }
58
        [$namespace, $application, $module, $layer, $controllerName] = explode('\\', $controllerNamespaceName);
59
60
        $module = $this->resolveModuleName($module);
61
62
        $controller = $this->utilTextService->camelCaseToSeparator(str_replace('Controller', '', $controllerName));
63
        $action = $this->utilTextService->camelCaseToSeparator((str_replace('Action', '', $actionName)));
64
65
        return $module . '/' . $controller . '/' . $action;
66
    }
67
68
    /**
69
     * @param string $moduleName
70
     *
71
     * @return string
72
     */
73
    protected function resolveModuleName(string $moduleName): string
74
    {
75
        $codeBucketIdentifierLength = mb_strlen(APPLICATION_CODE_BUCKET);
0 ignored issues
show
Bug introduced by
The constant SprykerShop\Yves\ShopApp...APPLICATION_CODE_BUCKET was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
        $codeBucketSuffix = mb_substr($moduleName, -$codeBucketIdentifierLength);
77
78
        if ($codeBucketSuffix === APPLICATION_CODE_BUCKET) {
79
            $moduleName = mb_substr($moduleName, 0, -$codeBucketIdentifierLength);
80
        }
81
82
        return $moduleName;
83
    }
84
}
85