Completed
Push — master ( 586166...fecb59 )
by Paweł
47:58
created

TemplateNameResolver::resolveFromArticle()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

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