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

CurirBreadCrumbsTrail::updateCrumbs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
namespace rtens\domin\delivery\web\adapters\curir;
3
4
use rtens\domin\Action;
5
use rtens\domin\delivery\ParameterReader;
6
use rtens\domin\delivery\web\BreadCrumb;
7
use rtens\domin\delivery\web\BreadCrumbsTrail;
8
use watoki\curir\cookie\Cookie;
9
use watoki\curir\cookie\CookieStore;
10
11
class CurirBreadCrumbsTrail extends BreadCrumbsTrail {
12
13
    const COOKIE_KEY = 'domin_trail';
14
15
    /** @var CookieStore */
16
    private $cookies;
17
18
    public function __construct(ParameterReader $reader, CookieStore $cookies) {
19
        parent::__construct($reader, $this->readCrumbs($cookies));
20
        $this->cookies = $cookies;
21
    }
22
23
    public function updateCrumbs(Action $action, $actionId) {
24
        return $this->saveCrumbs(parent::updateCrumbs($action, $actionId));
25
    }
26
27
    /**
28
     * @param CookieStore $cookies
29
     * @return BreadCrumb[]
30
     */
31
    private function readCrumbs(CookieStore $cookies) {
32
        if ($cookies->hasKey(self::COOKIE_KEY)) {
33
            return array_map(function ($array) {
34
                return new BreadCrumb($array['caption'], $array['target']);
35
            }, $cookies->read(self::COOKIE_KEY)->payload);
36
        }
37
        return [];
38
    }
39
40
    /**
41
     * @param BreadCrumb[] $crumbs
42
     * @return BreadCrumb[]
43
     */
44 View Code Duplication
    private function saveCrumbs($crumbs) {
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...
45
        $this->cookies->create(new Cookie(array_map(function (BreadCrumb $crumb) {
46
            return [
47
                'caption' => $crumb->getCaption(),
48
                'target' => $crumb->getTarget()
49
            ];
50
        }, $crumbs)), self::COOKIE_KEY);
51
        return $crumbs;
52
    }
53
}