1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Seo\Resolver; |
4
|
|
|
|
5
|
|
|
use Bankiru\Seo\Destination\CartesianDestinationGenerator; |
6
|
|
|
use Bankiru\Seo\Destination\DestinationCompiler; |
7
|
|
|
use Bankiru\Seo\Destination\DestinationGenerator; |
8
|
|
|
use Bankiru\Seo\Entity\TargetLinkInterface; |
9
|
|
|
use Bankiru\Seo\Exception\DestinationException; |
10
|
|
|
use Bankiru\Seo\Exception\LinkResolutionException; |
11
|
|
|
use Bankiru\Seo\SourceInterface; |
12
|
|
|
|
13
|
|
|
final class TargetLinkResolver implements LinkResolver |
14
|
|
|
{ |
15
|
|
|
/** @var DestinationGenerator */ |
16
|
|
|
private $generator; |
17
|
|
|
/** @var SourceRegistry */ |
18
|
|
|
private $registry; |
19
|
|
|
/** @var DestinationCompiler */ |
20
|
|
|
private $compiler; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TargetLinkResolver constructor. |
24
|
|
|
* |
25
|
|
|
* @param SourceRegistry $registry |
26
|
|
|
* @param DestinationCompiler $compiler |
27
|
|
|
* @param DestinationGenerator $generator |
28
|
|
|
*/ |
29
|
1 |
|
public function __construct( |
30
|
|
|
SourceRegistry $registry, |
31
|
|
|
DestinationCompiler $compiler, |
32
|
|
|
DestinationGenerator $generator = null |
33
|
|
|
) { |
34
|
1 |
|
$this->registry = $registry; |
35
|
1 |
|
$this->compiler = $compiler; |
36
|
1 |
|
$this->generator = $generator ?: new CartesianDestinationGenerator(); |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** {@inheritdoc} */ |
40
|
1 |
|
public function resolve($link) |
41
|
|
|
{ |
42
|
1 |
|
if (!$this->supports($link)) { |
43
|
|
|
throw new LinkResolutionException('Unsupported link type to resolve'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var TargetLinkInterface $link */ |
47
|
|
|
/** @var SourceInterface[] $sources */ |
48
|
1 |
|
$sources = []; |
49
|
|
|
try { |
50
|
1 |
|
foreach ($link->getSources() as $code => $key) { |
51
|
1 |
|
$sources[$code] = $this->registry->get($key); |
52
|
1 |
|
} |
53
|
1 |
|
} catch (\OutOfBoundsException $exception) { |
54
|
|
|
throw new LinkResolutionException( |
55
|
|
|
'Cannot resolve link: ' . $exception->getMessage(), |
56
|
|
|
$exception->getCode(), |
57
|
|
|
$exception |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
1 |
|
foreach ($sources as $code => $source) { |
63
|
1 |
|
$source->withCondition($link->getTarget()->getCondition($code)); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
$destinations = $this->generator->generate($link->getTarget()->getRoute(), $sources, $link->getFillers()); |
67
|
|
|
|
68
|
1 |
|
$links = []; |
69
|
1 |
|
foreach ($destinations as $destination) { |
70
|
1 |
|
$links[] = $this->compiler->compile($destination, $link->getTitleTemplate(), []); |
71
|
1 |
|
} |
72
|
1 |
|
} catch (DestinationException $exception) { |
73
|
|
|
throw new LinkResolutionException( |
74
|
|
|
'Error expanding destinations for link: ' . $exception->getMessage(), |
75
|
|
|
$exception->getCode(), |
76
|
|
|
$exception |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
return $links; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** {@inheritdoc} */ |
84
|
1 |
|
public function supports($link) |
85
|
|
|
{ |
86
|
1 |
|
return $link instanceof TargetLinkInterface; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|