Completed
Pull Request — master (#190)
by
unknown
03:11
created

Context::getEntityResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
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 Doctrine\Common\Persistence\ObjectManager;
7
use Knp\FriendlyContexts\Dictionary\Backgroundable;
8
use Knp\FriendlyContexts\Dictionary\Taggable;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
11
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
12
13
abstract class Context implements ContextInterface
14
{
15
    use Backgroundable,
16
        Taggable;
17
18
    protected $config = [];
19
    protected $container;
20
21
    public function initialize(array $config, ContainerInterface $container)
22
    {
23
        $this->config    = array_merge($this->getDefaultOptions(), $config);
24
        $this->container = $container;
25
    }
26
27
    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...
28
    {
29
        return $this->get('friendly.record.bag');
30
    }
31
32
    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...
33
    {
34
        return $this->get('friendly.entity.hydrator');
35
    }
36
37
    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...
38
    {
39
        return $this->get('friendly.entity.resolver');
40
    }
41
42
    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...
43
    {
44
        return $this->get('friendly.text.formater');
45
    }
46
47
    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...
48
    {
49
        return $this->get('friendly.asserter');
50
    }
51
52
    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...
53
    {
54
        return $this->get('friendly.guesser.manager');
55
    }
56
57
    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...
58
    {
59
        return $this->get('friendly.object.reflector');
60
    }
61
62
    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...
63
    {
64
        return $this->get('friendly.feature.walker');
65
    }
66
67
    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...
68
    {
69
        return $this->get('friendly.alice.fixtures.loader');
70
    }
71
72
    /**
73
     * Gets Doctrine manager
74
     *
75
     * @return ObjectManager
76
     */
77
    protected function getManager()
78
    {
79
        return $this->get($this->config['doctrine']['service'])->getManager();
80
    }
81
82
    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...
83
    {
84
        return $this->get('friendly.unique_cache');
85
    }
86
87
    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...
88
    {
89
        return $this->get('friendly.page.resolver');
90
    }
91
92
    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...
93
    {
94
        return $this->get('friendly.builder.request_builder');
95
    }
96
97
    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...
98
    {
99
        return $this->get('friendly.http.http_content_type_guesser');
100
    }
101
102 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...
103
    {
104
        if ($this->container->has($service)) {
105
            return $this->container->get($service);
106
        }
107
108
        if (null !== $this->getKernel() && $this->getKernel()->getContainer()->has($service)) {
109
            return $this->getKernel()->getContainer()->get($service);
110
        }
111
112
        throw new ServiceNotFoundException($service);
113
    }
114
115 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...
116
    {
117
        if ($this->container->hasParameter($name)) {
118
            return $this->container->getParameter($name);
119
        }
120
121
        if (null !== $this->getKernel() && $this->getKernel()->getContainer()->hasParameter($name)) {
122
            return $this->getKernel()->getContainer()->getParameter($name);
123
        }
124
125
        throw new ParameterNotFoundException($name);
126
    }
127
128
    protected function getKernel()
129
    {
130
        if ($this->container->has('friendly.symfony.kernel')) {
131
            $kernel = $this->container->get('friendly.symfony.kernel');
132
            $kernel->boot();
133
134
            return $kernel;
135
        }
136
    }
137
138
    protected function resolveEntity($name)
139
    {
140
        $namespaces = $this->getParameter('friendly.entities.namespaces');
141
142
        $entities = $this
143
            ->getEntityResolver()
144
            ->resolve(
145
                $this->getManager(),
146
                $name,
147
                empty($namespaces) ? '' : $namespaces
148
            )
149
        ;
150
151
        switch (true) {
152
            case 1 < count($entities):
153
                throw new \Exception(
154
                    sprintf(
155
                        'Failed to find a unique model from the name "%s", "%s" found',
156
                        $name,
157
                        implode('" and "', array_map(
158
                            function ($rfl) {
159
                                return $rfl->getName();
160
                            },
161
                            $entities
162
                        ))
163
                    )
164
                );
165
                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...
166
            case 0 === count($entities):
167
                throw new \Exception(
168
                    sprintf(
169
                        'Failed to find a model from the name "%s"',
170
                        $name
171
                    )
172
                );
173
        }
174
175
        return current($entities);
176
    }
177
178
    protected function getDefaultOptions()
179
    {
180
        return [];
181
    }
182
}
183