GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( abe42f...878d0b )
by Cees-Jan
03:19
created

TestCase::createCommandBus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace ApiClients\Tools\ResourceTestUtilities;
5
6
use ApiClients\Foundation\Hydrator\Factory;
7
use ApiClients\Foundation\Hydrator\Options;
8
use ApiClients\Tools\CommandBus\CommandBus;
9
use ApiClients\Tools\TestUtilities\TestCase as BaseTestCase;
10
use DI\ContainerBuilder;
11
use League\Tactician\Handler\CommandHandlerMiddleware;
12
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor;
13
use League\Tactician\Handler\Locator\InMemoryLocator;
14
use League\Tactician\Handler\MethodNameInflector\HandleInflector;
15
use React\EventLoop\Factory as LoopFactory;
16
use React\EventLoop\LoopInterface;
17
18
abstract class TestCase extends BaseTestCase
19
{
20
    const DEFAULT_GENERATED_CLASS_NAMESPACE = 'GHGC_%s';
21
22
    /**
23
     * @var string
24
     */
25
    private $tmpDir;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    /**
28
     * @var string
29
     */
30
    private $tmpNamespace;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
31
32 6
    public function setUp()
33
    {
34 6
        parent::setUp();
35 6
        $crc32 = crc32(get_class($this));
36 6
        $this->tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('act-' . $crc32, true) . DIRECTORY_SEPARATOR;
37 6
        mkdir($this->tmpDir, 0777, true);
38 6
        $this->tmpNamespace = sprintf(
39 6
            static::DEFAULT_GENERATED_CLASS_NAMESPACE,
40 6
            crc32(uniqid((string)$crc32, true))
41
        );
42 6
    }
43
44 6
    public function tearDown()
45
    {
46 6
        parent::tearDown();
47 6
        $this->rmdir($this->tmpDir);
48 6
    }
49
50 6
    protected function rmdir($dir)
51
    {
52 6
        $directory = dir($dir);
53 6
        while (false !== ($entry = $directory->read())) {
54 6
            if (in_array($entry, ['.', '..'])) {
55 6
                continue;
56
            }
57
58 6
            if (is_dir($dir . $entry)) {
59 6
                $this->rmdir($dir . $entry . DIRECTORY_SEPARATOR);
60 6
                continue;
61
            }
62
63
            if (is_file($dir . $entry)) {
64
                unlink($dir . $entry);
65
                continue;
66
            }
67
        }
68 6
        $directory->close();
69 6
        rmdir($dir);
70 6
    }
71
72
    protected function getTmpDir(): string
73
    {
74
        return $this->tmpDir;
75
    }
76
77
    protected function getRandomNameSpace(): string
78
    {
79
        return $this->tmpNamespace;
80
    }
81
82
    public function hydrate($class, $json, $namespace)
83
    {
84
        $loop = LoopFactory::create();
85
        $container = ContainerBuilder::buildDevContainer();
86
        $container->set(CommandBus::class, $this->createCommandBus($loop));
87
        return Factory::create($container, [
88
            Options::NAMESPACE => '',
89
            Options::NAMESPACE_SUFFIX => $namespace,
90
            Options::RESOURCE_CACHE_DIR => $this->getTmpDir(),
91
            Options::RESOURCE_NAMESPACE => $this->getRandomNameSpace(),
92
        ])->hydrateFQCN($class, $json);
93
    }
94
95
    protected function createCommandBus(LoopInterface $loop, array $map = []): CommandBus
96
    {
97
        $commandHandlerMiddleware = new CommandHandlerMiddleware(
98
            new ClassNameExtractor(),
99
            new InMemoryLocator($map),
100
            new HandleInflector()
101
        );
102
103
        return new CommandBus(
104
            $loop,
105
            $commandHandlerMiddleware
106
        );
107
    }
108
}
109