Completed
Push — master ( f8c7ce...9048a9 )
by Thibaud
7s
created

RestProvider::registerContentTypeMatcher()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1.0233

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 5
cts 7
cp 0.7143
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
crap 1.0233
1
<?php
2
/*
3
 * This file is part of Phraseanet
4
 *
5
 * (c) 2005-2015 Alchemy
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Alchemy\RestProvider;
11
12
use Alchemy\Rest\Request\ContentTypeMatcher;
13
use Alchemy\Rest\Request\DateParser\FormatDateParser;
14
use Alchemy\Rest\Response\ExceptionTransformer\DefaultExceptionTransformer;
15
use Alchemy\RestBundle\EventListener;
16
use Alchemy\RestBundle\Rest\Request\PaginationOptionsFactory;
17
use Alchemy\RestBundle\Rest\Request\SortOptionsFactory;
18
use Alchemy\RestProvider\Middleware;
19
use Negotiation\Negotiator;
20
use Silex\Application;
21
use Silex\ServiceProviderInterface;
22
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
25
class RestProvider implements ServiceProviderInterface
26
{
27 2
    public function register(Application $app)
28
    {
29 2
        $app['alchemy_rest.debug'] = false;
30
31 2
        $this->registerContentTypeMatcher($app);
32 2
        $this->registerExceptionListener($app);
33 2
        $this->registerDateListener($app);
34 2
        $this->registerPaginationListener($app);
35 2
        $this->registerSortListener($app);
36
37 2
        $app->register(new TransformerServiceProvider());
38 2
        $app->register(new MiddlewareServiceProvider());
39
40 2
        $app['dispatcher'] = $app->share(
41
            $app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) {
42
                $this->bindRequestListeners($app, $dispatcher);
43
44
                // Bind exception
45
                $dispatcher->addSubscriber($app['alchemy_rest.exception_listener']);
46
                // This block must be called after all other result listeners
47
                $dispatcher->addSubscriber($app['alchemy_rest.transform_response_listener']);
48
                $dispatcher->addSubscriber($app['alchemy_rest.encode_response_listener']);
49
50
                return $dispatcher;
51 2
            })
52 2
        );
53 2
    }
54
55
    private function bindRequestListeners(Application $app, EventDispatcherInterface $dispatcher)
56
    {
57
        $dispatcher->addSubscriber($app['alchemy_rest.decode_request_listener']);
58
        $dispatcher->addSubscriber($app['alchemy_rest.paginate_request_listener']);
59
        $dispatcher->addSubscriber($app['alchemy_rest.sort_request_listener']);
60
        $dispatcher->addSubscriber($app['alchemy_rest.date_request_listener']);
61
    }
62
63 2
    public function boot(Application $app)
64
    {
65
        // Nothing to do.
66 2
    }
67
68 2
    private function registerPaginationListener(Application $app)
69
    {
70 2
        $app['alchemy_rest.paginate_options.offset_parameter'] = 'offset';
71 2
        $app['alchemy_rest.paginate_options.limit_parameter'] = 'limit';
72
        $app['alchemy_rest.paginate_options_factory'] = $app->share(function () use ($app) {
73
            return new PaginationOptionsFactory(
74
                $app['alchemy_rest.paginate_options.offset_parameter'],
75
                $app['alchemy_rest.paginate_options.limit_parameter']
76
            );
77 2
        });
78
79
        $app['alchemy_rest.paginate_request_listener'] = $app->share(function () use ($app) {
80
            return new EventListener\PaginationParamRequestListener($app['alchemy_rest.paginate_options_factory']);
81 2
        });
82 2
    }
83
84 2
    private function registerSortListener(Application $app)
85
    {
86 2
        $app['alchemy_rest.sort_options.sort_parameter'] = 'sort';
87 2
        $app['alchemy_rest.sort_options.direction_parameter'] = 'dir';
88 2
        $app['alchemy_rest.sort_options.multi_sort_parameter'] = 'sorts';
89
        $app['alchemy_rest.sort_options_factory'] = $app->share(function () use ($app) {
90
            return new SortOptionsFactory(
91
                $app['alchemy_rest.sort_options.sort_parameter'],
92
                $app['alchemy_rest.sort_options.direction_parameter'],
93
                $app['alchemy_rest.sort_options.multi_sort_parameter']
94
            );
95 2
        });
96
97
        $app['alchemy_rest.sort_request_listener'] = $app->share(function () use ($app) {
98
            return new EventListener\SortParamRequestListener($app['alchemy_rest.sort_options_factory']);
99 2
        });
100 2
    }
101
102 2
    private function registerDateListener(Application $app)
103
    {
104 2
        $app['alchemy_rest.date_parser.timezone'] = 'UTC';
105 2
        $app['alchemy_rest.date_parser.format'] = 'Y-m-d H:i:s';
106
        $app['alchemy_rest.date_parser'] = $app->share(function () use ($app) {
107
            return new FormatDateParser(
108
                $app['alchemy_rest.date_parser.timezone'],
109
                $app['alchemy_rest.date_parser.format']
110
            );
111 2
        });
112
113
        $app['alchemy_rest.date_request_listener'] = $app->share(function () use ($app) {
114
            return new EventListener\DateParamRequestListener($app['alchemy_rest.date_parser']);
115 2
        });
116 2
    }
117
118 2
    private function registerExceptionListener(Application $app)
119
    {
120
        $app['alchemy_rest.exception_transformer'] = $app->share(function () use ($app) {
121
            return new DefaultExceptionTransformer($app['alchemy_rest.debug']);
122 2
        });
123
124 2
        $app['alchemy_rest.exception_handling_content_types'] = array('application/json');
125
        $app['alchemy_rest.exception_listener'] = $app->share(function () use ($app) {
126
            return new EventListener\ExceptionListener(
127
                $app['alchemy_rest.content_type_matcher'],
128
                $app['alchemy_rest.exception_transformer'],
129
                $app['alchemy_rest.exception_handling_content_types']
130
            );
131 2
        });
132 2
    }
133
134 2
    private function registerContentTypeMatcher(Application $app)
135
    {
136
        $app['alchemy_rest.negotiator'] = $app->share(function () use ($app) {
137
            return new Negotiator();
138 2
        });
139 2
        $app['alchemy_rest.content_type_matcher'] = $app->share(function () use ($app) {
140
            return new ContentTypeMatcher($app['alchemy_rest.negotiator']);
141 2
        });
142 2
    }
143
}
144