BindingExpressionBuilder::leaveExpression()   C
last analyzed

Complexity

Conditions 11
Paths 11

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 11.3173

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 42
ccs 25
cts 29
cp 0.8621
rs 5.2653
c 1
b 0
f 0
cc 11
eloc 29
nc 11
nop 1
crap 11.3173

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the puli/manager package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
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 Puli\Manager\Asset;
13
14
use Puli\UrlGenerator\DiscoveryUrlGenerator;
15
use Webmozart\Expression\Constraint\EndsWith;
16
use Webmozart\Expression\Constraint\Equals;
17
use Webmozart\Expression\Constraint\NotEquals;
18
use Webmozart\Expression\Constraint\NotSame;
19
use Webmozart\Expression\Constraint\Same;
20
use Webmozart\Expression\Expr;
21
use Webmozart\Expression\Expression;
22
use Webmozart\Expression\Logic\Conjunction;
23
use Webmozart\Expression\Selector\Method;
24
use Webmozart\Expression\Traversal\ExpressionTraverser;
25
use Webmozart\Expression\Traversal\ExpressionVisitor;
26
27
/**
28
 * Transforms an {@link AssetMapping} expression to a {@link BindingDescriptor}
29
 * expression.
30
 *
31
 * @since  1.0
32
 *
33
 * @author Bernhard Schussek <[email protected]>
34
 *
35
 * @internal
36
 */
37
class BindingExpressionBuilder implements ExpressionVisitor
38
{
39
    /**
40
     * @var Conjunction
41
     */
42
    private $defaultExpression;
43
44
    /**
45
     * Builds a {@link BindingDescriptor} expression for a given
46
     * {@link AssetMapping} expression.
47
     *
48
     * @param Expression|null $expr The {@link AssetMapping} expression.
49
     *
50
     * @return Expression The built expression.
51
     */
52 39
    public function buildExpression(Expression $expr = null)
53
    {
54 39
        if (!$this->defaultExpression) {
55 39
            $this->defaultExpression = Expr::method('isEnabled', Expr::same(true))
56 39
                ->andMethod('getTypeName', Expr::same(DiscoveryUrlGenerator::BINDING_TYPE))
57 39
                ->andMethod('getBinding', Expr::method('getQuery', Expr::endsWith('{,/**/*}')));
58
        }
59
60 39
        if (!$expr) {
61 17
            return $this->defaultExpression;
62
        }
63
64 24
        $traverser = new ExpressionTraverser();
65 24
        $traverser->addVisitor($this);
66
67 24
        return $this->defaultExpression->andX($traverser->traverse($expr));
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 24
    public function enterExpression(Expression $expr)
74
    {
75 24
        return $expr;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 24
    public function leaveExpression(Expression $expr)
82
    {
83 24
        if ($expr instanceof Method) {
84 19
            switch ($expr->getMethodName()) {
85 19
                case 'getUuid':
86 6
                    return Expr::method('getUuid', $expr->getExpression());
87
88 14
                case 'getGlob':
89 6
                    $queryExpr = $expr->getExpression();
90
91 6
                    if ($queryExpr instanceof Same) {
92 2
                        $queryExpr = Expr::same($queryExpr->getComparedValue().'{,/**/*}');
93
                    } elseif ($queryExpr instanceof Equals) {
94 1
                        $queryExpr = Expr::equals($queryExpr->getComparedValue().'{,/**/*}');
95
                    } elseif ($queryExpr instanceof NotSame) {
96 1
                        $queryExpr = Expr::notSame($queryExpr->getComparedValue().'{,/**/*}');
97
                    } elseif ($queryExpr instanceof NotEquals) {
98 1
                        $queryExpr = Expr::notEquals($queryExpr->getComparedValue().'{,/**/*}');
99
                    } elseif ($queryExpr instanceof EndsWith) {
100 1
                        $queryExpr = Expr::endsWith($queryExpr->getAcceptedSuffix().'{,/**/*}');
101
                    }
102
103 6
                    return Expr::method('getBinding', Expr::method('getQuery', $queryExpr));
104
105 9
                case 'getServerName':
106 3
                    return Expr::method(
107 3
                        'getParameterValue',
108 3
                        DiscoveryUrlGenerator::SERVER_PARAMETER,
109 3
                        $expr->getExpression()
110
                    );
111
112 7
                case 'getServerPath':
113 7
                    return Expr::method(
114 7
                        'getParameterValue',
115 7
                        DiscoveryUrlGenerator::PATH_PARAMETER,
116 7
                        $expr->getExpression()
117
                    );
118
            }
119
        }
120
121 6
        return $expr;
122
    }
123
}
124