Passed
Push — main ( c90a44...178315 )
by Nobufumi
02:25
created

Dependencies::createPostModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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