Completed
Push — master ( ac1fa0...9301b0 )
by Neomerx
02:58
created

CsrfContainerConfigurator::configureContainer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 1
nop 1
crap 2
1
<?php namespace Limoncello\Application\Packages\Csrf;
2
3
/**
4
 * Copyright 2015-2018 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Application\Contracts\Csrf\CsrfTokenGeneratorInterface;
20
use Limoncello\Application\Contracts\Csrf\CsrfTokenStorageInterface;
21
use Limoncello\Application\Packages\Csrf\CsrfSettings as C;
22
use Limoncello\Contracts\Application\ContainerConfiguratorInterface;
23
use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface;
24
use Limoncello\Contracts\Session\SessionInterface;
25
use Limoncello\Contracts\Settings\SettingsProviderInterface;
26
use Psr\Container\ContainerInterface as PsrContainerInterface;
27
28
/**
29
 * @package Limoncello\Application
30
 */
31
class CsrfContainerConfigurator implements ContainerConfiguratorInterface
32
{
33
    /** @var callable */
34
    const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME];
35
36
    /**
37
     * @inheritdoc
38
     *
39
     * @SuppressWarnings(PHPMD.StaticAccess)
40
     */
41 3
    public static function configureContainer(LimoncelloContainerInterface $container): void
42
    {
43 3
        $storage = null;
44
        $factory = function (PsrContainerInterface $container) use (&$storage) {
45 3
            if ($storage === null) {
46 3
                $storage = static::createStorage($container);
0 ignored issues
show
Bug introduced by
Since createStorage() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of createStorage() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
47
            }
48
49 3
            return $storage;
50 3
        };
51
52 3
        $container[CsrfTokenGeneratorInterface::class] = $factory;
53 3
        $container[CsrfTokenStorageInterface::class]   = $factory;
54
    }
55
56
    /**
57
     * @param PsrContainerInterface $container
58
     *
59
     * @return CsrfTokenStorageInterface
60
     */
61 3
    private static function createStorage(PsrContainerInterface $container): CsrfTokenStorageInterface
62
    {
63
        /** @var SettingsProviderInterface $provider */
64 3
        assert($container->has(SettingsProviderInterface::class));
65 3
        $provider = $container->get(SettingsProviderInterface::class);
66 3
        assert($provider->has(C::class));
67
        [
68 3
            C::TOKEN_STORAGE_KEY_IN_SESSION => $sessionKey,
0 ignored issues
show
Bug introduced by
The variable $sessionKey does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
69 3
            C::MAX_TOKENS                   => $maxTokens,
0 ignored issues
show
Bug introduced by
The variable $maxTokens does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70 3
            C::MAX_TOKENS_THRESHOLD         => $maxTokensThreshold,
0 ignored issues
show
Bug introduced by
The variable $maxTokensThreshold does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
71
        ]
72 3
            = $provider->get(C::class);
73
74
        /** @var SessionInterface $session */
75 3
        assert($container->has(SessionInterface::class));
76 3
        $session = $container->get(SessionInterface::class);
77
78 3
        $storage = new CsrfTokenStorage($session, $sessionKey, $maxTokens, $maxTokensThreshold);
79
80 3
        return $storage;
81
    }
82
}
83