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

CurirBreadCrumbsTrail   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 20.93 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 2
dl 9
loc 43
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A updateCrumbs() 0 3 1
A readCrumbs() 0 8 2
A saveCrumbs() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}