Completed
Push — master ( e327bd...2b2b50 )
by Nikolas
03:41
created

IndexResource::resetBreadCrumbs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace rtens\domin\delivery\web\root;
3
4
use rtens\domin\delivery\web\BreadCrumbs;
5
use rtens\domin\delivery\web\HeadElements;
6
use rtens\domin\delivery\web\WebApplication;
7
use watoki\curir\Container;
8
use watoki\curir\cookie\CookieStore;
9
use watoki\curir\delivery\WebRequest;
10
use watoki\curir\protocol\Url;
11
use watoki\curir\rendering\PhpRenderer;
12
use watoki\deli\Path;
13
use watoki\deli\Request;
14
use watoki\factory\Factory;
15
16
class IndexResource extends Container {
17
18
    /** @var CookieStore */
19
    private $cookies;
20
21
    /** @var WebApplication  */
22
    private $app;
23
24
    /**
25
     * @param Factory $factory <-
26
     * @param WebApplication $app <-
27
     * @param CookieStore $cookies <-
28
     */
29
    public function __construct(Factory $factory, WebApplication $app, CookieStore $cookies) {
30
        parent::__construct($factory);
31
        $this->cookies = $cookies;
32
        $this->app = $app;
33
    }
34
35
    /**
36
     * @param Request|WebRequest $request
37
     * @return \watoki\curir\delivery\WebResponse
38
     */
39
    public function respond(Request $request) {
40
        $this->app->prepare($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<watoki\deli\Request> is not a sub-type of object<watoki\curir\delivery\WebRequest>. It seems like you assume a child class of the class watoki\deli\Request to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
41
42
        if (!$this->isContainerTarget($request)) {
43
            $request = $request
44
                ->withTarget(Path::fromString('execute'))
45
                ->withArgument(ExecuteResource::ACTION_ARG, $request->getTarget()->toString());
46
        }
47
        return parent::respond($request);
48
    }
49
50
    /**
51
     * @param WebRequest $request <-
52
     * @return array
53
     */
54
    public function doGet(WebRequest $request) {
55
        (new BreadCrumbs($this->cookies, $request))->reset();
56
57
        return [
58
            'name' => $this->app->name,
59
            'menu' => $this->app->menu->render($request),
60
            'action' => $this->assembleActions($request->getContext()),
61
            'headElements' => [
62
                (string)HeadElements::jquery(),
63
                (string)HeadElements::bootstrap(),
64
                (string)HeadElements::bootstrapJs(),
65
            ]
66
        ];
67
    }
68
69
    /**
70
     * @param Url $base
71
     * @return array
72
     */
73
    private function assembleActions(Url $base) {
74
        $actions = [];
75
        foreach ($this->app->actions->getAllActions() as $id => $action) {
76
            $actions[] = [
77
                'caption' => $action->caption(),
78
                'description' => $this->app->parser->shorten($action->description()),
79
                'link' => ['href' => $base->appended($id)->toString()]
80
            ];
81
        }
82
        return $actions;
83
    }
84
85
    protected function createDefaultRenderer() {
86
        return new PhpRenderer();
87
    }
88
}