Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
TwigInstanceContextExtension
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFunctions
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 getActiveInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getActiveInstanceId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllActiveInstances
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isRemoteInstance
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Instance\Presentation\Twig;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Instance\Application\Service\InstanceContextManagerInterface;
12use App\Core\Instance\Domain\Model\ClientInstance;
13use Twig\Extension\AbstractExtension;
14use Twig\TwigFunction;
15
16/**
17 * Twig Extension for accessing Instance Context in UI layouts and headers.
18 *
19 * @package App\Core\Instance\Presentation\Twig
20 */
21final class TwigInstanceContextExtension extends AbstractExtension
22{
23    /**
24     * TwigInstanceContextExtension constructor.
25     *
26     * @param InstanceContextManagerInterface $contextManager Context manager service.
27     */
28    public function __construct(
29        private readonly InstanceContextManagerInterface $contextManager
30    ) {
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function getFunctions(): array
37    {
38        return [
39            new TwigFunction('active_instance', [$this, 'getActiveInstance']),
40            new TwigFunction('active_instance_id', [$this, 'getActiveInstanceId']),
41            new TwigFunction('all_active_instances', [$this, 'getAllActiveInstances']),
42            new TwigFunction('is_remote_instance', [$this, 'isRemoteInstance']),
43        ];
44    }
45
46    /**
47     * Returns currently active client instance or null if local console.
48     *
49     * @return ClientInstance|null Active instance or null.
50     */
51    public function getActiveInstance(): ?ClientInstance
52    {
53        return $this->contextManager->getActiveInstance();
54    }
55
56    /**
57     * Returns active instance ID (0 if local).
58     *
59     * @return int Instance ID.
60     */
61    public function getActiveInstanceId(): int
62    {
63        return $this->contextManager->getActiveInstanceId() ?? 0;
64    }
65
66    /**
67     * Returns list of all active instances.
68     *
69     * @return list<ClientInstance> All active instances.
70     */
71    public function getAllActiveInstances(): array
72    {
73        return $this->contextManager->getAllActiveInstances();
74    }
75
76    /**
77     * Checks if current context is remote client.
78     *
79     * @return bool True if remote.
80     */
81    public function isRemoteInstance(): bool
82    {
83        return $this->contextManager->isRemote();
84    }
85}