Completed
Push — master ( 82cd1d...312098 )
by Nikolas
163:59 queued 138:22
created

BreadCrumbsTrail::hasCrumbs()   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 0
1
<?php
2
namespace rtens\domin\delivery\web;
3
4
use rtens\domin\Action;
5
use rtens\domin\delivery\ParameterReader;
6
7
class BreadCrumbsTrail {
8
9
    /** @var BreadCrumb[] */
10
    private $crumbs;
11
12
    /** @var ParameterReader */
13
    private $reader;
14
15
    /**
16
     * @param ParameterReader $reader
17
     * @param BreadCrumb[] $crumbs
18
     */
19
    public function __construct(ParameterReader $reader, array $crumbs) {
20
        $this->crumbs = $crumbs;
21
        $this->reader = $reader;
22
    }
23
24
    /**
25
     * @return bool
26
     */
27
    public function hasCrumbs() {
28
        return !empty($this->crumbs);
29
    }
30
31
    /**
32
     * @return array|BreadCrumb[]
33
     */
34
    public function getCrumbs() {
35
        return $this->crumbs;
36
    }
37
38
    /**
39
     * @return BreadCrumb
40
     * @throws \Exception If there are no crumbs
41
     */
42
    public function getLastCrumb() {
43
        if (!$this->hasCrumbs()) {
44
            throw new \Exception("There are no crumbs");
45
        }
46
        return $this->crumbs[count($this->crumbs) - 1];
47
    }
48
49
    /**
50
     * @param Action $action
51
     * @param string $actionId
52
     * @return array|BreadCrumb[]
53
     */
54
    public function updateCrumbs(Action $action, $actionId) {
55
        $current = new BreadCrumb($action->caption(), $this->makeTarget($actionId, $action));
56
57
        $newCrumbs = [];
58
        foreach ($this->crumbs as $crumb) {
59
            if ($crumb == $current) {
60
                break;
61
            }
62
            $newCrumbs[] = $crumb;
63
        }
64
        $newCrumbs[] = $current;
65
66
        $this->crumbs = $newCrumbs;
67
        return $newCrumbs;
68
    }
69
70
    public function reset() {
71
        $this->crumbs = [];
72
    }
73
74 View Code Duplication
    private function makeTarget($actionId, Action $action) {
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...
75
        $target = $actionId;
76
77
        $parameters = $this->readRawParameters($action);
78
        if ($parameters) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parameters of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
79
            $keyValues = [];
80
            foreach ($parameters as $key => $value) {
81
                $keyValues[] = urlencode($key) . '=' . urlencode($value);
82
            }
83
            $target .= '?' . implode('&', $keyValues);
84
        }
85
86
        return $target;
87
    }
88
89
    private function readRawParameters(Action $action) {
90
        $values = [];
91
92
        foreach ($action->parameters() as $parameter) {
93
            if ($this->reader->has($parameter)) {
94
                $values[$parameter->getName()] = $this->reader->read($parameter);
95
            }
96
        }
97
        return $values;
98
    }
99
}