Completed
Push — development ( b626b0...ff940a )
by Thomas
16s
created

GlobalContextFactory::createFromRequestStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\GlobalContext;
4
5
use Oc\GlobalContext\Provider\LanguageProvider;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\RequestStack;
8
9
/**
10
 * Class GlobalContextFactory
11
 */
12
class GlobalContextFactory
13
{
14
    /**
15
     * @var LanguageProvider
16
     */
17
    private $languageProvider;
18
19
    /**
20
     * GlobalContextFactory constructor.
21
     *
22
     * @param LanguageProvider $languageProvider
23
     */
24
    public function __construct(LanguageProvider $languageProvider)
25
    {
26
        $this->languageProvider = $languageProvider;
27
    }
28
29
    /**
30
     * Creates a global context from given request.
31
     *
32
     * @param Request $request
33
     *
34
     * @return GlobalContext
35
     */
36
    public function createFromRequest(Request $request)
37
    {
38
        return new GlobalContext(
39
            $this->languageProvider->getDefaultLanguage(),
40
            $this->languageProvider->getPreferredLanguage($request)
41
        );
42
    }
43
44
    /**
45
     * Creates a global context from request stack.
46
     *
47
     * @param RequestStack $requestStack
48
     *
49
     * @return GlobalContext
50
     */
51
    public function createFromRequestStack(RequestStack $requestStack)
52
    {
53
        return $this->createFromRequest(
54
            $requestStack->getMasterRequest()
0 ignored issues
show
Bug introduced by
It seems like $requestStack->getMasterRequest() can be null; however, createFromRequest() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
55
        );
56
    }
57
}
58