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

SilexBreadCrumbsTrail   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 2
dl 8
loc 36
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A readCrumbs() 0 10 2
A getCookie() 8 8 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\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
}