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