Completed
Push — master ( d98c10...e4052f )
by Nikolas
04:37
created

SilexControllerProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace rtens\domin\delivery\web\adapters\silex;
3
4
use rtens\domin\delivery\ParameterReader;
5
use rtens\domin\delivery\web\BreadCrumbsTrail;
6
use rtens\domin\delivery\web\resources\ExecutionResource;
7
use rtens\domin\delivery\web\WebApplication;
8
use Silex\Application;
9
use Silex\ControllerCollection;
10
use Silex\ControllerProviderInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use watoki\factory\Factory;
14
15
class SilexControllerProvider implements ControllerProviderInterface {
16
17
    /** @var WebApplication */
18
    private $domin;
19
20
    public function __construct(Factory $factory) {
21
        $this->domin = $factory->getInstance(WebApplication::class);
22
    }
23
24
    /**
25
     * Returns routes to connect to the given application.
26
     *
27
     * @param Application $app An Application instance
28
     *
29
     * @return ControllerCollection A ControllerCollection instance
30
     */
31
    public function connect(Application $app) {
32
        /** @var ControllerCollection $controller */
33
        $controller = $app['controllers_factory'];
34
35 View Code Duplication
        $controller->get('/{action?}', function (Request $request, $action = null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            return $this->respond($request, function (BreadCrumbsTrail $crumbs, ParameterReader $reader) use ($action, $request) {
37
                $execution = new ExecutionResource($this->domin, $reader, $crumbs);
38
                return $execution->handleGet($action, $request->get(ExecutionResource::TOKEN_ARG));
39
            });
40
        });
41 View Code Duplication
        $controller->post('/{action}', function (Request $request, $action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
            return $this->respond($request, function (BreadCrumbsTrail $crumbs, ParameterReader $reader) use ($action, $request) {
43
                $execution = new ExecutionResource($this->domin, $reader, $crumbs);
44
                return $execution->handlePost($action, $request->get(ExecutionResource::TOKEN_ARG));
45
            });
46
        });
47
48
        return $controller;
49
    }
50
51
    private function respond(Request $request, callable $content) {
52
        $reader = new SilexParameterReader($request);
53
        $crumbs = new SilexBreadCrumbsTrail($reader, $request);
54
        return $this->createResponse(call_user_func($content, $crumbs, $reader), $crumbs);
55
    }
56
57
    private function createResponse($content, SilexBreadCrumbsTrail $crumbs) {
58
        $response = Response::create($content);
59
        $response->headers->setCookie($crumbs->getCookie());
60
        return $response;
61
    }
62
}