Completed
Push — 1.4 ( f8c621...762927 )
by Rafał
22:37 queued 11:18
created

MediaRouter::generate()   B

Complexity

Conditions 8
Paths 7

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.4444
c 0
b 0
f 0
cc 8
nc 7
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Content Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2016 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\ContentBundle\Routing;
16
17
use SWP\Bundle\ContentBundle\Model\ArticleMedia;
18
use SWP\Bundle\ContentBundle\Model\AuthorMedia;
19
use SWP\Bundle\ContentBundle\Model\FileInterface;
20
use SWP\Bundle\ContentBundle\Model\ImageInterface;
21
use SWP\Bundle\ContentBundle\Model\ImageRendition;
22
use SWP\Bundle\ContentBundle\Model\ImageRenditionInterface;
23
use SWP\Bundle\ContentBundle\Model\PreviewUrlAwareInterface;
24
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta;
25
use Symfony\Bundle\FrameworkBundle\Routing\Router;
26
use Symfony\Cmf\Component\Routing\VersatileGeneratorInterface;
27
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
28
29
class MediaRouter extends Router implements VersatileGeneratorInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
35
    {
36
        if (null === $item = $this->getItem($name)) {
37
            return;
38
        }
39
40
        $routeName = 'swp_media_get';
41
        if ($name instanceof Meta && $name->getValues() instanceof AuthorMedia) {
42
            $routeName = 'swp_author_media_get';
43
        }
44
45
        if ($name->getValues() instanceof ImageRenditionInterface && null !== $name->getValues()->getPreviewUrl()) {
0 ignored issues
show
Bug introduced by
The method getValues cannot be called on $name (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
46
            return $name->getValues()->getPreviewUrl();
0 ignored issues
show
Bug introduced by
The method getValues cannot be called on $name (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
47
        }
48
49
        if ($item instanceof PreviewUrlAwareInterface && null !== ($previewUrl = $item->getPreviewUrl())) {
50
            return $previewUrl;
51
        }
52
53
        $parameters['mediaId'] = $item->getAssetId();
54
        $parameters['extension'] = $item->getFileExtension();
55
56
        return parent::generate($routeName, $parameters, $referenceType);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function supports($name)
63
    {
64
        return $name instanceof Meta && (
65
            $name->getValues() instanceof ArticleMedia ||
66
            $name->getValues() instanceof ImageRendition ||
67
            $name->getValues() instanceof AuthorMedia
68
        );
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getRouteDebugMessage($name, array $parameters = array())
75
    {
76
        return 'Route for media '.$name->getValues()->getId().' not found';
77
    }
78
79
    private function getItem($name)
80
    {
81
        $values = $name->getValues();
82
        if ($name->getValues() instanceof ImageRendition) {
83
            return $name->getValues()->getImage();
84
        }
85
86
        if (($image = $values->getImage()) instanceof ImageInterface) {
87
            return $image;
88
        } elseif (($file = $values->getFile()) instanceof FileInterface) {
89
            return $file;
90
        }
91
    }
92
}
93