Context::getRequestBuilder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Knp\FriendlyContexts\Context;
4
5
use Behat\Behat\Context\Context as ContextInterface;
6
use Knp\FriendlyContexts\Dictionary\Backgroundable;
7
use Knp\FriendlyContexts\Dictionary\Taggable;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
10
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
11
12
abstract class Context implements ContextInterface
13
{
14
    use Backgroundable,
15
        Taggable;
16
17
    protected $config = [];
18
    protected $container;
19
20
    public function initialize(array $config, ContainerInterface $container)
21
    {
22
        $this->config    = array_merge($this->getDefaultOptions(), $config);
23
        $this->container = $container;
24
    }
25
26
    protected function getRecordBag()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
27
    {
28
        return $this->get('friendly.record.bag');
29
    }
30
31
    protected function getEntityHydrator()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
32
    {
33
        return $this->get('friendly.entity.hydrator');
34
    }
35
36
    protected function getEntityResolver()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
37
    {
38
        return $this->get('friendly.entity.resolver');
39
    }
40
41
    protected function getTextFormater()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
42
    {
43
        return $this->get('friendly.text.formater');
44
    }
45
46
    protected function getAsserter()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
47
    {
48
        return $this->get('friendly.asserter');
49
    }
50
51
    protected function getGuesserManager()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
52
    {
53
        return $this->get('friendly.guesser.manager');
54
    }
55
56
    protected function getObjectReflector()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
57
    {
58
        return $this->get('friendly.object.reflector');
59
    }
60
61
    protected function getFeatureWalker()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
62
    {
63
        return $this->get('friendly.feature.walker');
64
    }
65
66
    protected function getAliceLoader()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        return $this->get('friendly.alice.fixtures.loader');
69
    }
70
71
    protected function getEntityManager()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
72
    {
73
        return $this->get('doctrine')->getManager();
74
    }
75
76
    protected function getUniqueCache()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
77
    {
78
        return $this->get('friendly.unique_cache');
79
    }
80
81
    protected function getPageClassResolver()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
82
    {
83
        return $this->get('friendly.page.resolver');
84
    }
85
86
    protected function getRequestBuilder()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
87
    {
88
        return $this->get('friendly.builder.request_builder');
89
    }
90
91
    protected function getHttpContentTypeGuesser()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
92
    {
93
        return $this->get('friendly.http.http_content_type_guesser');
94
    }
95
96 View Code Duplication
    protected function get($service)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if ($this->container->has($service)) {
99
            return $this->container->get($service);
100
        }
101
102
        if (null !== $this->getKernel() && $this->getKernel()->getContainer()->has($service)) {
103
            return $this->getKernel()->getContainer()->get($service);
104
        }
105
106
        throw new ServiceNotFoundException($service);
107
    }
108
109 View Code Duplication
    protected function getParameter($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if ($this->container->hasParameter($name)) {
112
            return $this->container->getParameter($name);
113
        }
114
115
        if (null !== $this->getKernel() && $this->getKernel()->getContainer()->hasParameter($name)) {
116
            return $this->getKernel()->getContainer()->getParameter($name);
117
        }
118
119
        throw new ParameterNotFoundException($name);
120
    }
121
122
    protected function getKernel()
123
    {
124
        if ($this->container->has('friendly.symfony.kernel')) {
125
            $kernel = $this->container->get('friendly.symfony.kernel');
126
            $kernel->boot();
127
128
            return $kernel;
129
        }
130
    }
131
132
    protected function resolveEntity($name)
133
    {
134
        $namespaces = $this->getParameter('friendly.entities.namespaces');
135
136
        $entities = $this
137
            ->getEntityResolver()
138
            ->resolve(
139
                $this->getEntityManager(),
140
                $name,
141
                empty($namespaces) ? '' : $namespaces
142
            )
143
        ;
144
145
        switch (true) {
146
            case 1 < count($entities):
147
                throw new \Exception(
148
                    sprintf(
149
                        'Failed to find a unique model from the name "%s", "%s" found',
150
                        $name,
151
                        implode('" and "', array_map(
152
                            function ($rfl) {
153
                                return $rfl->getName();
154
                            },
155
                            $entities
156
                        ))
157
                    )
158
                );
159
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
160
            case 0 === count($entities):
161
                throw new \Exception(
162
                    sprintf(
163
                        'Failed to find a model from the name "%s"',
164
                        $name
165
                    )
166
                );
167
        }
168
169
        return current($entities);
170
    }
171
172
    protected function getDefaultOptions()
173
    {
174
        return [];
175
    }
176
}
177