Completed
Push — master ( e05b73...a0cd97 )
by Aske
16s queued 11s
created

AssetExistsViewHelper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 5 1
A evaluateCondition() 0 9 2
1
<?php
2
namespace AE\History\ViewHelpers;
3
4
use Doctrine\ORM\EntityNotFoundException;
5
use Neos\Flow\Annotations as Flow;
6
use Neos\FluidAdaptor\Core\ViewHelper\AbstractConditionViewHelper;
7
use Neos\FluidAdaptor\Core\ViewHelper\Exception as ViewHelperException;
8
use Neos\Media\Domain\Model\AssetInterface;
9
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
10
11
/**
12
 * Checks if an asset exists.
13
 */
14
class AssetExistsViewHelper extends AbstractConditionViewHelper
15
{
16
    /**
17
     * @return void
18
     *
19
     * @throws ViewHelperException
20
     */
21
    public function initializeArguments() : void
22
    {
23
        $this->registerArgument('asset', AssetInterface::class, 'The asset to check for existence.', true);
24
        $this->registerArgument('then', 'mixed', 'Value to be returned if the asset exists.', false);
25
        $this->registerArgument('else', 'mixed', 'Value to be returned if the asset doesn\'t exist.', false);
26
    }
27
28
    /**
29
     * @param array|null $arguments
30
     * @param RenderingContextInterface $renderingContext
31
     *
32
     * @return bool
33
     */
34
    protected static function evaluateCondition($arguments = null, RenderingContextInterface $renderingContext) : bool
35
    {
36
        try {
37
            $arguments['asset']->getResource();
38
        } catch (EntityNotFoundException $exception) {
39
            return false;
40
        }
41
42
        return true;
43
    }
44
}
45