Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

TestLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\Security;
12
13
use Psr\Cache\CacheItemPoolInterface;
14
use Yarhon\RouteGuardBundle\Cache\CacheFactory;
15
use Yarhon\RouteGuardBundle\Security\Test\AbstractTestBagInterface;
16
use Yarhon\RouteGuardBundle\Security\TestBagResolver\TestBagResolverInterface;
17
use Yarhon\RouteGuardBundle\Routing\RouteContextInterface;
18
19
/**
20
 * @author Yaroslav Honcharuk <[email protected]>
21
 */
22
class TestLoader implements TestLoaderInterface
23
{
24
    /**
25
     * @var CacheItemPoolInterface
26
     */
27
    private $testsCache;
28
29
    /**
30
     * @var TestBagResolverInterface
31
     */
32
    private $testBagResolver;
33
34
    /**
35
     * @param CacheItemPoolInterface   $testsCache
36
     * @param TestBagResolverInterface $testBagResolver
37
     */
38 8
    public function __construct(CacheItemPoolInterface $testsCache, TestBagResolverInterface $testBagResolver)
39
    {
40 8
        $this->testsCache = $testsCache;
41 8
        $this->testBagResolver = $testBagResolver;
42 8
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function load(RouteContextInterface $routeContext)
48
    {
49 8
        $cacheKey = CacheFactory::getValidCacheKey($routeContext->getName());
50 8
        $cacheItem = $this->testsCache->getItem($cacheKey);
51
52 8
        if (!$cacheItem->isHit()) {
53 1
            return [];
54
        }
55
56 7
        $testBags = $cacheItem->get();
57
58 7
        $tests = [];
59 7
        foreach ($testBags as $testBag) {
60
            /** @var AbstractTestBagInterface $testBag */
61 5
            $providerClass = $testBag->getProviderClass();
62 5
            $providerTests = $this->testBagResolver->resolve($testBag, $routeContext);
63 5
            foreach ($providerTests as $test) {
64 5
                $test->setProviderClass($providerClass);
65 5
                $tests[] = $test;
66
            }
67
        }
68
69 7
        return $tests;
70
    }
71
}
72