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

AssetExistsViewHelper::evaluateCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
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