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) { |
|
|
|
|
75
|
|
|
$target = $actionId; |
76
|
|
|
|
77
|
|
|
$parameters = $this->readRawParameters($action); |
78
|
|
|
if ($parameters) { |
|
|
|
|
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
|
|
|
} |
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.