Passed
Pull Request — master (#2)
by ANTHONIUS
05:01
created

EOffice::createApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the EOffice project.
5
 *
6
 * (c) Anthonius Munthi <https://itstoni.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace EOffice\Core;
15
16
use EOffice\Core\Exceptions\CoreException;
17
use EOffice\Core\Exceptions\Handler;
18
use Illuminate\Contracts\Console\Kernel;
19
use Illuminate\Contracts\Debug\ExceptionHandler;
20
use Laravel\Lumen\Bootstrap\LoadEnvironmentVariables;
21
use Symfony\Component\HttpKernel\HttpKernelInterface;
22
23
/**
24
 * EOffice Bootstrapper.
25
 *
26
 * @codeCoverageIgnore
27
 * @psalm-suppress MissingConstructor
28
 * @psalm-suppress PossiblyUndefinedMethod
29
 */
30
class EOffice
31
{
32
    /**
33
     * @var HttpKernelInterface|Application
34
     */
35
    private $app;
36
37
    /**
38
     * @throws CoreException
39
     *
40
     * @return $this
41
     */
42
    public function bootstrap(): self
43
    {
44
        /** @psalm-param Application $app */
45
        $app = $this->createApplication();
46
47
        $app->withFacades();
48
        $app->singleton(ExceptionHandler::class, Handler::class);
49
        $app->singleton(Kernel::class, Console\Kernel::class);
50
        $app->configure('app');
51
52
        $this->app = $app;
53
54
        $this->registerProviders();
55
56
        return $this;
57
    }
58
59
    /**
60
     * @return Application|HttpKernelInterface
61
     */
62
    public function getApplication()
63
    {
64
        return $this->app;
65
    }
66
67
    /**
68
     * Create new application.
69
     *
70
     * @throws CoreException
71
     *
72
     * @return Application|HttpKernelInterface
73
     */
74
    private function createApplication()
75
    {
76
        $basePath = $this->detectPath();
77
        (new LoadEnvironmentVariables($basePath))->bootstrap();
78
        date_default_timezone_set((string) env('APP_TIMEZONE', 'UTC'));
79
80
        return new Application($basePath);
81
    }
82
83
    /**
84
     * Detect app path.
85
     *
86
     * @throws CoreException
87
     *
88
     * @return string
89
     */
90
    private function detectPath(): string
91
    {
92
        if(is_dir($dir = __DIR__.'/../../vendor')){
93
            return realpath(dirname($dir));
94
        }
95
96
        if (is_dir($dir = __DIR__.'/../../../vendor')) {
97
            return realpath(\dirname($dir));
98
        }
99
100
        throw CoreException::undetectedBasePathDir();
101
    }
102
103
    private function registerProviders(): void
104
    {
105
        $app = $this->app;
106
        $app->register(Providers\AuthServiceProvider::class);
0 ignored issues
show
Bug introduced by
The method register() does not exist on Symfony\Component\HttpKernel\HttpKernelInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $app->/** @scrutinizer ignore-call */ 
107
              register(Providers\AuthServiceProvider::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        $app->register(Providers\EventServiceProvider::class);
108
        $app->register(Providers\RouteServiceProvider::class);
109
        $app->register(Providers\EOfficeServiceProvider::class);
110
    }
111
}
112