Passed
Push — master ( 4d49b1...530588 )
by Valentin
03:01 queued 11s
created

JsonPayloadsBootloader   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 11
c 1
b 0
f 1
dl 0
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 12 1
A addContentType() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader\Http;
13
14
use Spiral\Boot\Bootloader\Bootloader;
15
use Spiral\Config\ConfiguratorInterface;
16
use Spiral\Config\JsonPayloadConfig;
17
use Spiral\Config\Patch\Append;
18
use Spiral\Http\Middleware\JsonPayloadMiddleware;
19
20
final class JsonPayloadsBootloader extends Bootloader
21
{
22
    /** @var array */
23
    protected const DEPENDENCIES = [
24
        HttpBootloader::class
25
    ];
26
27
    /** @var ConfiguratorInterface */
28
    private $config;
29
30
    /**
31
     * JsonPayloadsBootloader constructor.
32
     * @param ConfiguratorInterface $config
33
     */
34
    public function __construct(ConfiguratorInterface $config)
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * @param HttpBootloader $http
41
     */
42
    public function boot(HttpBootloader $http): void
43
    {
44
        $this->config->setDefaults(
45
            JsonPayloadConfig::CONFIG,
46
            [
47
                'contentTypes' => [
48
                    'application/json'
49
                ]
50
            ]
51
        );
52
53
        $http->addMiddleware(JsonPayloadMiddleware::class);
54
    }
55
56
    /**
57
     * Add custom MIME type to be parsed as JSON.
58
     *
59
     * @param string $contentType
60
     */
61
    public function addContentType(string $contentType): void
62
    {
63
        $this->config->modify(JsonPayloadConfig::CONFIG, new Append('contentTypes', null, $contentType));
64
    }
65
}
66