Completed
Push — master ( 2058f9...b13840 )
by Paweł
31:54
created

TemplateNameResolver::resolveFromArticle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 8.8571
cc 6
eloc 9
nc 10
nop 2
crap 6
1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Core Bundle.
5
 *
6
 * Copyright 2016 Sourcefabric z.u. 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
 * Some parts of that file were taken from the Liip/ThemeBundle
12
 * (c) Liip AG
13
 *
14
 * @copyright 2016 Sourcefabric z.ú.
15
 * @license http://www.superdesk.org/license
16
 */
17
namespace SWP\Bundle\CoreBundle\Resolver;
18
19
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\RouteObjectInterface;
20
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
23
/**
24
 * Class TemplateNameResolver.
25
 */
26
class TemplateNameResolver implements TemplateNameResolverInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31 15
    public function resolve($object, $defaultFileName = 'article.html.twig')
32
    {
33 15
        if ($object instanceof ArticleInterface) {
34 8
            return $this->resolveFromArticle($object);
35
        } elseif ($object instanceof RouteObjectInterface) {
36 7
            return $this->resolveFromRoute($object);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->resolveFromRoute($object); of type boolean|string adds the type boolean to the return on line 36 which is incompatible with the return type declared by the interface SWP\Bundle\CoreBundle\Re...olverInterface::resolve of type string.
Loading history...
37
        }
38
39
        return $defaultFileName;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 8
    public function resolveFromArticle(ArticleInterface $article, $templateName = 'article.html.twig')
46
    {
47
        /** @param $route RouteObjectInterface */
48 8
        if (null !== ($route = $article->getRoute())) {
49 8
            if (null !== $route->getTemplateName()) {
50 1
                $templateName = $route->getTemplateName();
51
            }
52
53 8
            if (RouteObjectInterface::TYPE_COLLECTION === $route->getType() && null !== $route->getArticlesTemplateName()) {
54 2
                $templateName = $route->getArticlesTemplateName();
55
            }
56
        }
57
58 8
        if (null !== $article->getTemplateName()) {
59 1
            $templateName = $article->getTemplateName();
60
        }
61
62 8
        return $templateName;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 7
    public function resolveFromRoute(RouteObjectInterface $route, $templateName = 'article.html.twig')
69
    {
70 7
        if (null !== $route->getTemplateName()) {
71 3
            $templateName = $route->getTemplateName();
72
        }
73
74 7
        if (RouteObjectInterface::TYPE_COLLECTION === $route->getType() && null === $route->getTemplateName()) {
75 2
            if ($contentTemplateName = $this->getTemplateNameFromRouteContent($route)) {
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTemplateNameFromRouteContent($route); of type boolean adds the type boolean to the return on line 86 which is incompatible with the return type declared by the interface SWP\Bundle\CoreBundle\Re...rface::resolveFromRoute of type string.
Loading history...
76
                $templateName = $contentTemplateName;
77
            } else {
78 2
                throw new NotFoundHttpException(sprintf('There is no template file defined for "%s" route!', $route->getId()));
79
            }
80 5
        } elseif (RouteObjectInterface::TYPE_CONTENT === $route->getType()) {
81 3
            if ($contentTemplateName = $this->getTemplateNameFromRouteContent($route)) {
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTemplateNameFromRouteContent($route); of type boolean adds the type boolean to the return on line 86 which is incompatible with the return type declared by the interface SWP\Bundle\CoreBundle\Re...rface::resolveFromRoute of type string.
Loading history...
82
                $templateName = $contentTemplateName;
83
            }
84
        }
85
86 5
        return $templateName;
87
    }
88
89
    /**
90
     * @param RouteObjectInterface $route
91
     *
92
     * @return bool
93
     */
94 5
    private function getTemplateNameFromRouteContent(RouteObjectInterface $route)
95
    {
96 5
        if (null !== $route->getContent()) {
97 1
            if (null !== $templateName = $route->getContent()->getTemplateName()) {
98
                return $templateName;
99
            }
100
        }
101
102 5
        return false;
103
    }
104
}
105