Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
33.33% covered (danger)
33.33%
4 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoggedIn
33.33% covered (danger)
33.33%
4 / 12
50.00% covered (danger)
50.00%
1 / 2
16.67
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applies
27.27% covered (danger)
27.27%
3 / 11
0.00% covered (danger)
0.00%
0 / 1
14.62
1<?php
2namespace Apie\Core\Attributes;
3
4use Apie\Core\Context\ApieContext;
5use Apie\Core\ContextConstants;
6use ReflectionClass;
7
8/**
9 * Tell Apie you need to be logged in to see/execute a class/method/property.
10 */
11final class LoggedIn implements ApieContextAttribute
12{
13    /**
14     * @param class-string<object>|null $className
15     */
16    public function __construct(
17        public ?string $className = null
18    ) {
19    }
20    public function applies(ApieContext $context): bool
21    {
22        if (!$context->hasContext(ContextConstants::AUTHENTICATED_USER)) {
23            if ($this->className !== null && $context->hasContext($this->className)) {
24                $value = $context->getContext($this->className);
25                $refl = new ReflectionClass($value);
26                return $refl->getShortName() === 'UserInterface';
27            }
28            return false;
29        }
30        if ($this->className === null) {
31            return true;
32        }
33        $authenticatedUser = $context->getContext(ContextConstants::AUTHENTICATED_USER);
34        $refl = new ReflectionClass($this->className);
35        return $refl->isInstance($authenticatedUser);
36    }
37}