Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

TemplateNameResolver::resolveFromArticle()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 19

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 9.0111
c 0
b 0
f 0
cc 6
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\Model\ArticleInterface;
21
use SWP\Bundle\ContentBundle\Model\RouteInterface;
22
23
/**
24
 * Class TemplateNameResolver.
25
 */
26
class TemplateNameResolver implements TemplateNameResolverInterface
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function resolve($object, $defaultFileName = TemplateNameResolverInterface::TEMPLATE_NAME)
32 12
    {
33
        if ($object instanceof ArticleInterface) {
34 12
            return $this->resolveFromArticle($object);
35 5
        } elseif ($object instanceof RouteInterface) {
36
            return $this->resolveFromRoute($object);
37 7
        }
38
39
        return $defaultFileName;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function resolveFromArticle(ArticleInterface $article, $templateName = TemplateNameResolverInterface::TEMPLATE_NAME)
46 5
    {
47
        /** @param $route RouteInterface */
48
        if (null !== ($route = $article->getRoute())) {
49 5
            if (null !== $route->getTemplateName()) {
50 5
                $templateName = $route->getTemplateName();
51 3
            }
52
53
            if (RouteInterface::TYPE_COLLECTION === $route->getType() && null !== $route->getArticlesTemplateName()) {
54 5
                $templateName = $route->getArticlesTemplateName();
55 3
            }
56
        }
57
58
        if (null !== $article->getTemplateName()) {
59 5
            $templateName = $article->getTemplateName();
60
        }
61
62
        return $templateName;
63 5
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function resolveFromRoute(RouteInterface $route, $templateName = TemplateNameResolverInterface::TEMPLATE_NAME)
69 7
    {
70
        if (null !== $route->getTemplateName()) {
71 7
            $templateName = $route->getTemplateName();
72 2
        }
73
74
        if (RouteInterface::TYPE_COLLECTION === $route->getType() && null === $route->getTemplateName()) {
75 7
            if ($contentTemplateName = $this->getTemplateNameFromRouteContent($route)) {
76 2
                $templateName = $contentTemplateName;
77
            } else {
78
                $templateName = TemplateNameResolverInterface::ROUTE_TEMPLATE_NAME;
79 2
            }
80
        } elseif (RouteInterface::TYPE_CONTENT === $route->getType()) {
81 5
            if ($contentTemplateName = $this->getTemplateNameFromRouteContent($route)) {
82 4
                $templateName = $contentTemplateName;
83
            }
84
        }
85
86
        return $templateName;
87 5
    }
88
89
    /**
90
     * @param RouteInterface $route
91
     *
92
     * @return bool
93
     */
94
    private function getTemplateNameFromRouteContent(RouteInterface $route)
95 6
    {
96
        if (null !== $route->getContent()) {
97 6
            if (null !== $templateName = $route->getContent()->getTemplateName()) {
98
                return $templateName;
99
            }
100
        }
101
102
        return false;
103 6
    }
104
}
105