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

SilexBreadCrumbsTrail::getCookie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
namespace rtens\domin\delivery\web\adapters\silex;
3
4
use rtens\domin\delivery\ParameterReader;
5
use rtens\domin\delivery\web\BreadCrumb;
6
use rtens\domin\delivery\web\BreadCrumbsTrail;
7
use Symfony\Component\HttpFoundation\Cookie;
8
use Symfony\Component\HttpFoundation\ParameterBag;
9
use Symfony\Component\HttpFoundation\Request;
10
11
class SilexBreadCrumbsTrail extends BreadCrumbsTrail {
12
13
    const COOKIE_NAME = 'domin_breadcrumbs';
14
15
    /**
16
     * @param ParameterReader $reader
17
     * @param Request $request
18
     */
19
    public function __construct($reader, $request) {
20
        parent::__construct($reader, $this->readCrumbs($request->cookies));
21
    }
22
23
    /**
24
     * @param ParameterBag $cookies
25
     * @return BreadCrumb[]
26
     */
27
    private function readCrumbs(ParameterBag $cookies) {
28
        if (!$cookies->has(self::COOKIE_NAME)) {
29
            return [];
30
        }
31
32
        $serialized = json_decode($cookies->get(self::COOKIE_NAME), true);
33
        return array_map(function ($item) {
34
            return new BreadCrumb($item['caption'], $item['target']);
35
        }, $serialized);
36
    }
37
38 View Code Duplication
    public function getCookie() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
39
        return new Cookie(self::COOKIE_NAME, json_encode(array_map(function (BreadCrumb $crumb) {
40
            return [
41
                'target' => $crumb->getTarget(),
42
                'caption' => $crumb->getCaption()
43
            ];
44
        }, $this->getCrumbs())));
45
    }
46
}