Passed
Push — main ( c9076e...0f9b2d )
by Iain
04:41
created

EntityViewType   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 31
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setData() 0 3 1
A getHtmlOutput() 0 11 2
A getName() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Iain Cambridge 2020-2023.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.2.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Athena\ViewType;
16
17
use Parthenon\Athena\Entity\CrudEntityInterface;
18
use Parthenon\Athena\SectionManagerInterface;
19
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20
21
class EntityViewType implements ViewTypeInterface
22
{
23
    private mixed $data;
24
25
    public function __construct(
26
        private UrlGeneratorInterface $urlGenerator,
27
        private SectionManagerInterface $sectionManager,
28
    ) {
29
    }
30
31
    public function getName(): string
32
    {
33
        return 'entity';
34
    }
35
36
    public function setData($data)
37
    {
38
        $this->data = $data;
39
    }
40
41
    public function getHtmlOutput(): string
42
    {
43
        if (!$this->data instanceof CrudEntityInterface) {
44
            throw new \InvalidArgumentException('Invalid entity');
45
        }
46
47
        $section = $this->sectionManager->getByEntity($this->data);
48
49
        $url = $this->urlGenerator->generate('parthenon_athena_crud_'.$section->getUrlTag().'_read', ['id' => $this->data->getId()]);
50
51
        return sprintf('<a href="%s">%s</a>', $url, $this->data->getDisplayName());
52
    }
53
}
54