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

JsonPayloadsBootloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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