Dependencies   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
dl 0
loc 96
rs 10
c 2
b 0
f 0
wmc 14

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createRoutesService() 0 3 1
A createPhpRenderer() 0 3 1
A createValidationService() 0 4 1
A createFileService() 0 6 1
A createSession() 0 11 4
A createUserModel() 0 5 1
A __construct() 0 2 1
A createPostModel() 0 7 1
A createAuth() 0 5 1
A register() 0 16 1
A createDatabase() 0 8 1
1
<?php
2
3
namespace Jidaikobo\Kontiki\Config;
4
5
use Aura\Session\SessionFactory;
6
use Aura\Session\Session;
7
use DI\Container;
8
use Slim\App;
9
use Slim\Routing\RouteParser;
10
use Slim\Views\PhpRenderer;
11
use Valitron\Validator;
12
use Jidaikobo\Kontiki\Services\FileService;
13
use Jidaikobo\Kontiki\Services\RoutesService;
14
use Jidaikobo\Kontiki\Services\ValidationService;
15
use Jidaikobo\Kontiki\Core\Auth;
16
use Jidaikobo\Kontiki\Core\Database;
17
use Jidaikobo\Kontiki\Models\PostModel;
18
use Jidaikobo\Kontiki\Models\UserModel;
19
20
class Dependencies
21
{
22
    public function __construct(private App $app)
23
    {
24
    }
25
26
    public function register(): void
27
    {
28
        /** @var Container $container */
29
        $container = $this->app->getContainer();
30
31
        $container->set(App::class, $this->app);
32
        $container->set(Database::class, fn() => $this->createDatabase());
33
        $container->set(Session::class, fn() => $this->createSession());
34
        $container->set(PostModel::class, fn($c) => $this->createPostModel($c));
35
        $container->set(UserModel::class, fn($c) => $this->createUserModel($c));
36
        $container->set(Auth::class, fn($c) => $this->createAuth($c));
37
        $container->set(ValidationService::class, fn($c) => $this->createValidationService($c));
38
        $container->set(PhpRenderer::class, fn() => $this->createPhpRenderer());
39
        $container->set(FileService::class, fn() => $this->createFileService());
40
        $container->set(RoutesService::class, fn() => $this->createRoutesService());
41
        $container->set(RouteParser::class, fn() => $this->app->getRouteCollector()->getRouteParser());
42
    }
43
44
    private function createDatabase(): Database
45
    {
46
        return new Database([
47
            'driver' => 'sqlite',
48
            'database' => env('PROJECT_PATH', '') . '/' . env('DB_DATABASE', ''),
49
            'charset' => 'utf8',
50
            'collation' => 'utf8_unicode_ci',
51
            'prefix' => '',
52
        ]);
53
    }
54
55
    private function createSession(): Session
56
    {
57
        $uri = $_SERVER['REQUEST_URI'] ?? '';
58
        if (
59
            str_contains($uri, '.js') ||
60
            str_contains($uri, '.css') ||
61
            str_contains($uri, '.ico')
62
        ) {
63
            session_cache_limiter('private_no_expire');
64
        }
65
        return (new SessionFactory())->newInstance($_COOKIE);
66
    }
67
68
    private function createPostModel(Container $c): PostModel
69
    {
70
        return new PostModel(
71
            $c->get(Database::class),
72
            $c->get(ValidationService::class),
73
            $c->get(Auth::class),
74
            $c->get(UserModel::class)
75
        );
76
    }
77
78
    private function createUserModel(Container $c): UserModel
79
    {
80
        return new UserModel(
81
            $c->get(Database::class),
82
            $c->get(ValidationService::class)
83
        );
84
    }
85
86
    private function createAuth(Container $c): Auth
87
    {
88
        return new Auth(
89
            $c->get(Session::class),
90
            $c->get(UserModel::class)
91
        );
92
    }
93
94
    private function createValidationService(Container $c): ValidationService
95
    {
96
        $validator = new Validator([], [], env('APPLANG', 'en'));
97
        return new ValidationService($c->get(Database::class), $validator);
98
    }
99
100
    private function createPhpRenderer(): PhpRenderer
101
    {
102
        return new PhpRenderer(__DIR__ . '/../../src/views');
103
    }
104
105
    private function createFileService(): FileService
106
    {
107
        $uploadDir = env('PROJECT_PATH', '') . env('UPLOADDIR', '');
108
        $allowedTypes = json_decode(env('ALLOWED_MIME_TYPES', '[]'), true);
109
        $maxSize = env('MAXSIZE', 5000000);
110
        return new FileService($uploadDir, $allowedTypes, $maxSize);
111
    }
112
113
    private function createRoutesService(): RoutesService
114
    {
115
        return new RoutesService($this->app->getRouteCollector());
116
    }
117
}
118