Completed
Push — master ( 312098...b95ba9 )
by Nikolas
40:06
created

SilexControllerProvider::connect()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 8.8571
cc 1
eloc 16
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\ActionListResource;
7
use rtens\domin\delivery\web\resources\ExecutionResource;
8
use rtens\domin\delivery\web\WebApplication;
9
use Silex\Application;
10
use Silex\ControllerCollection;
11
use Silex\ControllerProviderInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use watoki\factory\Factory;
15
16
class SilexControllerProvider implements ControllerProviderInterface {
17
18
    /** @var WebApplication */
19
    private $domin;
20
21
    public function __construct(Factory $factory) {
22
        $this->domin = $factory->getInstance(WebApplication::class);
23
        $this->domin->prepare();
24
    }
25
26
    /**
27
     * Returns routes to connect to the given application.
28
     *
29
     * @param Application $app An Application instance
30
     *
31
     * @return ControllerCollection A ControllerCollection instance
32
     */
33
    public function connect(Application $app) {
34
        /** @var ControllerCollection $controller */
35
        $controller = $app['controllers_factory'];
36
37
        $controller->get('/', function (Request $request) {
38
            return $this->respond($request, function (BreadCrumbsTrail $crumbs) {
39
                $actionList = new ActionListResource($this->domin, $crumbs);
40
                return $actionList->handleGet();
41
            });
42
        });
43
        $controller->get('/{action}', function ($action, Request $request) {
44
            return $this->respond($request, function (BreadCrumbsTrail $crumbs, ParameterReader $reader) use ($action, $request) {
45
                $execution = new ExecutionResource($this->domin, $reader, $crumbs);
46
                $force = !!$request->get(ExecutionResource::FORCE_ARG, false);
47
                return $execution->handleGet($action, $force);
48
            });
49
        });
50
        $controller->post('/{action}', function ($action, Request $request) {
51
            return $this->respond($request, function (BreadCrumbsTrail $crumbs, ParameterReader $reader) use ($action, $request) {
52
                $execution = new ExecutionResource($this->domin, $reader, $crumbs);
53
                return $execution->handlePost($action);
54
            });
55
        });
56
57
        return $controller;
58
    }
59
60
    private function respond(Request $request, callable $content) {
61
        $reader = new SilexParameterReader($request);
62
        $crumbs = new SilexBreadCrumbsTrail($reader, $request);
63
        return $this->createResponse(call_user_func($content, $crumbs, $reader), $crumbs);
64
    }
65
66
    private function createResponse($content, SilexBreadCrumbsTrail $crumbs) {
67
        $response = Response::create($content);
68
        $response->headers->setCookie($crumbs->getCookie());
69
        return $response;
70
    }
71
}