Passed
Push — main ( 1a70e4...e654d0 )
by Thierry
01:53
created

start.php$1 ➔ onChanges()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Jaxon\Upload;
4
5
use Jaxon\App\Config\ConfigEventManager;
6
use Jaxon\App\Config\ConfigListenerInterface;
7
use Jaxon\App\Config\ConfigManager;
8
use Jaxon\App\I18n\Translator;
9
use Jaxon\Di\Container;
10
use Jaxon\Request\Upload\UploadHandlerInterface;
0 ignored issues
show
Bug introduced by
The type Jaxon\Request\Upload\UploadHandlerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Jaxon\Response\Manager\ResponseManager;
12
use Jaxon\Upload\Manager\FileNameInterface;
13
use Jaxon\Upload\Manager\FileStorage;
14
use Jaxon\Upload\Manager\UploadManager;
15
use Jaxon\Upload\Manager\Validator;
16
use Jaxon\Utils\Config\Config;
17
use Nyholm\Psr7\Factory\Psr17Factory;
18
19
use function bin2hex;
20
use function random_bytes;
21
22
/**
23
 * @param Container $di
24
 * @param bool $bForce Force registration
25
 *
26
 * @return void
27
 */
28
function register(Container $di, bool $bForce = false)
29
{
30
    if(!$bForce && $di->h(UploadHandler::class))
31
    {
32
        return;
33
    }
34
35
    // Upload file and dir name generator
36
    $di->set(FileNameInterface::class, function() {
37
        return new class implements FileNameInterface
38
        {
39
            public function random(int $nLength): string
40
            {
41
                return bin2hex(random_bytes((int)($nLength / 2)));
42
            }
43
        };
44
    });
45
    // Upload validator
46
    $di->set(Validator::class, function($c) {
47
        return new Validator($c->g(ConfigManager::class), $c->g(Translator::class));
48
    });
49
    // File storage
50
    $di->set(FileStorage::class, function($c) {
51
        return new FileStorage($c->g(ConfigManager::class), $c->g(Translator::class));
52
    });
53
    // File upload manager
54
    $di->set(UploadManager::class, function($c) {
55
        return new UploadManager($c->g(FileNameInterface::class), $c->g(ConfigManager::class),
56
            $c->g(Validator::class), $c->g(Translator::class), $c->g(FileStorage::class));
57
    });
58
    // File upload plugin
59
    $di->set(UploadHandler::class, function($c) {
60
        return new UploadHandler($c->g(UploadManager::class), $c->g(ResponseManager::class),
61
            $c->g(Translator::class), $c->g(Psr17Factory::class));
62
    });
63
    // Set alias on the interface
64
    $di->alias(UploadHandlerInterface::class, UploadHandler::class);
65
}
66
67
/**
68
 * Register the values into the container
69
 *
70
 * @return void
71
 */
72
function registerUpload()
73
{
74
    $di = jaxon()->di();
75
    $sEventListenerKey = UploadHandler::class . '\\ConfigListener';
76
    if($di->h($sEventListenerKey))
77
    {
78
        return;
79
    }
80
81
    // The annotation package is installed, register the real annotation reader,
82
    // but only if the feature is activated in the config.
83
    $di->set($sEventListenerKey, function() {
84
        return new class implements ConfigListenerInterface
85
        {
86
            public function onChange(Config $xConfig, string $sName)
87
            {
88
                $sConfigKey = 'core.upload.enabled';
89
                if(($sName === $sConfigKey || $sName === '') && $xConfig->getOption($sConfigKey))
90
                {
91
                    register(jaxon()->di());
92
                }
93
            }
94
        };
95
    });
96
97
    // Register the event listener
98
    $xEventManager = $di->g(ConfigEventManager::class);
99
    $xEventManager->addListener($sEventListenerKey);
100
}
101
102
// Initialize the upload handler
103
registerUpload();
104