EntryPoint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 54
c 0
b 0
f 0
wmc 4
lcom 0
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A enter() 0 12 2
A createContainer() 0 12 1
A createApplication() 0 7 1
1
<?php
2
3
namespace Koriit\PHPDeps\Application;
4
5
use DI\Container;
6
use DI\ContainerBuilder;
7
use DI\DependencyException;
8
use DI\NotFoundException;
9
use Exception;
10
use Psr\Container\ContainerInterface;
11
use RuntimeException;
12
13
class EntryPoint
14
{
15
    /**
16
     * @param string   $appClass
17
     * @param callable $dependenciesDefFactory
18
     */
19
    public function enter($appClass, callable $dependenciesDefFactory)
20
    {
21
        $container = $this->createContainer($dependenciesDefFactory());
22
23
        try {
24
            $application = $this->createApplication($container, $appClass);
25
        } catch (Exception $e) {
26
            throw new RuntimeException('Could not create application: ' . $appClass, 0, $e);
27
        }
28
29
        $application->run();
30
    }
31
32
    /**
33
     * @param array $definitionSources
34
     *
35
     * @return Container
36
     */
37
    protected function createContainer(array $definitionSources)
38
    {
39
        $containerBuilder = new ContainerBuilder();
40
        $containerBuilder->useAnnotations(false);
41
        $containerBuilder->useAutowiring(true);
42
        $containerBuilder->addDefinitions($definitionSources);
43
44
        $container = $containerBuilder->build();
45
        $container->set(ContainerInterface::class, $container);
46
47
        return $container;
48
    }
49
50
    /**
51
     * @param Container $container
52
     * @param string    $appClass
53
     *
54
     * @throws DependencyException
55
     * @throws NotFoundException
56
     *
57
     * @return ApplicationInterface
58
     */
59
    protected function createApplication(Container $container, $appClass)
60
    {
61
        $application = $container->get($appClass);
62
        $container->set(ApplicationInterface::class, $application);
63
64
        return $application;
65
    }
66
}
67