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

registerControllerResultListeners()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1.048

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 7
cts 11
cp 0.6364
rs 9.4285
cc 1
eloc 9
nc 1
nop 1
crap 1.048
1
<?php
2
3
namespace Alchemy\RestProvider;
4
5
use Alchemy\Rest\Response\ArrayTransformer;
6
use Alchemy\RestBundle\EventListener;
7
use League\Fractal\Manager;
8
use Silex\Application;
9
use Silex\ServiceProviderInterface;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class TransformerServiceProvider implements ServiceProviderInterface
14
{
15
16
    private $subscriberKeys = [];
17
18 2
    public function register(Application $app)
19
    {
20 2
        $this->registerControllerResultListeners($app);
21 2
        $this->registerResponseListener($app);
22
23 2
        $this->registerRequestDecoder($app);
24 2
        $this->registerResponseEncoder($app);
25
26 2
        $app['dispatcher'] = $app->share(
27
            $app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) {
28
                $this->bindResultListeners($app, $dispatcher);
29
30
                return $dispatcher;
31 2
            })
32 2
        );
33 2
    }
34
35 2
    private function shareEventListener(Application $app, $key, callable $factory)
36
    {
37 2
        $app[$key] = $app->share($factory);
38
39 2
        $this->subscriberKeys[] = $key;
40 2
    }
41
42
    private function bindResultListeners(Application $app, EventDispatcherInterface $dispatcher)
43
    {
44
        foreach ($this->subscriberKeys as $subscriberKey) {
45
            $dispatcher->addSubscriber($app[$subscriberKey]);
46
        }
47
    }
48
49 2
    public function boot(Application $app)
50
    {
51
        // no-op
52 2
    }
53
54
    /**
55
     * @param Application $app
56
     * @return Application
57
     */
58 2
    private function registerRequestDecoder(Application $app)
59
    {
60 2
        $app['alchemy_rest.decode_request_content_types'] = array('application/json');
61
62
        $app['alchemy_rest.decode_request_listener'] = $app->share(function () use ($app) {
63
            return new EventListener\DecodeJsonBodyRequestListener($app['alchemy_rest.content_type_matcher']);
64 2
        });
65
66
        $app['alchemy_rest.request_decoder'] = $app->protect(function (Request $request) use ($app) {
67
            $app['alchemy_rest.decode_request_listener']->decodeBody($request);
68 2
        });
69 2
    }
70
71
    /**
72
     * @param Application $app
73
     * @return Application
74
     */
75 2
    private function registerResponseEncoder(Application $app)
76
    {
77
        $app['alchemy_rest.encode_response_listener'] = $app->share(function () use ($app) {
78
            return new EventListener\EncodeJsonResponseListener();
79 2
        });
80 2
    }
81
82
    /**
83
     * @param Application $app
84
     * @return Application
85
     */
86 2
    private function registerResponseListener(Application $app)
87
    {
88
        $app['alchemy_rest.fractal_manager'] = $app->share(function () {
89
            return new Manager();
90 2
        });
91
92
        $app['alchemy_rest.transformers_registry'] = $app->share(function () {
93
            return new \Pimple();
94 2
        });
95
96
        $app['alchemy_rest.array_transformer'] = $app->share(function () use ($app) {
97
            return new ArrayTransformer(
98
                $app['alchemy_rest.fractal_manager'],
99
                $app['alchemy_rest.transformers_registry']
100
            );
101 2
        });
102
103
        $app['alchemy_rest.transform_response_listener'] = $app->share(function () use ($app) {
104
            return new EventListener\TransformResponseListener(
105
                $app['alchemy_rest.array_transformer'],
106
                $app['url_generator']
107
            );
108 2
        });
109 2
    }
110
111
    /**
112
     * @param Application $app
113
     */
114 2
    private function registerControllerResultListeners(Application $app)
115
    {
116
        $this->shareEventListener($app, 'alchemy_rest.transform_success_result_listener', function () {
117
            return new EventListener\SuccessResultListener();
118 2
        });
119
120
        $this->shareEventListener($app, 'alchemy_rest.transform_bad_request_listener', function () {
121
            return new EventListener\BadRequestListener();
122 2
        });
123
124
        $this->shareEventListener($app, 'alchemy_rest.transform_request_accepted_listener', function () {
125
            return new EventListener\RequestAcceptedListener();
126 2
        });
127
128 2
        $this->shareEventListener($app, 'alchemy_rest.transform_resource_created_listener', function () use ($app) {
129
            return new EventListener\ResourceCreatedListener($app['alchemy_rest.array_transformer']);
130 2
        });
131 2
    }
132
}
133