Completed
Push — master ( b90886...70bc0c )
by Thibaud
02:46
created

RestProvider::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 15
Bugs 0 Features 0
Metric Value
c 15
b 0
f 0
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 10
nc 1
nop 1
crap 1
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
        $this->registerEventSubscribers($app);
41 2
    }
42
43
    private function bindRequestListeners(Application $app, EventDispatcherInterface $dispatcher)
44
    {
45
        $dispatcher->addSubscriber($app['alchemy_rest.decode_request_listener']);
46
        $dispatcher->addSubscriber($app['alchemy_rest.paginate_request_listener']);
47
        $dispatcher->addSubscriber($app['alchemy_rest.sort_request_listener']);
48
        $dispatcher->addSubscriber($app['alchemy_rest.date_request_listener']);
49
    }
50
51 2
    public function boot(Application $app)
52
    {
53
        // Nothing to do.
54 2
    }
55
56 2
    private function registerPaginationListener(Application $app)
57
    {
58 2
        $app['alchemy_rest.paginate_options.offset_parameter'] = 'offset';
59 2
        $app['alchemy_rest.paginate_options.limit_parameter'] = 'limit';
60
        $app['alchemy_rest.paginate_options_factory'] = $app->share(function () use ($app) {
61
            return new PaginationOptionsFactory(
62
                $app['alchemy_rest.paginate_options.offset_parameter'],
63
                $app['alchemy_rest.paginate_options.limit_parameter']
64
            );
65 2
        });
66
67
        $app['alchemy_rest.paginate_request_listener'] = $app->share(function () use ($app) {
68
            return new EventListener\PaginationParamRequestListener($app['alchemy_rest.paginate_options_factory']);
69 2
        });
70 2
    }
71
72 2
    private function registerSortListener(Application $app)
73
    {
74 2
        $app['alchemy_rest.sort_options.sort_parameter'] = 'sort';
75 2
        $app['alchemy_rest.sort_options.direction_parameter'] = 'dir';
76 2
        $app['alchemy_rest.sort_options.multi_sort_parameter'] = 'sorts';
77
        $app['alchemy_rest.sort_options_factory'] = $app->share(function () use ($app) {
78
            return new SortOptionsFactory(
79
                $app['alchemy_rest.sort_options.sort_parameter'],
80
                $app['alchemy_rest.sort_options.direction_parameter'],
81
                $app['alchemy_rest.sort_options.multi_sort_parameter']
82
            );
83 2
        });
84
85
        $app['alchemy_rest.sort_request_listener'] = $app->share(function () use ($app) {
86
            return new EventListener\SortParamRequestListener($app['alchemy_rest.sort_options_factory']);
87 2
        });
88 2
    }
89
90 2
    private function registerDateListener(Application $app)
91
    {
92 2
        $app['alchemy_rest.date_parser.timezone'] = 'UTC';
93 2
        $app['alchemy_rest.date_parser.format'] = 'Y-m-d H:i:s';
94
        $app['alchemy_rest.date_parser'] = $app->share(function () use ($app) {
95
            return new FormatDateParser(
96
                $app['alchemy_rest.date_parser.timezone'],
97
                $app['alchemy_rest.date_parser.format']
98
            );
99 2
        });
100
101
        $app['alchemy_rest.date_request_listener'] = $app->share(function () use ($app) {
102
            return new EventListener\DateParamRequestListener($app['alchemy_rest.date_parser']);
103 2
        });
104 2
    }
105
106 2
    private function registerExceptionListener(Application $app)
107
    {
108
        $app['alchemy_rest.exception_transformer'] = $app->share(function () use ($app) {
109
            return new DefaultExceptionTransformer($app['alchemy_rest.debug']);
110 2
        });
111
112 2
        $app['alchemy_rest.exception_handling_content_types'] = array('application/json');
113
        $app['alchemy_rest.exception_listener'] = $app->share(function () use ($app) {
114
            return new EventListener\ExceptionListener(
115
                $app['alchemy_rest.content_type_matcher'],
116
                $app['alchemy_rest.exception_transformer'],
117
                $app['alchemy_rest.exception_handling_content_types']
118
            );
119 2
        });
120 2
    }
121
122 2
    private function registerContentTypeMatcher(Application $app)
123
    {
124
        $app['alchemy_rest.negotiator'] = $app->share(function () use ($app) {
125
            return new Negotiator();
126 2
        });
127
        $app['alchemy_rest.content_type_matcher'] = $app->share(function () use ($app) {
128
            return new ContentTypeMatcher($app['alchemy_rest.negotiator']);
129 2
        });
130 2
    }
131
132
    /**
133
     * @param Application $app
134
     */
135 2
    private function registerEventSubscribers(Application $app)
136
    {
137 2
        $app['dispatcher'] = $app->share(
138 2
            $app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) {
139
                $this->bindRequestListeners($app, $dispatcher);
140
141
                // Bind exception
142
                $dispatcher->addSubscriber($app['alchemy_rest.exception_listener']);
143
                // This block must be called after all other result listeners
144
                $dispatcher->addSubscriber($app['alchemy_rest.transform_response_listener']);
145
                $dispatcher->addSubscriber($app['alchemy_rest.encode_response_listener']);
146
147
                return $dispatcher;
148 2
            })
149 2
        );
150 2
    }
151
}
152